Java Questions 111 -120  «Prev  Next»


Java SE 25: Collection Interfaces and Hashing

Distinguish Collection, Collections, Map, and sequenced interfaces, and review transient fields, basic operations, and sorting in Java SE 25.

  1. Can a transient field participate in hashCode()?

    Answer: The transient modifier controls default Java object serialization; it does not forbid use in equals or hashCode. Choose equality-relevant state first. An unrelated transient counter must not make two equal objects produce different hashes.

    A transient cache of a hash computed from immutable state can be valid if it is recomputed correctly. If transient state actually determines equality, consider how deserialization restores that meaning. A transient field is omitted by default serialization, rather than being a value that can never be written by custom serialization code.

  2. What are two basic collection operations?

    Answer: Adding and removing elements. Collection defines add(E) and remove(Object), but mutation is an optional operation: an unmodifiable collection can reject it with UnsupportedOperationException. A Set may reject an equal duplicate by returning false rather than growing.

    For a List<Integer>, remove(1) removes index 1, while remove(Integer.valueOf(1)) removes a matching value. The selected overload matters.

  3. Which core interfaces should you understand for Java SE 25 collection interviews?

    Answer: Start with Collection, List, Set, Queue, Deque, and Map; then learn SortedSet, NavigableSet, SortedMap, and NavigableMap. Include SequencedCollection, SequencedSet, and SequencedMap, introduced in Java 21 for defined encounter order, end access, and reversed views.

    Iterator supports traversal, Comparable defines natural ordering, and Comparator supplies a separate ordering. Functional interfaces such as Predicate and Function support collection and stream operations. Collections and CompletableFuture are classes, not interfaces. This is a language/API study guide, not a claim about the objectives of a particular certification exam.

  4. Which two utility classes support collections and arrays?

    Answer: java.util.Collections supplies static helpers for collection objects, while java.util.Arrays supplies helpers for arrays. Examples include sorting, searching, and wrappers or conversions. They are classes in java.util, not two special categories of Java types.

    Some operations also exist directly on interfaces: List.sort sorts a modifiable list, and Collection.removeIf removes elements matching a predicate when removal is supported.

  5. Is every Collections Framework object a Collection?

    Answer: No. Maps belong to the framework but Map does not extend Collection. A map associates keys with values; its keySet(), values(), and entrySet() methods expose collection views. Those views are generally backed by the map, so supported removals through them affect its mappings.

  6. What common interface underlies the standard map types?

    Answer: Map is their common map interface. In Java SE 25, SortedMap extends SequencedMap, which extends Map; NavigableMap extends SortedMap. TreeMap implements NavigableMap. HashMap and Hashtable implement Map, while LinkedHashMap extends HashMap and implements SequencedMap.

    The distinction between direct and indirect inheritance matters. Saying that SortedMap directly extends Map omits the sequenced interface introduced in Java 21. Map remains separate from Collection.

  7. How do collection, Collection, and Collections differ?

    Answer: Lowercase "collection" is an informal term for a group or data structure. Capitalized Collection is the element-container interface. Plural Collections is the static utility class. A Map is a collection in broad everyday language but is not assignable to Collection.

    import java.util.Collection;
    import java.util.Collections;
    import java.util.LinkedHashMap;
    import java.util.Map;
    
    public class FrameworkViews {
        public static void main(String[] args) {
            Map<String, Integer> counts = new LinkedHashMap<>();
            counts.put("red", 2);
            counts.put("blue", 5);
            Collection<Integer> values = counts.values();
            System.out.println(Collections.max(values));
            values.remove(2);
            System.out.println(counts.containsKey("red"));
            System.out.println(counts);
        }
    }
    
  8. Is Collections a class or an interface?

    Answer: Collections is a class of static utility methods; you do not instantiate it to store elements. Collection is an interface with operations such as size, contains, iterator, add, and remove. Concrete implementations determine ordering, allowed elements, mutability, and performance.

  9. What are four useful families in the Collections Framework?

    Answer: Lists store positional sequences, sets enforce uniqueness, queues organize elements for processing, and maps associate unique keys with values. These are useful categories rather than four direct subinterfaces of Collection: Map is separate, and the families overlap through interfaces such as Deque.

    Choose a List for indexed positions, a Set for membership, a Queue or Deque for processing at its ends, and a Map for lookup by key. Then select an implementation with the ordering and concurrency behavior the application needs.

  10. How should you think about sorting a collection?

    Answer: Sorting applies a comparison rule to put elements in order. It is different from simply having an encounter order. A List can be sorted and later modified into an unsorted sequence; a TreeSet maintains its comparison order as elements are added.

    Use a consistent Comparable implementation or Comparator. Avoid changing the comparison fields of elements already in a sorted set or keys already in a sorted map, because the structure will not automatically reposition them.

Java SE 25 references: Collection, Collections, SequencedCollection, SortedMap, Map, Serializable.

SEMrush Software