我正在尝试将我的Object []数组映射并过滤到int []数组.如果对象是int,则工作得很好,但如果没有,则抛出强制转换异常.我想知道我是否能以某种方式在lambda表达式中附加try/catch?这是我的代码:
b[i] = Arrays.stream(item).mapToInt(e -> (int) e).filter(e -> e % 2 != 0).toArray();
Run Code Online (Sandbox Code Playgroud)
或更好的方法是尝试/捕获整块?
所以我有一个键值对的HashMap,并希望创建一个使用每个键值对实例化的新对象列表.例如:
//HashMap of coordinates with the key being x and value being y
Map<Integer, Integer> coordinates = new HashMap<Integer, Integer>();
coordinates.put(1,2);
coordinates.put(3,4);
List<Point> points = new ArrayList<Point>();
//Add points to the list of points instantiated using key-value pairs in HashMap
for(Integer i : coordinates.keySet()){
points.add(new Point(i , coordinates.get(i)));
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能使用Java 8流做同样的事情.
我想将Java 7代码重构为Java 8.
这里是Java 7代码:
List<A> aList = new ArrayList<>();
for (B b : bList) {
D d = new D(b.getSomeWhat());
d.setDisabled(true);
aList.add(d);
}
Run Code Online (Sandbox Code Playgroud)
我在Java 8中尝试这个:
bList.stream().map(b -> {
D d = new new D(b.getSomeWhat());
d.setDisabled(true);
}).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
我有一个missisng return statement错误.
如何用Java 8流编写代码?
我想从大文件内容中找到重复的字数.有没有使用java 8流API的最佳方法?
更新细节
文件格式为逗号分隔值,文件大小约为4 GB
List<String> strList1 = Arrays.asList("aaa", "bbb", "ccc");
List<String> strList2 = Arrays.asList("ddd", "eee", "fff");
List<List<String>> list = Arrays.asList(strList1,strList2);
int noOfElements = (int) list.stream().flatMap(i->i.stream()).count();
System.out.println(noOfElements);
Run Code Online (Sandbox Code Playgroud)
我想计算个人数量Strings(如上面的代码片段所示).在展平列表后我能够这样做,但我想在不使用的情况下做同样的事情flatMap().必须有一些方法来计算.提前致谢.
我在一个方法中有这个列表:
List<? extends Object> objects;
Run Code Online (Sandbox Code Playgroud)
或者它也可以是:
List<?> objects;
Run Code Online (Sandbox Code Playgroud)
我想用Java 8 lambda这样排序:
objects.sort((p1, p2) -> p1.compareTo(p2));
Run Code Online (Sandbox Code Playgroud)
但它说:
方法compareTo(捕获#6-of?)未定义类型捕获#6-of?
我也尝试过泛型类型:
List<O extends Object> objects;
Run Code Online (Sandbox Code Playgroud)
但随后它说:
对于类型O,方法compareTo(O)未定义
我怎么能这样排序一个列表?我需要它在我的抽象类中,它通过反射识别列表字段,并且可以对其进行排序.
我创建这段代码:
static int i;
public static void main(String[] args) throws InterruptedException {
for (i = 1; i <= 5; i++) {
new Thread(() -> {
hello(i);
}).start();
}
}
public static void hello(int i) {
System.out.println("Thread number = " + i);
}
Run Code Online (Sandbox Code Playgroud)
当你看到这个,你认为它会打印出来:
Thread number = 1
Thread number = 2
Thread number = 3
Thread number = 4
Thread number = 5
Run Code Online (Sandbox Code Playgroud)
但这不正确它打印一个随机的int和它打印的不合逻辑6有时像:
Thread number = 5
Thread number = 6
Thread number = 6
Thread number …Run Code Online (Sandbox Code Playgroud) 我有一个对象,"Item",带有字段:int:id String:price
字符串价格包含一个或多个以逗号分隔的价格值.
问题:使用流操作我要地图的类型,Map<Integer,Set<Integer>>从List<Item> items
其中Map的键是id,Set的值是从String价格中提取的整数.
列表中会有重复的ID,并且可以有不同的价格字符串.
我想出了一种产生以下结果的方法:
Map<Integer, Set<List<Integer>>> itemStoresMapIntermediate = items.stream()
.collect(Collectors.groupingBy(Item::getItemId,Collectors.mapping(Item::getStoresAsIntList, Collectors.toSet())));
Run Code Online (Sandbox Code Playgroud)
getStoresAsIntList()返回对象中String价格的价格值列表.
以上肯定不是我想要的.
java 8 API doc SortedSet仅适用于stream()继承的状态java.util.Collection(请参阅https://docs.oracle.com/javase/8/docs/api/java/util/SortedSet.html).这意味着流是顺序的,但可能不是有序的.
那么,确保sortedSet.stream().filter(...).findFirst()行为与for( ... )返回第一个匹配元素的经典循环相同的最安全方法是什么?或者情况已经如此,但不保证是API?
(findFirst()api doc:如果流没有遭遇顺序,则可以返回任何元素.)
Stream.sorted() 应该做的伎俩,但这增加了排序元素的开销,这些元素已经在原始集合中排序.
我有这个简单的指示
Stream.concat(manager.getChild().stream(),
manager1.getChild().stream())
.map(dev -> dev.getSalary())
.reduce(0, Integer::max);
Run Code Online (Sandbox Code Playgroud)
结束两个List并返回获得更多收益的开发人员.这将返回流中对象的最大工资,但是如何检索具有最大工资的对象?