我有一个基本的SpringBoot应用程序.使用Spring Initializer,JPA,嵌入式Tomcat,Thymeleaf模板引擎和包作为可执行JAR文件我想创建一个基于POJO的自定义SummaryStatistics,它有2个字段,price和updateDate.此统计数据应该是最小/最大价格和最小/最大价格的日期.
public class MenuPriceSummaryStatistics implements Consumer<MenuPrice> {
private long count;
private double sum;
private double sumCompensation; // Low order bits of sum
private double simpleSum; // Used to compute right sum for non-finite inputs
private double min = Double.POSITIVE_INFINITY;
private double max = Double.NEGATIVE_INFINITY;
private Date maxDate;
private Date minDate;
public MenuPriceSummaryStatistics() {
}
/**
* Combines the state of another {@code DoubleSummaryStatistics} into this one.
*
* @param other
* another {@code DoubleSummaryStatistics}
* @throws NullPointerException
* if …Run Code Online (Sandbox Code Playgroud) lambda functional-programming java-8 java-stream functional-interface
我有以下字符串列表
List<String> product_a= Arrays.asList("Product A", "Category A", "Price");
List<String> product_b= Arrays.asList("Product A", "Category A", "Price");
List<String> product_c= Arrays.asList("Product B", "Category B", "Price");
List<String> product_d = Arrays.asList("Product C", "Category C", "Price");
Run Code Online (Sandbox Code Playgroud)
然后,我把它们放在另一个列表中
List<List<String>> products = new ArrayList<>();
products.add(product_a);
products.add(product_b);
products.add(product_c);
products.add(product_d);
Run Code Online (Sandbox Code Playgroud)
使用流,Collectors.groupingBy,Collectors.counting,我如何获得以下输出?可能吗?
Product A - 2
Product B - 1
Product C - 1
Run Code Online (Sandbox Code Playgroud)
提前谢谢你的帮助.
我试图将一个字符串转换为一个Stream<Character>并遇到这个奇怪的事情.
它似乎word.toCharArray()返回一个数组,但当我尝试将其转换为列表时Arrays.asList(word.toCharArray()),它似乎失败了(输出是一个完整的字符串).
这里发生了什么?
@Test
public void testCharacterStream() {
characterStreamNew("HELLO");
}
private Stream<Character> characterStreamNew(String word) {
for(Character c: word.toCharArray()) {
out.println(c);
}
Arrays.asList(word.toCharArray()).stream()
.peek(out::println)
.forEachOrdered(out::println);
return null;
}
Run Code Online (Sandbox Code Playgroud)
这是输出:
H
E
L
L
O
HELLO
HELLO
Run Code Online (Sandbox Code Playgroud) 我是java 8的新手,出于某种原因我对了解流有问题.假设我们有一个对象列表List <MyObject>,其中MyObject有2个字段:Long Id,Date insertTime,我想删除具有相同ID和更早时间的元素.
有2个for循环,它是这样的:
for(MyObject object : myObjects) {
for(MyObject tmpObject : myObjects) {
if(object.getId() == tmpObject.getId()) {
if(object.getInsertDate().after(tmpObject.getInsertDate()))
myObjects.remove(tmpObject);
else
myObjects.remove(object);
}
}
}
Run Code Online (Sandbox Code Playgroud)
使用流时看起来如何?
@Michael我的坏.比我需要另一个列表或数组来存储响应数据.
让我说我有
所以我需要的结果
谢谢你们.
我有一个List,我需要将它转换为Map但具有相同的键顺序,所以我需要转换为LinkedHashMap.我需要这样的东西:
list.stream().collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
Run Code Online (Sandbox Code Playgroud)
但具体的地图类型,如:
list.stream().collect(Collectors.toCollection(LinkedHashMap::new))
Run Code Online (Sandbox Code Playgroud)
是否有可能将上述两种变体结合起来?
阅读有关java 8流的文章,并找到了
Java Streams是可以使用的,因此无法创建对流的引用以供将来使用.由于数据是按需的,因此不可能多次重复使用相同的流.
同时在同一篇文章中
//sequential stream
Stream<Integer> sequentialStream = myList.stream();
//parallel stream
Stream<Integer> parallelStream = myList.parallelStream();
Run Code Online (Sandbox Code Playgroud)
"没有办法为将来的使用创建流的引用"是什么意思?不是sequentialStream和parallelStream流的引用
它也意味着"多次重用同一个流是不可能的"?
我有一个用例,我有一个数据源,比方说:每一秒,一个新的字符串来自该数据源.
我想创建一个管道,如果新的字符串到达,它将被推送到该管道进行处理.
我猜Java 8引入的Stream API可以做到这一点,因为它有方便的功能来处理任意集合的数据,但是我想跳过我收集数据的部分到一个单独的集合并派遣到达数据直接到我刚刚创建的Stream.
有没有办法做到这一点?
我尝试从流中获取订单地图.我有下一个代码:
Map<String, PatternWrapper> regexps = allConfigurations
.stream()
.flatMap(configuration -> configuration.getPlugins().stream())
.sorted(
(left, right) -> {
if (left.getType().equals(ObfuscationType.REGEX_BLACKLIST) &&
right.getType().equals(ObfuscationType.REGEX_BLACKLIST))
return 0;
else if (left.getType().equals(ObfuscationType.REGEX_BLACKLIST) &&
(!right.getType().equals(ObfuscationType.REGEX_BLACKLIST)))
return 1;
else
return -1;
}
)
// .sorted(LogConfiguration.Plugin::compareTo)
.filter(plugin -> (ObfuscationType.REGEX.equals(plugin.getType())
|| ObfuscationType.REGEX_BLACKLIST.equals(plugin.getType())))
.collect(Collectors.toMap(
plugin -> plugin.getId(),
plugin -> new
PatternWrapper(plugin.getId(), Pattern.
compile(plugin.getValue()))));
return regexps;
Run Code Online (Sandbox Code Playgroud)
在哪里我尝试订购数据流,我认为该地图也是订购的.但是我错了.我认为调用collect(Collectors.toMap())不保存流的顺序.有没有例子我怎么能这样做?
我有两个字符串列表.我想检查一个列表中的任何字符串是否在另一个列表中可用.使用下面的方法失败了.
请让我知道一个更好的方法
List<String> mylist = Arrays.asList(stringArray1);
List<String> items = Arrays.asList(stringArray2);
return mylist.stream().anyMatch(t->items.stream().anyMatch(t::contains));
Run Code Online (Sandbox Code Playgroud) 不确定这里的语法,lambda表达式和排序的新功能.
itemStream.parallel().filter(Objects::nonNull).forEach(
(item) -> randomMethod(item));
Run Code Online (Sandbox Code Playgroud)
我之前在for循环中所做的是检查null,然后在遇到null项时发出警告
log.warn("Skipping a null item!");
Run Code Online (Sandbox Code Playgroud)
如何使用基于流的方法在遇到空对象(同时仍在过滤时)时进行记录?