Java Questions 1 - 10   «Prev  Next»

Sorting Java Objects in Collections

  1. What is the natural order for sorting objects?

    Answer:
    For a collection of String objects, the natural order is alphabetical. For Integer objects, the natural order is by numeric value.

  2. In Java, how many implementations are there for the List interface?

    Answer:
    The number of implementations for the `List` interface in Java depends on whether you consider only those in the core Java libraries or include third-party libraries as well.
    Within the core Java libraries:
    There are two general-purpose implementations:
    • ArrayList: This is the most commonly used implementation due to its efficient random access and fast operations like adding/removing elements at the end.
    • LinkedList: This implementation offers better performance for frequent insertions/removals in the middle of the list but has slower random access compared to ArrayList.

    Additionally, you might consider two other classes that implement `List` but serve specific purposes:
    • Stack: This is a specialized LIFO (Last-In-First-Out) data structure built on top of the `List` interface.
    • Vector: This is a synchronized version of `ArrayList`, but its use is generally discouraged due to performance overhead and other reasons.

    Beyond the core libraries:
    Numerous third-party libraries offer various specialized `List` implementations with different characteristics and functionalities. Some popular examples include:
    1. Guava: Collections like `ImmutableList`, `ConcurrentLinkedHashMultimap`, and `CopyOnWriteArrayList` extend the functionality of the standard `List` implementations.
    2. Apache Commons Collections: Provides implementations like `FastList`, `BoundedBuffer`, and `CircularFifoBuffer` with specific performance or memory usage optimizations.
    3. Trove4j: Offers highly optimized primitive-type `List` implementations for performance-critical applications.

    Therefore, while there are only two general-purpose `List` implementations in the core Java libraries, the total number of `List` implementations available depends on the scope you consider and can be quite vast when including third-party libraries. It's crucial to choose the appropriate implementation based on your specific needs and usage patterns, considering factors like performance, thread safety, and desired functionalities.


  3. What is the difference between an ArrayList and a LinkedList?

    Answer:
    In a LinkedList the elements are doubly-linked to one another.

  4. What is characterisitc of the Set Interface?

    Answer:
    A set cares about uniqueness and does not allow duplicates.

  5. When should you use the HashSet class?

    Answer:
    Use this class when you want a collection with no duplicates and you do not care about order when you iterate through it.

  6. When should you use the LinkedHashSet?

    Answer:
    1. Use this class instead of HashSet when you care about the iteration order.
    2. A LinkedHashSet lets you iterate through the elements in the order in which they were inserted.

  7. What set does TreeSet implement?

    Answer:
    As of Java 6, TreeSet implements NavigableSet.


  8. Which method do Maps rely on to determine whether two keys are the same or different?

    Answer:
    equals()

  9. When should you use a HashMap?

    Answer:
    When you need a Map and do not care about the order (when you iterate through it), then you should select HashMap.

  10. What are the key differences between HashMap and Hashtable in Java SE 17?

    Answer:
    In Java SE 17, both `HashMap` and `Hashtable` are part of the Collections Framework and are used to store key-value pairs. Despite their similar purpose, there are significant differences in their design, functionality, and usage. These differences are crucial for developers to understand in order to choose the appropriate implementation for their specific needs.
    Synchronization
    • `Hashtable`: It is a legacy class from the original version of Java and is fully synchronized, meaning it is thread-safe out of the box. Each method call on a `Hashtable` is wrapped in a synchronized block, which ensures that only one thread can access the table at a time. This makes `Hashtable` suitable for use in multithreaded environments where multiple threads might access and modify the table concurrently. However, this synchronization comes at the cost of performance, making `Hashtable` less efficient in scenarios where thread safety is not a concern.
    • `HashMap`: Introduced in Java 2, version 1.2, `HashMap` is not synchronized, which means it does not provide thread safety by default. This lack of synchronization improves its performance in single-threaded or read-heavy environments. For multithreaded scenarios, external synchronization must be applied if atomic operations are required, or alternatively, one can use `Collections.synchronizedMap(new HashMap<...>())` to synchronize a `HashMap` or use `ConcurrentHashMap` for better concurrency.

    Null Keys and Values
    • `Hashtable`: It does not allow null keys or values. Attempting to insert a null key or value into a `Hashtable` will result in a `NullPointerException`.
    • `HashMap`: It allows one null key and multiple null values. This flexibility can be useful in applications where null has semantic meaning, such as representing the absence of a value.
    • Iteration Order: Both `Hashtable` and `HashMap` do not guarantee any specific order of the elements during iteration. The iteration order can change with the addition or removal of elements. However, for ordered or sorted iteration, one might consider using `LinkedHashMap` or `TreeMap`, respectively.
    • Performance: Due to the lack of synchronization in `HashMap`, it generally offers better performance in scenarios where thread safety is not a concern. The cost of synchronization in `Hashtable` can be significant in terms of performance, especially in highly concurrent scenarios where contention for the Hashtable might be high.
    • Legacy Status: `Hashtable` is considered a legacy class. Although it is still supported, it has been superseded by `ConcurrentHashMap` in Java 1.5 for highly concurrent scenarios and `HashMap` for non-threaded applications. The Java Collections Framework (JCF) does not include `Hashtable`, and it is recommended to use `HashMap` or `ConcurrentHashMap` in new development.

    In summary, the choice between `HashMap` and `Hashtable` in Java SE 17 should be guided by the specific requirements regarding thread safety, performance, and the need to support null keys and values. `HashMap` is generally preferred in most modern Java applications due to its performance advantages and flexibility, while `Hashtable` may still be used in legacy systems or specific scenarios where its thread-safe nature is required without the need to move to `ConcurrentHashMap`.
    Hashtable is the synchronized counterpart to HashMap.
    When we say Vector and Hashtable are synchronized, we mean that the key methods of the class are synchronized.
    A HashMap lets you have null values as well as one null key, a Hashtable does not allow anything that is null.

SEMrush Software