Junior — Middle
What types of collections and their implementations have you used in Java for data storage?
sobes.tech AI
Answer from AI
In Java, I used various collection types from the java.util package for storing and processing data:
- List (e.g., ArrayList, LinkedList) — ordered collections that allow duplicates. ArrayList is good for fast access by index, LinkedList for frequent insertions and deletions.
- Set (HashSet, TreeSet) — collections without duplicates. HashSet provides quick access, TreeSet stores elements in sorted order.
- Map (HashMap, TreeMap) — collections of key-value pairs. HashMap provides fast access by key, TreeMap sorts by keys.
Example of using ArrayList and HashMap:
List<String> list = new ArrayList<>();
list.add("apple");
list.add("banana");
Map<String, Integer> map = new HashMap<>();
map.put("apple", 3);
map.put("banana", 5);