Sobes.tech
Junior

What data structures from collections do you know?

sobes.tech AI

Answer from AI

  • Interfaces:

    • Collection: The base interface for all collections.
    • List: An ordered collection of elements allowing duplicates.
    • Set: A collection of unique elements.
    • SortedSet: An ordered set.
    • NavigableSet: A SortedSet with navigation methods.
    • Queue: A collection of elements, typically supporting insertion at one end and removal from the other.
    • Deque: A double-ended queue.
    • Map: A key-value mapping. Keys are unique.
    • SortedMap: An ordered key-value mapping.
    • NavigableMap: A SortedMap with navigation methods.
  • Implementation classes:

    • List: ArrayList, LinkedList, Vector, Stack
    • Set: HashSet, LinkedHashSet, TreeSet
    • Queue: PriorityQueue, ArrayDeque
    • Map: HashMap, LinkedHashMap, TreeMap, Hashtable
  • Main characteristics:

Interface Ordered Duplicates Allowed
List Yes Yes
Set No No
SortedSet Yes No
Map No (by key) No (by key)
SortedMap Yes (by key) No (by key)
  • Usage examples:

    import java.util.ArrayList;
    import java.util.HashSet;
    import java.util.HashMap;
    
    public class CollectionExamples {
    
        public static void main(String[] args) {
            // ArrayList - ordered list allowing duplicates
            ArrayList<String> names = new ArrayList<>();
            names.add("Alice");
            names.add("Bob");
            names.add("Alice");
            System.out.println("ArrayList: " + names);
    
            // HashSet - set of unique elements without a specific order
            HashSet<Integer> numbers = new HashSet<>();
            numbers.add(1);
            numbers.add(2);
            numbers.add(1); // Duplicate will not be added
            System.out.println("HashSet: " + numbers);
    
            // HashMap - Map of key-value pairs without a specific order of keys
            HashMap<String, Integer> ages = new HashMap<>();
            ages.put("Alice", 30);
            ages.put("Bob", 25);
            ages.put("Charlie", 30);
            System.out.println("HashMap: " + ages);
        }
    }
    
What data structures from collections do you know… - sobes.tech