Skip to main content
PreparationZone Logo
PrepZone

Pack 3: Collections, Streams & Optional

Nine puzzles — mutable keys, lazy streams, Optional traps, immutable lists, and map merge.

Pass checkpoint below

Pack 3 — collections, streams & Optional

Collections, streams & Optional

Mutability traps, laziness, and API foot-guns.

0/9 solved
  1. What is printed?

    Java
    import java.util.HashSet;
    import java.util.Set;
    
    class Key {
        String id;
    
        Key(String id) {
            this.id = id;
        }
    
        @Override
        public int hashCode() {
            return id.hashCode();
        }
    
        @Override
        public boolean equals(Object o) {
            return o instanceof Key k && id.equals(k.id);
        }
    }
    
    public class Main {
        public static void main(String[] args) {
            Set<Key> set = new HashSet<>();
            Key k = new Key("x");
            set.add(k);
            k.id = "y";
            System.out.println(set.contains(new Key("y")));
        }
    }
    Predict the output, then tap Run on the code block

The rest of this guide is part of Pro

Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.

Test yourself

Answer these before moving on — recall is what makes it stick.