Collection1
11 collections.sort() VS Arrays.sort()
Difference between the two is input. Collections.sort() has a input as List so it does a translation of List to array and vice versa which is an additional step while sorting. So this should be used when you are trying to sort a list. Arrays.sort is for arrays so the sorting is done directly on the array. So clearly it should be used when you have a array available with you and you want to sort it.
12 Java Map Interface
A map contains values on the basis of key i.e. key and value pair. Each key and value pair is known as an entry. Map contains only unique keys. Map is useful if you have to search, update or delete elements on the basis of key.
Method
Description
Object put(Object key, Object value)
It is used to insert an entry in this map.
void putAll(Map map)
It is used to insert the specified map in this map.
Object remove(Object key)
It is used to delete an entry for the specified key.
Object get(Object key)
It is used to return the value for the specified key.
boolean containsKey(Object key)
It is used to search the specified key from this map.
Set keySet()
It is used to return the Set view containing all the keys.
Set entrySet()
It is used to return the Set view containing all the keys and values.
12.1 HashMap see HashMap chapter
12.2 LinkedHashMap
A LinkedHashMap contains values based on the key.
It contains only unique elements.
It may have one null key and multiple null values.
It is same as HashMap instead maintains insertion order.
method: Object get(Object key); void clear():remove all mapping from the map; boolean containsKey(Object Key).
12.3 TreeMap
A TreeMap contains values based on the key. It implements the NavigableMap interface and extends AbstractMap class.
It contains only unique elements.
It cannot have null key but can have multiple null values.
It is same as HashMap instead maintains ascending order.
Method
Description
boolean containsKey(Object key)
It is used to return true if this map contains a mapping for the specified key.
boolean containsValue(Object value)
It is used to return true if this map maps one or more keys to the specified value.
Object firstKey()
It is used to return the first (lowest) key currently in this sorted map.
Object get(Object key)
It is used to return the value to which this map maps the specified key.
Object lastKey()
It is used to return the last (highest) key currently in this sorted map.
Object remove(Object key)
It is used to remove the mapping for this key from this TreeMap if present.
Object put(Object key, Object value)
put the mapping in the treemap
void putAll(Map map)
It is used to copy all of the mappings from the specified map to this map.
Set entrySet()
It is used to return a set view of the mappings contained in this map.
int size()
It is used to return the number of key-value mappings in this map.
Collection values()
It is used to return a collection view of the values contained in this map.
12.4 Hashtable
It inherits Dictionary class and implements the Map interface.
A Hashtable is an array of list. Each list is known as a bucket. The position of bucket is identified by calling the hashcode() method. A Hashtable contains values based on the key.
It contains only unique elements.
It is not allow to have any null key or value.
It is synchronized.
Method
Description
void clear()
It is used to reset the hash table.
boolean contains(Object value)
This method return true if some value equal to the value exist within the hash table, else return false.
boolean containsValue(Object value)
This method return true if some value equal to the value exists within the hash table, else return false.
boolean containsKey(Object key)
This method return true if some key equal to the key exists within the hash table, else return false.
boolean isEmpty()
This method return true if the hash table is empty; returns false if it contains at least one key.
void rehash()
It is used to increase the size of the hash table and rehashes all of its keys.
Object get(Object key)
This method return the object that contains the value associated with the key.
Object remove(Object key)
It is used to remove the key and its value. This method return the value associated with the key.
int size()
This method return the number of entries in the hash table.
13. HashMap VS Hashtable
HashMap
Hashtable
1) HashMap is non synchronized. It is not-thread safe and can't be shared between many threads without proper synchronization code.
Hashtable is synchronized. It is thread-safe and can be shared with many threads.
2) HashMap allows one null key and multiple null values.
Hashtable doesn't allow any null key or value.
3) HashMap is a new class introduced in JDK 1.2.
Hashtable is a legacy class.
4) HashMap is fast.
Hashtable is slow.
5) We can make the HashMap as synchronized by calling this code Map m = Collections.synchronizedMap(hashMap);
Hashtable is internally synchronized and can't be unsynchronized.
6) HashMap is traversed by Iterator.
Hashtable is traversed by Enumerator and Iterator.
7) Iterator in HashMap is fail-fast.
Enumerator in Hashtable is not fail-fast. It is fail-safe.
8) HashMap inherits AbstractMap class.
Hashtable inherits Dictionary class.
14 Collections Class
Java Collection class supports the polymorphic algorithms that operate on collections.
Java Collection class throws a NullPointerException if the collections or class objects provided to them are null.
Method
Description
static <T> boolean addAll(Collection<? super T> c, T... elements)
It is used to add all of the specified elements to the specified collection.
static <T> Queue<T> asLifoQueue(Deque<T> deque)
It is used to return a view of a Deque as a Last-In-First-Out (LIFO) Queue.
static <T> int binarySearch(List<? extends T> list, T key, Comparator<? super T< c)
It is used to search the specified list for the specified object using the binary search algorithm.
static <E> List<E> checkedList(List<E> list, Class<E> type)
It is used to return a dynamically typesafe view of the specified list.
static <E> Set<E> checkedSet(Set<E> s, Class<E> type)
It is used to return a dynamically typesafe view of the specified set.
static <E> SortedSet<E>checkedSortedSet(SortedSet<E> s, Class<E> type)
It is used to return a dynamically typesafe view of the specified sorted set
static void reverse(List<?> list)
It is used to reverse the order of the elements in the specified list.
static <T> T max(Collection<? extends T> coll, Comparator<? super T> comp)
It is used to return the maximum element of the given collection, according to the order induced by the specified comparator.
static <T extends Object & Comparable<? super T>>T min(Collection<? extends T> coll)
It is used to return the minimum element of the given collection, according to the natural ordering of its elements.
staticboolean replaceAll(Listlist, T oldVal, T newVal)
It is used to replace all occurrences of one specified value in a list with another.
15. Comparable VS Comparator
Comparable and Comparator both are interfaces and can be used to sort collection elements.
Comparable
Comparator
1) Comparable provides single sorting sequence. In other words, we can sort the collection on the basis of single element such as id or name or price etc.
Comparator provides multiple sorting sequence. In other words, we can sort the collection on the basis of multiple elements such as id, name and price etc.
2) Comparable affects the original class i.e. actual class is modified.
Comparator doesn't affect the original class i.e. actual class is not modified.
3) Comparable provides compareTo() methodto sort elements.
Comparator provides compare() method to sort elements.
4) Comparable is found in java.langpackage.
Comparator is found in java.utilpackage.
5) We can sort the list elements of Comparable type by Collections.sort(List)method.
We can sort the list elements of Comparator type by Collections.sort(List,Comparator)method.
Comparable interface can be used to provide single way of sorting whereas Comparator interface is used to provide different ways of sorting.
For using Comparable, Class needs to implement it whereas for using Comparator we don’t need to make any change in the class.
Comparable interface is in
java.lang
package whereas Comparator interface is present injava.util
package.We don’t need to make any code changes at client side for using Comparable
Arrays.sort() or Collection.sort()
methods automatically uses thecompareTo()
method of the class. For Comparator, client needs to provide the Comparator class to use in compare() method.
16 Properties Class in Java
Thepropertiesobject contains key and value pair both as a string. The java.util.Properties class is the subclass of Hashtable.
It can be used to get property value based on the property key. The Properties class provides methods to get data from properties file and store data into properties file. Moreover, it can be used to get properties of system.
Advantage of properties file
Recompilation is not required, if information is changed from properties file:If any information is changed from the properties file, you don't need to recompile the java class. It is used to store information which is to be changed frequently.
17. ArrayList VS Vector
ArrayList and Vector both implements List interface and maintains insertion order.
ArrayList
Vector
1) ArrayList isnot synchronized.
Vector issynchronized.
2) ArrayListincrements 50%of current array size if number of element exceeds from its capacity.
Vectorincrements 100%means doubles the array size if total number of element exceeds than its capacity.
3) ArrayList isnot a legacyclass, it is introduced in JDK 1.2.
Vector is alegacyclass.
4) ArrayList isfastbecause it is non-synchronized.
Vector isslowbecause it is synchronized i.e. in multithreading environment, it will hold the other threads in runnable or non-runnable state until current thread releases the lock of object.
5) ArrayList usesIteratorinterface to traverse the elements.
Vector usesEnumerationinterface to traverse the elements. But it can use Iterator also.
18. Collection VS Collections
Collection is a root level interface of the Java Collection framework. Most of the classes in Java Collection Framework inherit from this interface. List, Set and Queue are main sub interfaces of this interface. JDK doesn’t provide any direct implementations of this interface. But, JDK provides direct implementations of it’s sub interfaces. ArrayList, Vector , HashSet, LinkedHashSet, PriorityQueue are some indirect implementations of Collection interface. Map interface, which is also a part of java collection framework, doesn’t inherit from Collection interface. Collection interface is a member of java.util package.
Collections is an utility class in java.util package. It consists of only static methods which are used to operate on objects of type Collection. For example, it has the method to find the maximum element in a collection, it has the method to sort the collection, it has the method to search for a particular element in a collection. Below is the list of some important methods of Collections class.
Collections.max()
This method returns maximum element in the specified collection.
Collections.min()
This method returns minimum element in the given collection.
Collections.sort()
This method sorts the specified collection.
Collections.shuffle()
This method randomly shuffles the elements in the specified collection.
Collections.synchronizedCollection()
This method returns synchronized collection backed by the specified collection.
Collections.binarySearch()
This method searches the specified collection for the specified object using binary search algorithm.
Collections.disjoint()
This method returns true if two specified collections have no elements in common.
Collections.copy()
This method copies all elements from one collection to another collection.
Collections.reverse()
This method reverses the order of elements in the specified collection.
Last updated
Was this helpful?