Java Collections

Interface Class/implementation Description
List  ArrayList  List
 LinkedList  List
 Vector  Vector
 Stack  Stack
 Set    HashSet  Set
 TreeSet  Set
 SortedSet  Sorted set
Map  HashMap Map/dictionary
 TreeMap  Map/dictionary
 SortedMap  Sorted dictionary
 Hashtable  Hash-table


Declaration Syntax:

List<String> list = new ArrayList<String>();
Set<String> set = new HashSet<String>();
Map<String, String> map = new HashMap<String, String>();

ADD Elements Syntax:

list.add(“techBuddha”);
set.add(“techBuddha”);
map.put(“key”, “techBuddha”);

Traversing Collection Syntax:

Iterator<String> iterator = list.iterator();while (iterator.hasNext()) {
String text = iterator.next(); System.out.println(text);}
—————————————————-
Iterator<String> iterator = set.iterator();
while (iterator.hasNext()) { String text = iterator.next(); System.out.println(text);}
——————————————————
Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();while (iterator.hasNext()){ Map.Entry<String, String> pair = iterator.next(); String key = pair.getKey(); String value = pair.getValue(); System.out.println(key + “:” + value);
}
—————————————————–

Traversing Collection with ShortHand Fast For Loop:

for (String text : list) { System.out.println(text); }
——————————————————-
for (String text : set) { System.out.println(text); }
——————————————————-
for (Map.Entry<String, String> pair : map.entrySet()){ String key = pair.getKey(); String value = pair.getValue(); System.out.println(key + “:” + value);}
——————————————————-

Methods for Set:

Operation Method
Add element(s) add(), addAll()
Remove element(s) remove(), removeAll()
Check for the presence of element(s) contains(), containsAll()


Methods for Map:
Operation Method
Get a set of all pairs entrySet()
Get a set of all keys keySet()
Get a set of all values values()
Add a pair put(key, value)
Get the value for the specified key get(key)
Check whether the specified key is present containsKey(key)
Check whether the specified value is present containsValue(value)
Check whether the Map is empty isEmpty()
Clear the Map clear()
Remove the value for the specified key remove(key)

Collection Methods:

ArrayList<Integer> list = new ArrayList<Integer>();

sort() : Collection.sort(list);
max() : Collection.max(list);
min() : Collection.min(list);
reverse() : Collection.reverse(list);
shuffle() : Collection.shuffle(list);
swap() : Collection.swap(list, i, j);


unmodifiableList() : creates immutable arrayList from an array.
List<String> weekEndDays = Collections.unmodifiableList(new ArrayList<>(Arrays.asList(saturday, sunday)));

disjoint() : It checks whether two collections intersect, returns false if intersection is non null otherwise true.

ArrayList<String> list1 = new ArrayList<>(Arrays.asList(sunday, monday, tuesday));ArrayList<String> list2 = new ArrayList<>(Arrays.asList(saturday, sunday));
Collection.disjoint(list1, list2); // will return false, sunday is common.

Collection Initialization:

public static Set join(Set a, Set b)
{
/*Double braces initialization*/
    return new HashSet() {{ addAll(a); addAll(b); }}
}

Leave a comment