Java Questions 110 -120  «Prev  Next»


Map versus HashMap (Java Questions)

  1. 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 .
  2. 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.
  3. Which collection would you use when you need to search for the objects based on a serial number?
    Answer:
    You should use a Map.
  4. What is the natural order for a String?
    Answer:
    The natural order for a String will be standard alphabetical sort.
  5. Which “Concrete Implementation Class” would you use if you had to keep track of which part was last accessed?
    Answer:
    LinkedHashMap.
  6. What are the advantages of the ArrayList over the array?
    Answer:
    1. An ArrayList can grow dynamically
    2. It provides more powerful insertion and search mechanisms than arrays.

  7. What is the correct way to declare the Collection’s type?
    Answer:
    List <String> myList = new ArrayList <String>();
  8. What is the difference between Java 5 and pre-Java 5 collections?
    Answer:
    Pre-Java 5 collections were untyped.
  9. 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.
  10. 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.