线程池线程?(pthread threadpool?)
pthread库中是否包含一个线程池实现? 还是有常用的库,人们使用/
does the pthread lib include a threadpool implementation? or are there commonly used libs that folks use/
原文:https://stackoverflow.com/questions/4568517
最满意答案
由于混合类型,这有点令人费解,但这可以完全用lambdas来完成:
Stream<Object> stream = Stream.concat(list1.stream(), list2.stream()); System.out.println(stream.sorted((Object o1, Object o2) -> (o1 instanceof String ? 2*((String)o1).length() : 1+2*(Integer)o1) - (o2 instanceof String ? 2*((String)o2).length() : 1+2*(Integer)o2) ).collect(Collectors.toList()));
基本的想法是使用标准的compareTo逻辑
a - b
来定义一个比较长度(如果它是一个字符串)和该值(如果它是一个整数)的lambda。 但是,为了处理一个关系的情况,我将所有值乘以2
,然后在Integer
数值中加上一个额外的1
。 这会导致Integer
在排序中最后排序。演示: http : //ideone.com/39ppGD
This is slightly convoluted because of the mixed types, but here is one way it could be done entirely with lambdas:
Stream<Object> stream = Stream.concat(list1.stream(), list2.stream()); System.out.println(stream.sorted((Object o1, Object o2) -> (o1 instanceof String ? 2*((String)o1).length() : 1+2*(Integer)o1) - (o2 instanceof String ? 2*((String)o2).length() : 1+2*(Integer)o2) ).collect(Collectors.toList()));
The basic idea is to define a lambda that compares the length (if it's a string) to the value (if it's an integer) using the standard compareTo logic
a - b
. However, to handle the case of a tie I've multiplied all the values by2
, and then added an extra1
to theInteger
values. This causesInteger
to be sorted last in the case of a tie.Demo: http://ideone.com/39ppGD
相关问答
更多-
@Test public void test14() throws Exception { Map
m1 = ImmutableMap.of("a", 2, "b", 3); Map m2 = ImmutableMap.of("a", 3, "c", 4); Map mx = Stream.of(m1, m2) .map(Map::entrySet) ... -
不要使用new Integer(String)来解析数字。 它在Java 9中已被弃用 。 使用Integer.parseInt(String s)或者Integer.valueOf(String s)如果你坚持装箱价值(在这种情况下是浪费)。 你所追求的lambda是: results.sort((r1,r2) -> Integer.compare(Integer.parseInt(r1.getNumOrder()), Int ...
-
如果您定义了具有多个类型参数的功能界面,这是可能的。 没有这样的内置类型。 (有几个有限的类型有多个参数。) @FunctionalInterface interface Function
{ public Six apply(One one, Two two, Three three, Four four, Five five); } public static void main(String[] args) throws E ... -
您可以创建帮助方法,将使用stream式map操作将类型T的列表(数组)转换为类型为U的列表(数组)。 //for lists public static
List convertList(List from, Function func) { return from.stream().map(func).collect(Collectors.toList()); } //for arrays public static U[] convertA ... -
由于混合类型,这有点令人费解,但这可以完全用lambdas来完成: Stream
-
class Utils
{ List getList(T t, List - list) {
return list.stream().filter(i -> t.getClass().isInstance(i.get(0))).flatMap(List
::stream).collect(Collectors.toList()); } } class Utils { List getList(T t, List - ...
-
我在java 8 lambda Predicate
上做错了什么?(What am i doing wrong with java 8 lambda Predicate [2023-08-21]? [duplicate]) 首先,让我们看看事物的规模。 你说的是100000000个项目的差异大约1505毫秒,或者每个项目大约15 纳秒 。 这个开销不是很大。 也就是说,开销是为了Predicate而将所有int s自动装入Integers 。 Predicate::test需要一个Integer ,因此p.test(i)实际上被编译为p.test(Integer.valueOf(i)) 。 这种方法并不是超级昂贵的,但它不是免费的。 显然,您的计算机需要大约15纳秒。 如果你使用IntPredicate - ... -
不,你根本做不到。 AutoBean要求静态输入所有内容 :没有多态,也没有混合类型的地图列表。 您可能对RequestFactory对JSON-RPC的内置支持感兴趣。 No, you simply can't. AutoBean requires everything to be statically typed: no polymorphism, and no mixed-typed lists of maps. You might be interested by RequestFactory's ...
-
您的比较器无效,因为它返回一个boolean而期望的返回类型是一个int 。 一个简单的解决方案是: jobs.sort(Comparator.comparing(e -> e.get(0))); 要么: jobs.sort((j1, j2) -> Integer.compare(j1.get(0), j2.get(0))); Your comparator is invalid as it returns a boolean whereas the expected return type is an ...
-
如何在Double not Integer上进行Java lambda求和?(How to do Java lambda sum on Double not Integer?)[2021-12-09]
除了变量'thesum'在使用前没有初始化; 代码看起来很好,并在eclipse中按预期编译和运行。 为了清楚起见,我在下面添加了一个示例 public static void main(String[] args) { Maptimes = new HashMap<>(); times.put("1", 1.0); times.put("2", 2.0); times.put("null", null); times.put("3", ...