Map versus HashMap (Java Questions)
- Why is a Map not the class to choose when you need to implement a name/value pair collection?
Answer:
This is because Map is an interface and not a concrete implementation .
- What are the 3 interface types that have concrete Classes for the Collection Interface?
Answer:
The 3 concrete implementations are 1) Map 2) Set and 3) List.
- Which collection would you use when you need to search for the objects based on a serial number?
Answer:
You should use a Map.
- What is the natural order for a String?
Answer:
The natural order for a String will be standard alphabetical sort.
- Which âConcrete Implementation Classâ would you use if you had to keep track of which part was last accessed?
Answer:
LinkedHashMap.
- What are the advantages of the ArrayList over the array?
Answer:
- An ArrayList can grow dynamically
- It provides more powerful insertion and search mechanisms than arrays.
- What is the correct way to declare the Collectionâs type?
Answer:
List <String> myList = new ArrayList <String>();
- What is the difference between Java 5 and pre-Java 5 collections?
Answer:
Pre-Java 5 collections were untyped.
- How does Java 5 deal with primtive to Wrapper conversion?
Answer:
With Java 5, primitives still have to be wrapped before they are put into a Collection, but autoboxing takes of it for you.
- What do collections and arrays have in common when it comes sorting and searching?
Answer:
Both collections and arrays can be sorted and searched using methods in the API.