A. The code sample given below elaborates on synchronization of various collection objects:
import java.util.ArrayList;Q. Why is it advisable to override hashCode() method as well, when equals() method is overridden?
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
public class CollectionSynchronization {
public static void main(String[] args) {
Collection collection = Collections.synchronizedCollection(new ArrayList());
List list = Collections.synchronizedList(new ArrayList());
Set set = Collections.synchronizedSet(new HashSet());
Map map = Collections.synchronizedMap(new HashMap());
SortedMap sortedMap = Collections.synchronizedSortedMap(new TreeMap());
SortedSet sortedSet = Collections.synchronizedSortedSet(new TreeSet());
}
}
A. When equals() method is overridden, then each object must have same hashcode for pair of objects which have equals() method overridden.If the .hashCode() method of your custom class doesn't work the same way your .equals() method works, the results of your code will be erroneous.
Q. If a collection object has duplicate values then how will you convert into a collection having unique values?
A. Here goes the listing:
import java.util.ArrayList;Q. How will you address the scenario given below to decide on collection object to work with:
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
public class CollectionExample {
public static void main(String[] args) {
ArrayListlist = new ArrayList ();
list.add("Apple");
list.add("Apple");
list.add("Apple");
list.add("Mango");
list.add("Guvava");
list.add("Pineapple");
Setset = new TreeSet (list);
Iteratoriterator = set.iterator();
while (iterator.hasNext()) {
String str= (String)iterator.next();
System.out.println(""+str);
}
}
}
Output:
Apple
Guvava
Mango
Pineapple
- Multiple keys with multiple values
- Multiple keys with single value
- Single key with multiple values
- Single key with single value
A. In the given scenario, HashMap will be the best choice. As long as the keys are unique, one can enter multiple values in an ArrayList and associate this with a key inside HashMap and retrieve those multiple values from unique key.
Q.How to get a TreeSet from a TreeMap?
A.The possibility of creating a TreeSet from the keyset of TreeMap.Check the example given as below:
import java.util.Iterator;Q.How a TreeSet different from HashSet?
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
public class CollectionExample {
public static void main(String[] args) {
TreeMaptreeMap = new TreeMap ();
treeMap.put("1", "Apple");
treeMap.put("2", "Mango");
treeMap.put("3", "Guvava");
treeMap.put("4", "Banana");
treeMap.put("5", "Grapes");
Setset = new TreeSet (treeMap.keySet());
Iteratoriterator = set.iterator();
while (iterator.hasNext()) {
String str = (String) iterator.next();
System.out.println("" + str);
}
}
}
Output:
1
2
3
4
5
A.One line answer is : TreeSet contains-> Unique and Ordered/Sorted values(as per natural order or based on Comparator implemented logic).It is backed by TreeMap.
While HashSet contains-> Unique values but not Ordered/Sorted
The example given below will explain the same:
import java.util.HashSet;Q.A practical problem, I want to choose a collection object which allows me to put only unique values inside it and in the same order as it is coming out of a database, here data from the database can contain duplicate values.What collection object will you choose?
import java.util.Iterator;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
public class CollectionExample {
public static void main(String[] args) {
SethashSet = new HashSet ();
SettreeSet = new TreeSet ();
addElements(hashSet);
addElements(treeSet);
System.out.println("HashSet:"+hashSet);
System.out.println("TreeSet"+treeSet);
}
private static void addElements(SetaTreeSet) {
aTreeSet.add(Integer.valueOf(6));
aTreeSet.add(Integer.valueOf(2));
aTreeSet.add(Integer.valueOf(2));
aTreeSet.add(Integer.valueOf(4));
aTreeSet.add(Integer.valueOf(7));
aTreeSet.add(Integer.valueOf(7));
aTreeSet.add(Integer.valueOf(1));
}
}
The Output is:
HashSet:[2, 4, 6, 1, 7]
TreeSet[1, 2, 4, 6, 7]
A.Choose:
Q. What is the initial capacity of Vector?
LinkedHashSetuniqueData = new LinkedHashSet (dataFromDB);
A. 10. Try out the code given below:
An article on JavaWorld on Collections' API
import java.util.Set;
import java.util.Vector;
public class CollectionExample {
public static void main(String[] args) {
Vector vector = new Vector();
int capacity = vector.capacity();
System.out.println("Capacity:" + capacity);
}
}
The Output is:
Capacity:10


1 comments:
Really very nice tutorial on collection.
Very helpful.
Post a Comment