Tag: java stream

并行消息使用Java8流API从令牌分隔的输入流解组

是否有可能使用Java8流API创建接口,如Message pullNext()在分隔的输入流之上检测,逻辑步骤如下所示? 从字符输入流中读取分隔的标记 用于并行unmarshaller的令牌(一个令牌 – >一个消息) 邮件将重新排序以保留原始传入订单 消息可以使用前面提到的pullNext() 有点破坏者和unmarshal阶段由并发池服务。 与此类似,也许(在InputStream之上实现stash是要解决的问题): Iterable stash = Iterables.cycle(“one”, “two”, “three”); Iterator sink = StreamSupport.stream(stash.spliterator(), true). .parallel().map((x)->x+”-unmarshalled”).iterator(); while (sink.hasNext()) process(sink.next()); // do something with decoded message

如何使用公共密钥从地图的内部地图获取所有值?

我有一张地图地图: HashMap<String, Map> 。 我只需要使用DistinctCode从内部地图中提取String值。 我怎么能在一行或一个声明中这样做? 换句话说,我需要一个像这样的方法: mapOfMap.find(distinctcode) 它是否可以在一行或声明中使用?

如何使用java并行流而不是executorThreadsPool?

我想编写一个测试,对我的API执行许多并行调用。 ExecutorService executor = Executors.newCachedThreadPool(); final int numOfUsers = 10; for (int i = 0; i { final Device device1 = getFirstDevice(); final ResponseDto responseDto = devicesServiceLocal.acquireDevice(device1.uuid, 4738); if (responseDto.status == Status.SUCCESS) { successCount.incrementAndGet(); } }); } 我知道我可以使用executorThreadsPool来做到这一点,如下所示: devicesList.parallelStream() .map(device -> do something) 我可以用java8并行流创建它: 我怎么能在一台设备上做到这一点? 这意味着我很少想要获得相同的设备。 像这样的东西: {{device}}.parallelStream().execute(myAction).times(10)

要映射的Java流

我遇到Map<Integer, List> personenPerLeeftijd ,编译器说方法Persoon :: getLeeftijd无法解析。 我真的不知道我还能做什么,抱歉荷兰语! 我还需要更多信息请问 public class TestPersoon2 { public static void main(String[] args) { final List personen = Personen.getPersonen(); Map map = personen.stream().collect(Collectors.toMap(p -> p.getNaam() + “-” + p.getLeeftijd(), p -> p)); for (String s : map.keySet()) { System.out.printf(“%-15s -> %s\n”, s, map.get(s)); } Map<Integer, List> personenPerLeeftijd = personen.stream().collect(Collectors.groupingBy(Persoon::getLeeftijd)); //THIS METHOD […]

Java8流操作是否缓存?

我在运行Intel(R) Xeon(R) CPU E5-2680 0 @ 2.70GHz (2 CPUs), ~2.7GHz PC上运行以下示例代码 String format = “%7s run taken %6d micro seconds %5d findAny”; // First run long start = System.nanoTime(); int rand = IntStream.range(0, 100000).parallel().findAny().getAsInt(); long end = System.nanoTime(); System.out.println(String.format(format, “First”, ((end – start) / 1000), rand)); // Subsequent runs for (int i = 0; i […]

Java 8 Stream – 查找最大的嵌套列表

我有一个Collection<List> values 如何使用Streams找到包含最大列表的集合? 我尝试过类似的东西,但它并不常用 values.stream().max(e -> e.stream().max(List::size).get()).get() 但是我得到了编译错误。 有任何想法吗?

高级java 8流使用问题

我试图使用java 8流,以便在Message和Member列表上执行操作。 Message是我的域模型中的实体。 Message具有sender字段和Member类型的receiver字段。 Member是我的域模型中的第二个实体。 Member具有已发送消息的集合和已接收消息的集合。 成员JPA实体 : @Entity public class Member implements UserDetails { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; … @OneToMany(fetch = FetchType.LAZY, mappedBy = “sender”) private Collection sentMessages; @OneToMany(fetch = FetchType.LAZY, mappedBy = “recipient”) private Collection receivedMessages; @Version private Integer version; 消息JPA实体 : @Entity […]

Java Stream:forEach和forEachOrdered之间的区别

前提 :我已经阅读过这个问题和其他问题 ,但我需要一些澄清。 我知道Stream.forEach方法在处理并行流时会产生差异(不仅仅是这个),这就解释了为什么会这样 //1 Stream.of(“one “,”two “,”three “,”four “,”five “,”six “) .parallel() .forEachOrdered(item -> System.out.print(item)); 版画 one two three four five six 但是当涉及到中间操作时,当并行化流时,不再保证顺序。 所以这段代码 //2 Stream.of(“one “,”two “,”three “,”four “,”five “,”six “) .parallel() .peek(item -> System.out.print(item)) .forEachOrdered(item -> System.out.print(“”)); 会印出类似的东西 four six five one three two 说forEachOrdered方法只影响其自身执行中元素的顺序是否正确? 直观地说,我想//1例子与…完全相同 //3 Stream.of(“one “,”two “,”three “,”four “,”five […]

Java 8流filter – 基于排序的pdate

我试图在filter中对字段进行排序。 输入文件/样本记录: DocumentList: [ Document{ { _id=5975ff00a213745b5e1a8ed9, u_id=, mailboxcontent_id=5975ff00a213745b5e1a8ed8, idmapping=Document{ {ptype=PDF, cid=00988, normalizedcid=00988, systeminstanceid=, sourceschemaname=, pid=0244810006} }, batchid=null, pdate=Tue Jul 11 17:52:25 IST 2017, locale=en_US } }, Document{ { _id=597608aba213742554f537a6, u_id=, mailboxcontent_id=597608aba213742554f537a3, idmapping=Document{ {platformtype=PDF, cid=00999, normalizedcid=00999, systeminstanceid=, sourceschemaname=, pid=0244810006} }, batchid=null, pdate=Fri Jul 28 01:26:22 IST 2017, locale=en_US } } ] 在这里,我需要基于pdate进行排序。 List outList = […]

基于条件设置对象值并使用java 8流返回布尔值

我有嵌套列表,如果条件为真,我可以设置isMatched和department.setMatchedStatus(true)。 boolean isMatched = false; for (Employee employee: company.getEmployees()) { for (Department department: employee.getDepartments()) { if(departmentList.contains(department.getDepartmentName())){ isMatched = true; department.setMatchedStatus(true); } } } return isMatched; 想使用java 8流实现相同,我尝试使用下面的代码,但无法返回布尔值。 isMatched = company.getEmployees().stream() .flatMap(employee-> employee.getDepartments().stream()) .filter((department) -> departmentList.contains(department.getDepartmentName())) .forEach((department) -> department.setMatchedStatus(true)); 有人可以帮我吗?