Java collections store and organise groups of objects. List, Set and Map are the three you will use most often, and interviewers ask about them in almost every fresher interview.
List
A List keeps items in order and allows duplicates. ArrayList is the most common choice, and it is fast for reading items by position.
Set
A Set does not allow duplicates. HashSet is fast but does not keep order. TreeSet keeps items sorted.
Map
A Map stores key and value pairs, such as an employee ID and the employee. HashMap is fast for lookups. TreeMap keeps keys sorted.
How to choose
- Need order and duplicates? Use a List.
- Need unique items? Use a Set.
- Need to look up a value by a key? Use a Map.
Next step: see our Java Full Stack course at ScholarLogic, or book a free demo class. A counsellor will share the fee and the next batch dates.
