Java Questions 110 -120  «Prev  Next»


Java Questions on LinkedList and Arrays

  1. What is the difference between a LinkedList and ArrayList in Java?

    Answer:
    In Java, the primary differences between a LinkedList and an ArrayList lie in their underlying data structures and performance characteristics.
    1. Data Structure:
      • ArrayList uses a dynamically resizable array to store its elements. This means that adding an element to an ArrayList might trigger a resizing of the underlying array if its capacity is exceeded, which can be a costly operation.
      • LinkedList uses a doubly-linked list to store its elements, where each element is a separate object with a data part and address part. This allows for more efficient insertion and deletion operations, as no shifting of elements is required.
    2. Performance:
      • ArrayList is faster for accessing elements by their index because it uses contiguous memory locations, allowing for direct access. However, adding or removing elements from the middle of an ArrayList can be slow, as it requires shifting all subsequent elements.
      • LinkedList is slower for accessing elements by their index because it has to traverse the list from the beginning or the end to find the specified index. However, it excels at adding or removing elements at any position, as it only needs to update the references between the elements.
    3. Memory Usage:
      • ArrayList uses less memory than LinkedList because it doesn't need to store the additional references for the next and previous elements.
      • LinkedList uses more memory due to the extra references for each element.
    4. Cache Efficiency:
      • ArrayList is more cache-friendly because of its contiguous memory locations, leading to better performance in scenarios where cache hits are frequent.
      • LinkedList has less cache efficiency due to its scattered memory locations.

    In summary, the choice between an ArrayList and a LinkedList depends on the specific requirements of the application, particularly the frequency of insertion, deletion, and random access operations. If you primarily need fast random access by index, use an ArrayList. If you need efficient insertion and deletion at any position, use a LinkedList.


  2. Which data structure is the LinkedList suited for?

    Answer:
    The linked list is suited for the Stack and Queue.

  3. Which are faster, arrays or Lists?

    Answer:
    Arrays are faster for all of these operations. The only time that linked lists are faster is when you need to delete or add an element.
    For a fixed collection of items, arrays are always faster.

  4. What are the characteristics of a HashSet in Java?

    Answer:
    Here's a breakdown of the key characteristics of a HashSet in Java:
    1. Unordered:
      • Elements in a HashSet are "not stored in any particular order". The order in which they are inserted is not maintained.
      • You can't retrieve them based on their original insertion order or index.
    2. Stores Unique Elements Only:
      • A HashSet cannot contain duplicate elements.
      • If you try to add an element that already exists, the old element will not be replaced.
    3. Implements the Set Interface:
      • HashSet inherits the properties of the `Set` interface. This interface mandates the uniqueness property, amongst others.
    4. Uses Hashing:
      • A HashSet uses a hash table data structure internally for efficient storage and retrieval.
      • Hashcode: Each element's `hashCode()` method is used to determine its storage location within the hash table. This enables fast lookups.
    5. Allows Null Values:
      • Unlike some other `Set` implementations, a HashSet can store a single `null` value.
    6. Non-Synchronized:
      • HashSet is not thread-safe. If multiple threads are concurrently modifying a HashSet, you need to introduce external synchronization (e.g., using `Collections.synchronizedSet()`).
    7. Performance:
      • HashSets generally offer excellent performance for adding, removing, and checking if an element exists (usually close to constant-time: O(1) ).
      • This performance comes from the hashing mechanism.

    Key Points:
    • Ideal for uniqueness: Use HashSets when you need a collection with no duplicates.
    • Fast lookups: Choose HashSets when you need to quickly determine if a particular element exists within a set.
    • Understand hashing: Having a basic understanding of hash codes and hash tables will help you use HashSets effectively.
    It is unsorted and unordered.


  5. When should you use the HashSet?

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

  6. What is the difference between a HashSet and LinkedHashSet?

    Answer:
    A LinkedHashSet maintains a doubly linked list across all elements.

  7. If you care about iteration order, which of the following 2 data structures should you choose?
    1. Hashset
    2. LinkedHashSet


    Answer:
    LinkedHashSet

  8. When using HashSet or LinkedHashSet, which method must be overridden for the objects being added?

    Answer:
    hashCode()

  9. What is one difference between TreeSet and TreeMap?

    Answer:
    TreeSet implements Navigable Set and TreeMap implements Navigable Map.

  10. Which method do Maps rely on to determine if two keys are equal?

    Answer:
    Maps rely on the equals() method to determine whether two keys are the same or different.