Tag: java stream

Group,Sum byType然后使用Java流获得diff

我想使用流来分组,按类型获取总和,然后按类型查找不同的结果。 所以这是我的数据集。 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) 我目前的做法是这样的: Map result = sampleList.stream() .collect( Collectors.groupingBy( Sample::getSampleTypeId, Collectors.summingInt(Sample::getSampleQuantity) ) ); 我的结果是这样的(SampleTypeId,result): {1=75, […]

Java Streams – 如何在每第n个项目中执行中间函数

我正在寻找一个Stream上的操作,使我能够每隔n项执行一次非终端(和/或终端)操作。 虽然我使用素数流,例如,流可以很容易地生成网络请求,用户操作或其他一些冷数据或实时源。 由此: Duration start = Duration.ofNanos(System.nanoTime()); IntStream.iterate(2, n -> n + 1) .filter(Findprimes::isPrime) .limit(1_000_1000 * 10) .forEach(System.out::println); System.out.println(“Duration: ” + Duration.ofNanos(System.nanoTime()).minus(start)); 对于像这样的流函数: IntStream.iterate(2, n -> n + 1) .filter(Findprimes::isPrime) .limit(1_000_1000 * 10) .peekEvery(10, System.out::println) .forEach( it -> {});

列表包含来自另一个列表的至少一个值(Java 8)

我的函数从列表中的给定单词列表中查找一个单词在页面中是否有效。 因此,当页面包含单词时,我将其编写如下:(简化版) private boolean atLeastOneWordIsValidInThePage(Page page, Set wordIdsToCheck) Set wordIds = page.getWords().stream() .filter(word -> word.isValid()) .map(word->getWordId()) .collect(Collectors.toSet()); return words.stream().anyMatch((o) -> wordIds.contains(o)); 写它是最好的java 8练习吗? 我想在找到第一场比赛时停止搜索。

有没有办法强制parallelStream()并行?

如果输入大小太小,则库会自动序列化流中映射的执行 ,但此自动化不会并且不能考虑映射操作的重要程度。 有没有办法强制parallelStream()实际并行化CPU 重图?

Files.newDirectoryStream与Files.list

我知道Files.list(Path)在内部使用Files.newDirectoryStream(Path) ,基本上只包装DirectoryStream。 但是我不明白,当我想使用第一个或后一个时。 如果我想使用流API,这只是一种方便的方法吗? 我本可以做到这一点相当容易, 看到这个问题 。 如果查看Files.list的实现,内部DirectoryStream抛出的exception将包含在UncheckedIOException 。 我应该知道的任何事情吗?

Java 8 – Stream – 按值分组并查找该对象的最小值和最大值

对于我的例子,拥有汽车对象并发现基于模型的最小和最大价格值(分组依据)。 List carsDetails = UserDB.getCarsDetails(); Map collect4 = carsDetails.stream() .collect(Collectors.groupingBy(Car::getMake, Collectors.summarizingDouble(Car::getPrice))); collect4.entrySet().forEach(e->System.out.println(e.getKey()+” “+e.getValue().getMax()+” “+e.getValue().getMin())); output : Lexus 94837.79 17569.59 Subaru 96583.25 8498.41 Chevrolet 99892.59 6861.85 但我找不到哪个车对象有最高和最低价格。 我怎样才能做到这一点?

使用流有条件地填充地图 – Java 8

我正在尝试将此(简化)代码翻译为使用Java-8流: Map files = new ConcurrentHashMap(); while(((line = reader.readLine()) != null) { if(content != null) files.put(“not null”+line, “not null”+line); else files.put(“its null”+line, “its null”+line); } reader.close(); 这是我尝试过的: files = reader.lines().parallel().collect((content != null)? (Collectors.toConcurrentMap(line->”notnull”+line, line->line+”notnull”)) : (Collectors.toConcurrentMap(line->line+”null”, line->line+”null”))); 但是上面给出了关于intelliJ上所有line->line+”…”的“循环推理”消息。 什么是循环推理? 这个逻辑中有错误吗? 我在SO上注意到了一些类似的问题。 但是他们建议使用interface(Map)而不是它的实现。 但是这里的files被声明为Map 。 更新:添加更多上下文, content是一个包含目录名称的String。 files是一个包含多个文件路径的映射。 需要进入files映射的文件路径取决于content目录名是否已填充。

在一次通过中执行多次减少

在单个流传递中执行多个减少的习惯用法是什么? 是否只有一个大的减速器类,即使这违反了SRP,如果需要多种类型的减少计算?

在这种情况下,如何处理Function 和省略号/ varargs?

我的一个项目是扔lambda ; 在其中我的目标是简化Stream中潜在的@FunctionalInterface的使用,它在Stream使用的唯一“缺陷”是它们抛出已检查的exception(就我而言,我宁愿称有缺陷的事实是你不能从流中抛出已检查的exception但这是另一个故事)。 所以,对于Function我定义了这个: @FunctionalInterface public interface ThrowingFunction extends Function { R doApply(T t) throws Throwable; default R apply(T t) { try { return doApply(t); } catch (Error | RuntimeException e) { throw e; } catch (Throwable throwable) { throw new ThrownByLambdaException(throwable); } } } 这允许我定义,例如: final ThrowingFunction = Path::toRealPath; (为什么Path::toRealPath ……好吧, 正是因为它有一个省略号 )。 不想停在这里我希望能够写下这样的东西: […]

如何选择集合中最大的元素之一

我有一个元组列表,我想找到具有最大x值的元组。 在存在多个最大x值的情况下,我想随机选择一个。 我无法弄清楚如何实现这种随机选择function。 以下是我到目前为止的代码: public void testSelectRandomFromLargestVals() { List<Tuple> list = new ArrayList(); list.add(new Tuple(5, “five-1”)); list.add(new Tuple(2, “two”)); list.add(new Tuple(3, “three”)); list.add(new Tuple(5, “five-2”)); list.add(new Tuple(5, “five-3”)); Optional<Tuple> largestTuple = list.stream().max((t1, t2) -> Integer.compare(t1.x, t2.x)); System.out.println(“Largest tuple is: ” + largestTuple.get().x + ” value is: ” + largestTuple.get().y); } public class Tuple { public […]