我在这个简单的事情上遇到了麻烦。我有一个返回的方法Optional<TypeA>:
public Optional<TypeA> getFirst() {
return set.stream().filter(it -> it.getStatus() == SUCCESS).findFirst(); // set is a Set<TypeA>
}
Run Code Online (Sandbox Code Playgroud)
...然后我从以下位置调用它:
public boolean hasFirst() {
return types.parallelStream() // types is a List<TypeB> that has a Set<TypeA>
.filter(it -> it.getTotalAmount() > 0)
.map(TypeA::getFirst)
.findFirst() // this can return an Optional[Optional.empty]
.isPresent();
}
Run Code Online (Sandbox Code Playgroud)
问题是,Optional空的Optional正true按预期返回,所以我不知道如何正确处理此问题!
有什么线索吗?
考虑以下代码:
List<String> myList = Arrays.asList(1, 2, 3);
String[] myArray1 = myList.toArray(new String[myList.size()]);
String[] myArray2 = myList.stream().toArray(String[]::new);
assert Arrays.equals(myArray1, myArray2);
Run Code Online (Sandbox Code Playgroud)
在我看来,使用流要简单得多。
因此,我测试了每个的速度。
List<String> myList = Arrays.asList("1", "2", "3");
double start;
start = System.currentTimeMillis();
for (int i = 0; i < 10_000_000; i++) {
String[] myArray1 = myList.toArray(new String[myList.size()]);
assert myArray1.length == 3;
}
System.out.println(System.currentTimeMillis() - start);
start = System.currentTimeMillis();
for (int i = 0; i < 10_000_000; i++) {
String[] myArray2 = myList.stream().toArray(String[]::new);
assert myArray2.length == 3;
}
System.out.println(System.currentTimeMillis() - start); …Run Code Online (Sandbox Code Playgroud)我有一个列表(GlobalBooks),看起来类似于下面
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
GlobalBooks globalBooks = new GlobalBooks();
List<Book> bookList = new ArrayList<Book>();
Book book = new Book();
List<BookContent> bookContents = new ArrayList<BookContent>();
book.setBookName("A");
BookContent content = new BookContent();
content.setDescription("December 2016");
content.setComponentID(20l);
bookContents.add(content);
content = new BookContent();
content.setDescription("January 2016");
content.setComponentID(30l);
bookContents.add(content);
content = new BookContent();
content.setDescription("Febuary 2016");
content.setComponentID(40l);
bookContents.add(content);
book.setContents(bookContents);
bookList.add(book);
book = new Book();
bookContents = new ArrayList<BookContent>();
book.setBookName("B");
content = new BookContent();
content.setDescription("December 2016");
content.setComponentID(20l);
bookContents.add(content); …Run Code Online (Sandbox Code Playgroud) 为什么toString我在使用时无法解决该方法Arrays.stream()或IntStream:
int row[] = ...
Arrays.stream(row)
.map(Arrays::toString)
.forEach(System.out::println);
Run Code Online (Sandbox Code Playgroud)
但是,另一方面,它可以在我使用时解决Stream.of():
int row[] = ...
Stream.of(row)
.map(Arrays::toString)
.forEach(System.out::println);
Run Code Online (Sandbox Code Playgroud) 我想组合列表中的每两个字符串并使用java8流返回组合列表:
List<String> list;
Stream.concat(list.stream(), list.stream())
.collect(toList());
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 8流API的最佳方法?
更新细节
文件格式为逗号分隔值,文件大小约为4 GB
我有一个对象,"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并返回获得更多收益的开发人员.这将返回流中对象的最大工资,但是如何检索具有最大工资的对象?