java-8

Does BigDecimal#min method qualify as a BinaryOperator?

ぃ、小莉子 提交于 2021-02-18 20:11:43
问题 The Stream.reduce method takes a BinaryOperator as an argument. The function signature of a BinaryOperator is (T,T) -> T . The BigDecimal::min method has only 1 parameter in its method signature (ie. (T) -> T ). Why doesn't the compiler complain when I pass BigDecimal::min to the Stream.reduce method? Sample code: List<BigDecimal> bigDecimalList = new ArrayList<>(); bigDecimalList.add(BigDecimal.valueOf(1)); bigDecimalList.add(BigDecimal.valueOf(2)); bigDecimalList.add(BigDecimal.valueOf(3));

Intermediate operation in Java streams [duplicate]

心不动则不痛 提交于 2021-02-18 19:20:05
问题 This question already has answers here : Java 8 Streams peek api (4 answers) Closed 2 years ago . In java 8, I am using Streams to print the output, but size is coming as 0. Why? public class IntermediateryAndFinal { public static void main(String[] args) { Stream<String> stream = Stream.of("one", "two", "three", "four", "five"); Predicate<String> p1 = Predicate.isEqual("two"); Predicate<String> p2 = Predicate.isEqual("three"); List<String> list = new ArrayList<>(); stream.peek(System.out:

Highest ordinal enum value

懵懂的女人 提交于 2021-02-18 18:47:25
问题 I'm looking to compute the highest ordinal enum value from a list of enum properties in a list of beans. For example, I have: @Data public class MyBean { private Priority priority; } and public enum Priority { URGENT("Urgent"), HIGH("High"), MEDIUM("Medium"), LOW("Low"); @Getter private final String value; Priority(String value) { this.value = value; } @Override public String toString() { return getValue(); } } If I have a List of MyBeans , how can I find the max ordinal value of the bean's

Highest ordinal enum value

≡放荡痞女 提交于 2021-02-18 18:46:39
问题 I'm looking to compute the highest ordinal enum value from a list of enum properties in a list of beans. For example, I have: @Data public class MyBean { private Priority priority; } and public enum Priority { URGENT("Urgent"), HIGH("High"), MEDIUM("Medium"), LOW("Low"); @Getter private final String value; Priority(String value) { this.value = value; } @Override public String toString() { return getValue(); } } If I have a List of MyBeans , how can I find the max ordinal value of the bean's

Test lambda expressions called by dependencies

夙愿已清 提交于 2021-02-18 11:47:31
问题 I am trying to test some code inside lambda expression which is a call back from another class. class EmailSender { private EmailBuilder emailBuilder; public void send() { String testEmail = emailBuilder.buildEmail("Test Email", bodyContentAppender()); //send testEmail } private Consumer<Email> bodyContentAppender() { //how to test this through JUnit? return email -> email.appendBody("Body Content"); } } interface EmailBuilder { String buildEmail(String templateName, Consumer<Email>

How should javac 11 link methods overridden in later versions with a Java 8 target?

被刻印的时光 ゝ 提交于 2021-02-18 11:36:33
问题 Let's say I'm using Java 11 javac, but I'm using the --source and --target options set to 1.8 so that my source code will be considered Java 8 and the output .class files will be compatible with Java 8. My goal is to produce .class files that can run on a Java 8 JVM. And let's say I have the following Java 8 code I'm compiling: import java.nio.ByteBuffer; … ByteBuffer byteBuffer = …; //init somehow byteBuffer.flip(); //what ends up in the `.class` file? The question is: what should Java 11

Generic type parameters inference in method chaining

痞子三分冷 提交于 2021-02-18 10:36:45
问题 After reading this question, I've started to think about generic methods in Java 8 . Specifically, what happens with generic type parameters when methods are chained. For this question, I will use some generic methods from Guava's ImmutableMap , but my question is more general and can be applied to all chained generic methods. Consider ImmutableMap.of generic method, which has this signature: public static <K, V> ImmutableMap<K, V> of(K k1, V v1) If we use this generic method to declare a Map

Types in a LambdaMetaFactory

女生的网名这么多〃 提交于 2021-02-18 10:28:11
问题 I get an exception when I call metafactory . It says: java.lang.invoke.LambdaConversionException: Incorrect number of parameters for instance method invokeVirtual my.ExecuteTest$AProcess.step_1:()Boolean; 0 captured parameters, 0 functional interface method parameters, 0 implementation parameters I do not understand all from the documentation of LambdaMetafactory.metafactory . I have problems figuring out the correct parameters: MethodHandles.Lookup caller -- thats easy String invokedName --

How to groupBy object properties and map to another object using Java 8 Streams?

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-18 09:53:09
问题 Suppose I have a group of bumper cars, which have a size, a color and an identifier ("car code") on their sides. class BumperCar { int size; String color; String carCode; } Now I need to map the bumper cars to a List of DistGroup objects, which each contains the properties size , color and a List of car codes. class DistGroup { int size; Color color; List<String> carCodes; void addCarCodes(List<String> carCodes) { this.carCodes.addAll(carCodes); } } For example, [ BumperCar(size=3, color

Why is adding default methods to interfaces in Java 8 a good design choice and what are the alternatives [duplicate]

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-17 21:16:46
问题 This question already has answers here : Purpose of Default or Defender methods in Java 8 (5 answers) Closed 4 years ago . I am just learning Java, so it is hard for me to access the possible alternatives, and the impact of such a design decision. Java 8 adds the default methods feature to interfaces, which allows interfaces to have an implementation. This allows to extend the existing interfaces with new methods, without breaking the clients, evolving the interface over time in a backward