Tag: java 8

Java 8不兼容的类型

这是简单的代码 import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.Map; public class SimpleTest { public static void main(String[] args) { final ArrayList<Map> maps = newArrayList( createMap(“1”, “a”, Collections.EMPTY_MAP, Collections.EMPTY_MAP), createMap(“2”, “b”, Collections.EMPTY_MAP, Collections.EMPTY_MAP), createMap(“3”, “c”, Collections.EMPTY_MAP, Collections.EMPTY_MAP) ); System.out.println(” maps = ” + maps); } public static Map createMap(String value1, String value2, Map object1, Map object2) […]

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.time中将LocalDateTime的精度设置为纳秒?

根据java.time文档 ,java.time应该能够以纳秒精度呈现LocalDateTime或LocalTime,但是当我运行LocalDateTime.now()并打印出来时,它只显示3位而不是9位。 喜欢这个: 2016-08-11T22:17:35.031 有没有办法获得更高的精度?

Lambda表达式:引用特定对象的实例方法

此代码使用方法引用特定对象的实例方法: public class Main { public static void main(String[] args) { One one=new One(); // F f = ()->{one.bar();}; //previous wrong syntax F f = one::bar; //4 f.foo(); } } class One{void bar(){}} interface F{void foo();} 我知道它有效。 但我无法理解为什么以及如何 。 我无法理解的是F.foo()方法如何使用对不是方法本身的参数的对象的引用(签名不是void foo(One one) )。 我在第4行 创建实现F接口的类的实例 通过使用引用来调用bar()方法来实现该方法 但是foo()如何在one引用上有一个范围? 我是否错误地试图将此解决方案转换为“传统的,明确的实现”? 如果不是,“明确的对应物”是什么?

基于条件设置对象值并使用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)); 有人可以帮我吗?

Lambda填充地图

我试图用一个单词和它们的出现次数来填充地图。 我正在尝试写一个lambda来做它,就像这样: Consumer wordCount = word -> map.computeIfAbsent(word, (w) -> (new Integer(1) + 1).intValue()); map是Map 。 如果它不存在,它应该只是将地图中的单词作为键插入,如果它存在,它应该将其整数值增加1.这个语法不正确。

将hashmap拆分为java 8中的分区

我有hashmap: Map<String, Set> myMap 我想将它拆分为包含Map列表: List<Map<String,Set>> listofMaps; ,每张地图最多100个键。 我知道如何以常规的方式做到这一点..(关于入口集的foreach,每100个项目创建新的地图)。 有没有选择用java 8 lambda做什么? (像Lists.partitions() ..)?

CompletableFuture | thenApplyAsync vs thenCompose及其用例

我试图理解CompletableFuture,并遇到了2个方法,然后是ApplyAsync然后是Make。 我试图了解这两者之间的区别。 CompletableFuture completableFuture = CompletableFuture.supplyAsync(() -> { System.out.println(Thread.currentThread().getName() + ” Printing hello”); return “Hello”; }).thenCompose((String s) -> { return CompletableFuture.supplyAsync(() -> { System.out.println(Thread.currentThread().getName() + ” Adding abc”); return “abc “+s;}); }).thenApplyAsync((String s) -> { System.out.println(Thread.currentThread().getName() + ” Adding world”); return s + ” World”; }).thenApplyAsync((String s) -> { System.out.println(Thread.currentThread().getName() + ” Adding name”); if […]

如何在O(N)时间和O(C)空间复杂度中使用Java 8流API从列表中仅删除一个max(min)

这是一个代码,用于从列表中仅删除其中一个最大值(在这种情况下是第一个,但这是无关紧要的)。 时间为O(n) ,空间为O(n) (超出输入)。 public List removeOneOfTheMax(List nums) { int max = Integer.MIN_VALUE; int maxIndex = -1; Iterator it = nums.iterator(); for (int i = 0; it.hasNext(); i++) { Integer temp = it.next(); if (max < temp) { maxIndex = i; max = temp; } } nums.remove(maxIndex); return nums; } 1.与使用Java 8流API的方法相同的是什么? 我想保留时间和空间的复杂性,因此不允许排序。 2.实际上,如果你将LinkedList传递给上面的代码,空间复杂度将是O(C) (再次超出输入),但据我所知, […]

如何使用Java 8 DateTime API从序列值本地日期时间获取POSIX时间(UTC)

我有一个类似于POSIX Time的时间戳,唯一的例外是它不是以UTC计算的。 相反,它是自1970年1月1日午夜以来在特定当地时区内经过的毫秒数。 为了将此值变为Instant ,我必须首先知道其对UTC / GMT的偏移(以毫秒为单位)。 所以问题是:知道本地时区id,例如。 “美国/芝加哥”和自本地大纪元以来的毫秒数,我如何制作一个瞬间(必须用POSIX纪元以来的毫秒构建)? 似乎任何java.time API构造函数都不接受本地Epoch中的毫秒参数。 我有一个解决方案,我首先将本地毫秒日期时间转换为本地格里高利历日期时间(然后我可以从中构建一个LocalDateTime并获得UTC的偏移量),但这看起来好像很多看起来应该很简单。