Java-Collections-经典20题

Collections:



1.Why Map interface doesn’t extend Collection interface?
  • Set is unordered collection and does not allows duplicate elements.
  • List is ordered collection allows duplicate elements.
  • Where as Map is key-value pair.
  • It is viewed as set of keys and collection of values.
  • Map is a collection of key value pairs so by design they separated from collection interface.


2.What is difference between HashMap and Hashtable?
  • Synchronization or Thread Safe
  • Null keys and null values
  • Iterating the values
  • Default Capacity


3.Differences between comparable and comparator?
  • Comparable Interface is actually from java.lang package.
  • It will have a method compareTo(Object obj)to sort objects
  • Comparator Interface is actually from java.util package.
  • It will have a method compare(Object obj1, Object obj2)to sort objects

Comparable
A comparable object is capable of comparing itself with another object. The class itself must implements the java.lang.Comparable interface in order to be able to compare its instances.

Comparator
A comparator object is capable of comparing two different objects. The class is not comparing its instances, but some other class’s instances. This comparator class must implement the java.util.Comparator interface.



4.How can we sort a list of Objects?
  • To sort the array of objects we will use  Arrays.sort() method.
  • If we need to sort collection of object we will use Collections.sort().


5.What is difference between fail-fast and fail-safe?
  • Fail fast is nothing but immediately report any failure. whenever a problem occurs fail fast system fails.
  • In java Fail fast iterator while iterating through collection of objects sometimes concurrent modification exception will come there are two reasons for this.
  • If one thread is iterating a collection and another thread trying to modify the collection.
  • And after remove() method call if we try to modify collection object


6. What is difference between Iterator ,ListIterator and Enumeration?
  • Enumeration interface implemented in java 1.2 version.So Enumeration is legacy interface.
  • Enumeration uses elements() method.
  • Iterator is implemented on all Java collection classes.
  • Iterator uses iterator() method.
  • Iterator can traverse in forward direction only.
  • ListIterator is implemented only for List type classes
  • ListIterator uses listIterator() method.


7.What is difference between Set and List in Java?
  • A set is a collection that allows unique elements.
  • Set having no index.
  • Set allows only one null value.
  • Set having classes like :
  •                                  HashSet
                                     LinkedHashMap
                                     TreeSet
  • List having index.
  • List allows n number of null values.
  • List will display Insertion order with index.
  • List having classes like :
  •                                  Vector
                                     ArrayList
                                     LinkedList


8.Differences between arraylist and vector?
  • Vector was introduced in  first version of java . that's the reason only vector is legacy class.
  • ArrayList was introduced in java version1.2, as part of java collections framework.
  • Vector is  synchronized.
  • ArrayList is not synchronized.


9.What are the classes implementing List interface?
  • ArrayList    
  • LinkedList    
  • Vector


10. Which all classes implement Set interface ?
  • HashSet
  • LinkedHashSet
  • TreeSet


11.How to make a collection thread safe?
  • Vector, Hashtable, Properties and Stack are synchronized classes, so they are thread-safe and can be used in multi-threaded environment.
  • By using java.util.Collections.synchronizedList(list) we can make list classes thread safe.
  • By using java.util.Collections.synchronizedSet(set)  we can make set classes thread safe.


12.Can a null element added to a TreeSet or HashSet?
  • One null element can be added to hashset.
  • TreeSet also allow null value once.


13. Explain Collection’s interface hierarchy?


14.Which design pattern Iterator follows?
  • Iterator design pattern


15.Which data structure HashSet implements
  • Hashset implements hashmap internally.


16.Why doesn't Collection extend Cloneable and Serializable?
  • List and Set and queue extends Collection interface.
  • SortedMap extends Map interface.


17.What is the importance of hashCode() and equals() methods? How they are used in Java?
  • equals() and hashcode() methods defined in "object" class.
  • If equals() method return true on comparing two objects then hashcode() of those two objects must be same.


18.What is difference between array & arraylist?
  • Array is collection of similar type of objects and fixed in size.
  • Arraylist is collection of homogeneous and heterogeneous elements.


19.What is the Properties class?
  • Properties is a subclass of Hashtable. It is used to maintain lists of values in which the key and the value is String.


20.How to convert a string array to arraylist?
  • Using java.util.Arrays.asList();

import java.util.Arrays;  
import java.util.List;  
import java.util.ArrayList;  
public class StringArrayTest  
{  
   public static void main(String[] args)  
   {  
      String[] words = {"ace", "boom", "crew", "dog", "eon"};  

      List<String> wordList = Arrays.asList(words);  

      for (String e : wordList)  
      {  
         System.out.println(e);  
      }  
   }  
}


21.Difference between HashMap and TreeMap?
  • TreeMap is an example of a SortedMap, which means that the order of the keys can be sorted, and when iterating over the keys, you can expect that they will be in order.
  • HashMap on the other hand, makes no such guarantee. Therefore, when iterating over the keys of a HashMap, you can't be sure what order they will be in.
  • HashMap is more time-efficient. A TreeMap is more space-efficient.
  • TreeMap only works with Comparable objects, HashMap only works with objects with a suitable hashCode() implementation.









______________________________________________________________

- 转自:http://www.instanceofjava.com/2015/07/collections-interview-questions-java.html



-

猜你喜欢

转载自lixh1986.iteye.com/blog/2256519