java-8

Stream ordered/unordered problems

我是研究僧i 提交于 2020-01-22 14:15:12
问题 I have the following code: Set<Integer> l = new TreeSet<>(); l.add(1); l.add(10); l.add(3); l.add(-3); l.add(-4); and I want to unorder the collection with: l.stream().unordered().forEach(System.out::println); but the forEach returns always the collection ordered! Then I have also another doubt about this sentence: For sequential streams, the presence or absence of an encounter order does not affect performance, only determinism. If a stream is ordered, repeated execution of identical stream

Converting string to date using java8

六月ゝ 毕业季﹏ 提交于 2020-01-22 14:07:06
问题 I am trying to convert a string to date using java 8 to a certain format. Below is my code. Even after mentioning the format pattern as MM/dd/yyyy the output I am receiving is yyyy/DD/MM format. Can somebody point out what I am doing wrong? String str = "01/01/2015"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy"); LocalDate dateTime = LocalDate.parse(str, formatter); System.out.println(dateTime); 回答1: That is because you are using the toString method which states that

Not implementing all the methods of an interface

不羁的心 提交于 2020-01-22 13:52:05
问题 I tried reproducing the code below on eclipse. I get an error telling me that I have to implement all the inherited methods (because Comparator is an interface). The type new Comparator(){} must implement the inherited abstract method Comparator.reversed() . There are many of these methods and the only one I want to overwrite is compare. Do I have to implement all the other methods or is there a way to specify I don't need to implement them? I understand that I will have to do it because of

Not implementing all the methods of an interface

一笑奈何 提交于 2020-01-22 13:51:07
问题 I tried reproducing the code below on eclipse. I get an error telling me that I have to implement all the inherited methods (because Comparator is an interface). The type new Comparator(){} must implement the inherited abstract method Comparator.reversed() . There are many of these methods and the only one I want to overwrite is compare. Do I have to implement all the other methods or is there a way to specify I don't need to implement them? I understand that I will have to do it because of

Value-based Classes confusion

南楼画角 提交于 2020-01-22 09:42:04
问题 I'm seeking some clarification to the definition of Value-based Classes. I can't imagine, how is the last bullet point (6) supposed to work together with the first one (1) they are final and immutable ( though may contain references to mutable objects ) (6) they are freely substitutable when equal, meaning that interchanging any two instances x and y that are equal according to equals() in any computation or method invocation should produce no visible change in behavior. Optional is such a

Java 8 Convert a simple List to Map

两盒软妹~` 提交于 2020-01-21 18:58:26
问题 import java.util.function.*; import java.util.*; public class Main { public static void main(String[] args) { List<Integer> newList = new ArrayList<Integer>(); newList.add(1); newList.add(2); newList.add(3); newList.add(4); Map<Integer,String> formMap = new LinkedHashMap<Integer,String>(); Function<Integer,Map<Integer,String>> myFunc = i->{ if(i%2==0) { formMap.put(i,"even"); } return formMap; }; Map<Integer,String> newMap = newList.stream().map(i->myFunc.apply(i)).collect(Collectors.toMap(

Java 8 Convert a simple List to Map

∥☆過路亽.° 提交于 2020-01-21 18:58:08
问题 import java.util.function.*; import java.util.*; public class Main { public static void main(String[] args) { List<Integer> newList = new ArrayList<Integer>(); newList.add(1); newList.add(2); newList.add(3); newList.add(4); Map<Integer,String> formMap = new LinkedHashMap<Integer,String>(); Function<Integer,Map<Integer,String>> myFunc = i->{ if(i%2==0) { formMap.put(i,"even"); } return formMap; }; Map<Integer,String> newMap = newList.stream().map(i->myFunc.apply(i)).collect(Collectors.toMap(

EclEmma, Java8 and Lambda - no coverage on lambda expression

江枫思渺然 提交于 2020-01-21 14:40:23
问题 I have a Java project under Eclipse Luna, with EclEmma 2.3.1.201405111647 (latest), which use Jacoco 0.7.1, which have support for Java 8 as stated in their changelog: "Version 2.3.1 (2014/05/11) Fixed ASM 5.0.1 dependency conflicts with new ASM bundles in Eclipse 4.4 (GitHub #83). Upgrade to JaCoCo 0.7.1 for full Java 8 support. I now have the following toString: @Override public String toString() { // [BLOCK0] if (0 == value) { return "0B"; } // [BLOCK1] final MutableLong val = new

Java 8 LocalDateTime has different results

谁说我不能喝 提交于 2020-01-21 10:34:53
问题 I am trying to update some code to use Java 8's feature for parsing multiple date formats. my local time on my box is set to UTC-11 . the below code works when using the SimpleDateformat. DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); Date correctDate = dateFormat.parse("2018-09-6T03:28:59.039-04:00"); //Gives me correct date System.println( correctDate);//Wed Sep 5th 20:28:59 GMT-11:00 2018 I am trying to update this code to give the same date as above with the

Java 8 LocalDateTime has different results

一个人想着一个人 提交于 2020-01-21 10:33:03
问题 I am trying to update some code to use Java 8's feature for parsing multiple date formats. my local time on my box is set to UTC-11 . the below code works when using the SimpleDateformat. DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); Date correctDate = dateFormat.parse("2018-09-6T03:28:59.039-04:00"); //Gives me correct date System.println( correctDate);//Wed Sep 5th 20:28:59 GMT-11:00 2018 I am trying to update this code to give the same date as above with the