Using Streams and Set Collection Factory methods with Java makes code easier to read and maintain.
By using these features, we can also make it more difficult for bugs to remain hidden. If you can use the latest LTS (Long-term support) Java version in your project, it’s the best scenario, so that you can use cool Java features such as Collection Factories from Java 9 onwards.
Now that we know why it’s important to understand Java features well, it’s time for the Java Challenge!
It's time to improve your Java skills with this Stream Set Distinct Challenge...
Stream Set Distinct Challenge
Can you guess what will happen when running the following Java code?
import java.util.Set; public class StreamDistinctChallenge { public static void main(String... doYourBest) { Set<Warrior> warriors = Set.of(new Warrior("Ezio"), new Warrior("Ezio"), new Warrior("Kratos"), new Warrior("Cloud"), new Warrior("Alucard")); warriors.stream() .distinct() .forEach(w -> System.out.println(w.name)); } static class Warrior { private String name; Warrior(String name) { this.name = name; } public int hashCode() { return this.name.length(); } public boolean equals(Object obj) { return this.name.equals(((Warrior) obj).name); } } }
A) Cloud Ezio Alucard Kratos
B) IllegalArgumentException will be thrown
C) Ezio Ezio Alucard Kratos Cloud
D) NullPointerException will be thrown
Explanation about this Java Challenge:
The catch of this quiz is the Set.of factory method behavior. Note that we are passing "Ezio" twice, so that will be equal and will have the same hashcode number.
Set.of(new Warrior("Ezio"), new Warrior("Ezio"), new Warrior("Kratos"), new Warrior("Cloud"), new Warrior("Alucard"));
The problem is that when we use two objects that are equal, the Set.of method will do something specific, because of the duplicated elements, one of the answers above. 🙂
Pay attention also that if we were using the List.of factory method, this would not happen.
If you want to watch the video explanation, check it out, but I recommend trying out the Java Challenge first:
That’s it challenger, rock on! Keep taking action and relentlessly break your limits!
Don’t hesitate to leave a comment with a question if anything is not clear!
To check the original Java Challengers post, access the following link:
https://javachallengers.com/streams-set-distinct-java-challenge-quiz/