草庐IT

stream_bufs

全部标签

java - 是否有更高效的 Java 8 Stream 方法来查找 int[] 中的索引?

基于BlackJackQuestion,我想知道如何指示所有获胜的手。实际上,最初的问题只是询问两个不大于21的数字中的最大值。所以像这样的方法publicintblackjack(inta,intb);但是,如果有人希望返回所有获胜的手(假设输入数组中的位置是table上的一个座位),那么签名如:/***returnsanarrayindicatetheindexinthespecifiedhandsthat*correspondtothewinninglocations.Willreturnanemptyarrayif*therearenowinners.Thelengthofth

java - 如何在 Java 8 Stream.flatMap(..) 中捕获异常

给定一个Stream和一个返回Stream作为数据源的不同参数的方法,我正在寻找一种通过flatMap合并流的方法(..)并在执行期间捕获某些Exceptions。让我们看下面的代码片段:publicclassFlatMap{publicstaticvoidmain(finalString[]args){longcount;//thismightthrowanexceptioncount=Stream.of(0.2,0.5,0.99).flatMap(chance->getGenerator(chance,20)).count();//tryingtocatchtheexception

java - 使用 Java8 stream api 将函数列表应用于值

我想要一个单条日志消息pojoLoggedExchange并对其应用一系列转换。转换是列表中的一元运算符:Listtransforms=newArrayList();哪里ConditionalTransform工具UnaryOperator我目前的解决方案是像这样使用reduce:publicLoggedExchangetransform(LoggedExchangeoriginal){returntransforms.stream().reduce(original,(o,t)->t.apply(o),(m1,m2)->m2);}并行运行它没有意义,因为无法合并两条消息((m1,m2

java - 如何使用 Java 8 Stream 扩展和重组列表列表?

我有一个A类列表,其中包括一个列表本身。publicclassA{publicdoubleval;publicStringid;publicListnames=newArrayList();publicA(doublev,StringID,Stringname){val=v;id=ID;names.add(name);}staticpublicListcreateAnExample(){Listitems=newArrayList();items.add(newA(8.0,"x1","y11"));items.add(newA(12.0,"x2","y21"));items.add(n

java - 如何将这个经典的 Java 代码重写为 Java Stream API 代码?

有一段旧的Java代码(没有lambda表达式):publicListgetAttackedCheckersForPoint(CheckerPositionfrom,booleanisSecondPlayerOwner,booleanisQueen,VectorDirectionignoredDirection){ListallDirections=VectorDirection.generateAllDirections();Listresult=newArrayList();for(VectorDirectiondirection:allDirections){if(!direct

java - 仅使用两个键和奇数或偶数列表索引作为值将 List 转换为 Map - Java 8 Stream

我想将列表转换为map,只使用两个字符串值作为键值。然后作为值只是包含来自输​​入列表的奇数或偶数索引位置的元素的字符串列表。这是旧时尚代码:Map>map=newHashMap();Listlist=Arrays.asList("one","two","three","four");map.put("evenIndex",newArrayList());map.put("oddIndex",newArrayList());for(inti=0;i如何使用流将此代码转换为Java8以获得此结果?{evenIndex=[one,three],oddIndex=[two,four]}我目前

java - 使用 Spring Cloud Stream 将 RabbitMQ 消费者绑定(bind)到现有队列

我使用RabbitMQ网络用户界面创建了一个主题交换TX并绑定(bind)到交换两个队列TX.Q1和TX.Q2,每个都与路由键rk1和rk2相应地绑定(bind),并向交换生成少量消息。现在我想使用SpringCloudStream创建一个消费者,它只会从Q1获取消息。我尝试使用配置:spring.cloud.stream.bindings.input.destination=TXspring.cloud.stream.bindings.input.group=Q1以及消费消息的方法的注解@StreamListner(Sink.INPUT)。结果我可以看到消费者创建了一个同名队列(或绑

java - 为什么不推荐基于 AtomicInteger 的 Stream 解决方案?

假设我有这份水果list:-Listf=Arrays.asList("Banana","Apple","Grape","Orange","Kiwi");我需要为每个水果添加一个序列号并打印出来。水果或序列号的顺序无关紧要。所以这是一个有效的输出:-4.Kiwi3.Orange1.Grape2.Apple5.Banana解决方案#1AtomicIntegernumber=newAtomicInteger(0);Stringresult=f.parallelStream().map(i->String.format("%d.%s",number.incrementAndGet(),i)).

java - 使用 Java Stream API 对数组进行部分降序排序

我需要知道如何使用StreamAPI按降序对原始唯一整数数组进行部分排序。比如有{1,2,3,4,5}这样的数组,我想得到{5,4,3,1,2}-首先是3个最大的元素,然后是其余的。甚至可以使用流吗?我检查了文档-有两种方法skip和limit但它们会更改流内容并从数组的开头开始工作。我可以像这样对整个数组进行排序Arrays.stream(arr).boxed().sorted(Collections.reverseOrder()).mapToInt(Integer::intValue).toArray();但是如何使这个排序部分化呢?我说StreamAPI是因为我希望它写得很好。而

java - 如何根据 Java 8 Stream 过滤器的输出计算百分比

我想获取一个作业列表(称为resultStream)并计算完全完成的作业的百分比。publicclassJob{privateDatedate;privateStringsuccess;//Getterandsetterandconstructor.}列表包含以下内容:newJob("TODAY","YES");newJob("TODAY","YES");newJob("YESTERDAY","YES");newJob("TODAY","NO");这是我目前的代码:resultStream.stream().parallel().filter(result->{if("YES".con