Tag: java stream

必须partitioningBy生成一个包含true和false条目的地图?

partitioningBy收集器将谓词应用于流中的每个元素,并生成从布尔到满足或不满足谓词的流中元素列表的映射。 例如: Stream.of(1,2,3,4).collect(partitioningBy(x -> x >= 3)) // {false=[1, 2], true=[3, 4]} 正如分区的目的是什么所讨论的 , 观察到的行为是partitioningBy总是返回一个包含true和false条目的映射。 例如: Stream.empty().collect(partitioningBy(x -> false)); // {false=[], true=[]} Stream.of(1,2,3).collect(partitioningBy(x -> false)); // {false=[1, 2, 3], true=[]} Stream.of(1,2,3).collect(partitioningBy(x -> true)); // {false=[], true=[1, 2, 3]} 这种行为实际上是在某处指定的吗? Javadoc只说: 返回一个Collector,它根据Predicate对输入元素进行分区,并将它们组织成Map<Boolean, List> 。 返回的Map的类型,可变性,可序列化或线程安全性无法保证。 符合实现可以返回这些: Stream.empty().collect(partitioningBy(x -> false)); // {}, or {false=[]}, or {true=[]} Stream.of(1,2,3).collect(partitioningBy(x […]

如何在Java Stream上应用多个filter?

我必须通过Map过滤对象集合,它保存对象字段名称和字段值的键值对。 我试图通过stream()。filter()应用所有filter。 对象实际上是JSON,因此Map保存其变量的名称以及它们必须包含的值以便被接受,但为了简单起见并且因为它与问题无关,我编写了一个简单的Testclass来模拟行为: public class TestObject { private int property1; private int property2; private int property3; public TestObject(int property1, int property2, int property3) { this.property1 = property1; this.property2 = property2; this.property3 = property3; } public int getProperty(int key) { switch(key) { case 1: return property1; case 2: return property2; default: return property3; } } } […]

使用Collectors.summingInt时如何获取自定义类型而不是Integer?

我目前正在创建一个Map<String, Map> ,其中Integer表示秒: Map<String, Map> map = stream.collect(Collectors.groupingBy( x -> x.getProject(), Collectors.groupingBy( x -> x.getDate(), Collectors.summingInt(t -> t.getDuration().toSecondOfDay()) ) )); 我怎样才能创建Map<String, Map> ?

如何使流管道更简单

我认为我的代码需要改进。 我在流的filter()和map()阶段使用对象allSummaryTSTLog ,所以我必须调用File.listFiles两次: public static List ParserPath(List allLogPath) { FilenameFilter filter = new MyFilter(“Summary_TSTLog”); return allLogPath.parallelStream().filter(path -> { File testPath = new File(path); if (!testPath.isDirectory()) { MyLog.log.info(“test path : [” + path + “] is not exist, continue”); return false; } File[] allSummaryTSTLog = testPath.listFiles(filter); if (allSummaryTSTLog == null || allSummaryTSTLog.length == 0) { MyLog.log.info(“test […]

Collectors.groupingBy不接受null键

在Java 8中,这适用于: Stream stream = Stream.of(ArrayList.class); HashMap<Class, List> map = (HashMap)stream.collect(Collectors.groupingBy(Class::getSuperclass)); 但这不是: Stream stream = Stream.of(List.class); HashMap<Class, List> map = (HashMap)stream.collect(Collectors.groupingBy(Class::getSuperclass)); Maps允许使用null键,List.class.getSuperclass()返回null。 但Collectors.grouping可以在Collectors.java第907行发布NPE: K key = Objects.requireNonNull(classifier.apply(t), “element cannot be mapped to a null key”); 如果我创建自己的收集器,它的工作原理改为: K key = classifier.apply(t); 我的问题是: 1)Collectors的Javadoc.groupingBy并没有说它不应该映射一个空键。 出于某种原因这种行为是否必要? 2)是否有另一种更简单的方法来接受一个空键,而不必创建我自己的收集器?

字符串的字符串IntStream – Java

是否可以将字符串str.chars()的流转换为使用字符串流,其中每个字符串包含5个字符,例如?

按嵌套映射的值对外部映射进行排序

按照以前保留外键和内键的嵌套映射中的列表大小对外部映射Map<String, Map<String, List>>进行排序 。

将一系列整数分组到函数的答案

对于一系列整数,我想应用(“昂贵的”)操作,仅过滤掉那些有趣答案的整数,然后分组答案。 第一个片段有效,但它在代码和计算中复制了操作(“模数2”): IntStream.range(1, 10).boxed() .filter(p -> (p % 2 != 0)) // Expensive computation .collect(Collectors.groupingBy(p -> (p % 2))); // Same expensive // computation! // {1=[1, 3, 5, 7, 9]} (Correct answer) 我尝试先映射到答案,然后过滤,然后分组 – 但最初的整数当然会在途中丢失: IntStream.range(1, 10).boxed() .map(p -> p % 2) // Expensive computation .filter(p -> p != 0) .collect(Collectors.groupingBy(p -> p)); // {1=[1, […]

为什么我们必须将Collectors.toList()返回的List转换为List ,即使Stream的元素已经映射到Integer?

我将原始Stream映射到Stream ,然后将元素收集到List 。 如果我的mapper – .map(str -> ((String)str).length()) – 我已经映射到Integer为什么我必须将collect(Collectors.toList())为List ? 我的代码: List list = Arrays.asList(“test”, “test2”); List lengths = (List) list.stream() .map(str -> ((String)str).length()) .collect(Collectors.toList()); 如果我不使用原始List ,则无需强制转换: List list = Arrays.asList(“test”, “test2”); List lengths = list.stream() .map(str -> str.length()) .collect(Collectors.toList());

组,收集器,映射(Int到String),映射(映射到对象)

这是我之前在Group上的问题的延续,Sum byType然后使用Java流获得差异 。 正如所建议的那样,我应该发布一个单独的线程而不是更新原始线程。 因此,在我之前的一系列问题中,我已经实现了这一点,现在,继续进行。 背景: 我有以下数据集 Sample(SampleId=1, SampleTypeId=1, SampleQuantity=5, SampleType=ADD), Sample(SampleId=2, SampleTypeId=1, SampleQuantity=15, SampleType=ADD), Sample(SampleId=3, SampleTypeId=1, SampleQuantity=25, SampleType=ADD), Sample(SampleId=4, SampleTypeId=1, SampleQuantity=5, SampleType=SUBTRACT), Sample(SampleId=5, SampleTypeId=1, SampleQuantity=25, SampleType=SUBTRACT) Sample(SampleId=6, SampleTypeId=2, SampleQuantity=10, SampleType=ADD), Sample(SampleId=7, SampleTypeId=2, SampleQuantity=20, SampleType=ADD), Sample(SampleId=8, SampleTypeId=2, SampleQuantity=30, SampleType=ADD), Sample(SampleId=9, SampleTypeId=2, SampleQuantity=15, SampleType=SUBTRACT), Sample(SampleId=10, SampleTypeId=2, SampleQuantity=35, SampleType=SUBTRACT) 我目前正在使用这个: sampleList.stream() .collect(Collectors.groupingBy(Sample::getTypeId, Collectors.summingInt( sample -> SampleType.ADD.equalsIgnoreCase(sample.getSampleType()) ? […]