[b]Collections:[/b]
[img]http://dl2.iteye.com/upload/attachment/0121/4980/2da51428-b00d-317c-9126-0c8f0b3eaf6f.png[/img]
[b]1.Why Map interface doesn’t extend Collection interface?[/b]
[list]
[*]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.
[/list]
[b]2.What is difference between HashMap and Hashtable?[/b]
[list]
[*]Synchronization or Thread Safe
[*]Null keys and null values
[*]Iterating the values
[*]Default Capacity
[/list]
[b]3.Differences between comparable and comparator?[/b]
[list]
[*]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
[/list]
[b]Comparable[/b]
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.
[b]Comparator[/b]
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.
[b]4.How can we sort a list of Objects?[/b]
[list]
[*]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().
[/list]
[b]5.What is difference between fail-fast and fail-safe?[/b]
[list]
[*]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
[/list]
[b]6. What is difference between Iterator ,ListIterator and Enumeration?[/b]
[list]
[*]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.
[/list]
[b]7.What is difference between Set and List in Java?[/b]
[list]
[*]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
[/list]
[b]8.Differences between arraylist and vector?[/b]
[list]
[*]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.
[/list]
[b]9.What are the classes implementing List interface?[/b]
[list]
[*]ArrayList
[*]LinkedList
[*]Vector
[/list]
[b]10. Which all classes implement Set interface ?[/b]
[list]
[*]HashSet
[*]LinkedHashSet
[*]TreeSet
[/list]
[b]11.How to make a collection thread safe?[/b]
[list]
[*]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.
[/list]
[b]12.Can a null element added to a TreeSet or HashSet?[/b]
[list]
[*]One null element can be added to hashset.
[*]TreeSet also allow null value once.
[/list]
[b]13. Explain Collection’s interface hierarchy?[/b]
[img]http://dl2.iteye.com/upload/attachment/0113/0434/37be0431-c1ea-3d25-b32c-6769a3f9a3d1.png[/img]
[b]14.Which design pattern Iterator follows?[/b]
[list]
[*]Iterator design pattern
[/list]
[b]15.Which data structure HashSet implements[/b]
[list]
[*]Hashset implements hashmap internally.
[/list]
[b]16.Why doesn't Collection extend Cloneable and Serializable?[/b]
[list]
[*]List and Set and queue extends Collection interface.
[*]SortedMap extends Map interface.
[/list]
[b]17.What is the importance of hashCode() and equals() methods? How they are used in Java?[/b]
[list]
[*]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.
[/list]
[b]18.What is difference between array & arraylist?[/b]
[list]
[*]Array is collection of similar type of objects and fixed in size.
[*]Arraylist is collection of homogeneous and heterogeneous elements.
[/list]
[b]19.What is the Properties class?[/b]
[list]
[*]Properties is a subclass of Hashtable. It is used to maintain lists of values in which the key and the value is String.
[/list]
[b]20.How to convert a string array to arraylist?[/b]
[list]
[*]Using java.util.Arrays.asList();
[/list]
[b]21.Difference between HashMap and TreeMap?[/b]
[list]
[*]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.
[/list]
______________________________________________________________
- 转自:http://www.instanceofjava.com/2015/07/collections-interview-questions-java.html
-
[img]http://dl2.iteye.com/upload/attachment/0121/4980/2da51428-b00d-317c-9126-0c8f0b3eaf6f.png[/img]
[b]1.Why Map interface doesn’t extend Collection interface?[/b]
[list]
[*]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.
[/list]
[b]2.What is difference between HashMap and Hashtable?[/b]
[list]
[*]Synchronization or Thread Safe
[*]Null keys and null values
[*]Iterating the values
[*]Default Capacity
[/list]
[b]3.Differences between comparable and comparator?[/b]
[list]
[*]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
[/list]
[b]Comparable[/b]
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.
[b]Comparator[/b]
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.
[b]4.How can we sort a list of Objects?[/b]
[list]
[*]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().
[/list]
[b]5.What is difference between fail-fast and fail-safe?[/b]
[list]
[*]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
[/list]
[b]6. What is difference between Iterator ,ListIterator and Enumeration?[/b]
[list]
[*]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.
[/list]
[b]7.What is difference between Set and List in Java?[/b]
[list]
[*]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
[/list]
[b]8.Differences between arraylist and vector?[/b]
[list]
[*]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.
[/list]
[b]9.What are the classes implementing List interface?[/b]
[list]
[*]ArrayList
[*]LinkedList
[*]Vector
[/list]
[b]10. Which all classes implement Set interface ?[/b]
[list]
[*]HashSet
[*]LinkedHashSet
[*]TreeSet
[/list]
[b]11.How to make a collection thread safe?[/b]
[list]
[*]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.
[/list]
[b]12.Can a null element added to a TreeSet or HashSet?[/b]
[list]
[*]One null element can be added to hashset.
[*]TreeSet also allow null value once.
[/list]
[b]13. Explain Collection’s interface hierarchy?[/b]
[img]http://dl2.iteye.com/upload/attachment/0113/0434/37be0431-c1ea-3d25-b32c-6769a3f9a3d1.png[/img]
[b]14.Which design pattern Iterator follows?[/b]
[list]
[*]Iterator design pattern
[/list]
[b]15.Which data structure HashSet implements[/b]
[list]
[*]Hashset implements hashmap internally.
[/list]
[b]16.Why doesn't Collection extend Cloneable and Serializable?[/b]
[list]
[*]List and Set and queue extends Collection interface.
[*]SortedMap extends Map interface.
[/list]
[b]17.What is the importance of hashCode() and equals() methods? How they are used in Java?[/b]
[list]
[*]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.
[/list]
[b]18.What is difference between array & arraylist?[/b]
[list]
[*]Array is collection of similar type of objects and fixed in size.
[*]Arraylist is collection of homogeneous and heterogeneous elements.
[/list]
[b]19.What is the Properties class?[/b]
[list]
[*]Properties is a subclass of Hashtable. It is used to maintain lists of values in which the key and the value is String.
[/list]
[b]20.How to convert a string array to arraylist?[/b]
[list]
[*]Using java.util.Arrays.asList();
[/list]
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);
}
}
}
[b]21.Difference between HashMap and TreeMap?[/b]
[list]
[*]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.
[/list]
______________________________________________________________
- 转自:http://www.instanceofjava.com/2015/07/collections-interview-questions-java.html
-