Java Questions 2
- 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.
- What are all 3 list implementations ordered by?
Answer:
All 3 list implementations are ordered by index position, a position that you determine either by setting an object at a specific index or by adding it without specifying position,
in which case the object is added to the end.
- What is the difference between an ArrayList and a LinkedList?
Answer:
In a LinkedList the elements are doubly-linked to one another.
- What is characterisitc of the Set Interface?
Answer:
A set cares about uniqueness and does not allow duplicates.
- 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.
- When should you use the LinkedHashSet?
Answer:
- Use this class instead of HashSet when you care about the iteration order.
- A LinkedHashSet lets you iterate through the elements in the order in which they were inserted.
- What set does TreeSet implement?
Answer:
As of Java 6, TreeSet implements NavigableSet.
- Which method do Maps rely on to determine whether two keys are the same or different?
Answer:
equals()
- 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.
- What are the key differences between HashMap and Hashtable?
Answer:
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.