Java Questions 111 -120  «Prev  Next»


Java SE 25: HashSet, LinkedHashSet, and Ordering

Compare unspecified, insertion, positional, and sorted order, including Java SE 25 sequenced operations on LinkedHashSet and lists.

  1. What is a HashSet?

    Answer: HashSet is a hash-table-backed Set. It stores unique elements under the equality contract, permits a null element, and provides no iteration-order guarantee. With suitable hash distribution, add, contains, and remove have expected constant-time behavior. It is not synchronized.

  2. What is a LinkedHashSet?

    Answer: It combines hash-based membership with a defined encounter order, normally insertion order. In Java SE 25 it implements SequencedSet, supporting first/last operations and a reversed view. The ordering links require additional storage compared with a plain HashSet.

  3. In what order does a hash table expose its entries?

    Answer: HashMap and Hashtable do not promise insertion or sorted iteration order. Their internal arrangement depends on hashing and implementation details and may change. "Unspecified" does not mean the implementation deliberately randomizes every traversal. Use an implementation with a documented ordering contract when order matters.

  4. What does it mean for a collection to be ordered?

    Answer: It has a defined encounter sequence. That sequence could come from list positions, insertion order, access order, or comparisons. Ordered does not automatically mean sorted. SequencedCollection gives a common API for collections with defined encounter order; not every Collection implements it.

  5. How does ArrayList maintain order?

    Answer: It stores a sequence indexed from zero. Appending adds at the end, indexed insertion shifts later elements right, and indexed removal shifts them left. Resizing its backing array preserves the sequence. Sorting or other list operations can intentionally change that sequence, so list order is not an unchangeable insertion history.

  6. Does adding an existing element change LinkedHashSet order?

    Answer: Ordinary add leaves the encounter position unchanged when an equal element is already present. Explicit addFirst and addLast can reposition an existing element. Removing and subsequently adding it also changes its position.

    import java.util.LinkedHashSet;
    import java.util.List;
    
    public class EncounterOrder {
        public static void main(String[] args) {
            LinkedHashSet<String> items = new LinkedHashSet<>(List.of("B", "A"));
            System.out.println(items.add("B"));
            System.out.println(items);
            items.addFirst("A");
            System.out.println(items);
            System.out.println(items.reversed());
        }
    }
    
  7. How do you insert at a particular ArrayList index?

    Answer: Call list.add(index, element). The index must be between zero and size, inclusive. Insertion at size appends; insertion at zero prepends. Existing elements at and after the position shift right. Use set to replace an existing element without increasing the list size.

  8. What describes a collection that maintains natural order?

    Answer: It is sorted by its elements' natural ordering. TreeSet without an explicit comparator uses Comparable, and TreeMap uses the natural ordering of its keys. Natural order is type-defined, not necessarily alphabetical or numeric in every class. A custom comparator can define a different sorted order.

  9. How is a sorted collection different from a sorted list?

    Answer: A sorted set or map maintains its comparison order as entries change through supported operations. A list sorted with List.sort is ordered at that moment but does not enforce sorted insertion later. TreeSet also removes comparison-equal duplicates, while sorting a list retains duplicates.

    PriorityQueue makes the least element under its ordering available at the head, but its iterator is not a sorted traversal. Do not classify every priority-based structure as having sorted iteration.

  10. What can a sorting rule be based on?

    Answer: It can compare values or selected properties, such as an employee name followed by an ID. Comparable places one natural ordering on a type; Comparator expresses other orderings. Use helpers such as comparing, comparingInt, and thenComparing instead of subtracting integers, which can overflow.

    Keep the rule stable during sorting and satisfy its comparison contract. If a comparator will be used in a TreeSet or TreeMap, review whether comparison equality agrees with equals.

Java SE 25 references: HashSet, LinkedHashSet, SequencedSet, ArrayList, TreeSet, Comparator.

SEMrush Software