Java Questions 110 -120  «Prev  Next»


HashSet<E> (Java Questions)

Class HashSet<E>
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractSet<E>
java.util.HashSet<E>
Type Parameters:
E - the type of elements maintained by this set
All Implemented Interfaces:
Serializable, Cloneable, Iterable<E>, Collection<E>, Set<E>
Direct Known Subclasses:
JobStateReasons, LinkedHashSet
  1. What is a hashset?

    Answer:
    A hashset is an unordered and unsorted set.

  2. What is a LinkedHashSet?

    Answer:
    A LinkedHashSet is an ordered but not sorted set that maintains the order in which the objects were inserted.

  3. In what order are the elements of a hashtable placed?

    Answer:
    In a hashtable the elements are placed in a chaotic order based on the hashcode of the key.

  4. What does it mean when a collection is ordered?

    Answer:
    When a collection is ordered, it means you can iterate through the collection in a specific (not-random) order.

  5. How does an ArrayList keep order?

    Answer:
    An ArrayList keeps the order established by the element's index position (just like an array). An ArrayList maintains order by means of an index position.

  6. How does the LinkedHashSet keep order?

    Answer:
    The LinkedHashSet keeps the order established by insertion.

  7. How do you insert an element at a specific index position in an ArrayList ?

    Answer:
    To insert an element in ArrayList at a specific position, use ArrayList.add(index, element) function where index (= i-1) specifies ith position and the element is the one that is inserted.
    When the element is inserted, the elements from i-th position are shifted right side by a position.

  8. How do you describe collections that maintain the natural order of elements?

    Answer:
    One says that these collections are ordered and sorted.

  9. What is a sorted collection?

    Answer:
    A sorted collection means that the order in the collection is determined according to some rule or rules, known as the sort order.

  10. What is sorting based on ?

    Answer:
    Sorting is done based on properties of the object's themselves.