Lecture 16 - Java Collections API
Lecture 16 - Java Collections API
Lecture 16 - Java Collections API
Collection Map
(interface) (interface)
java.util.Collection
Collection Interface
Java Collection interface
(java.util.Collection) is one of the root
interfaces of the Java Collection API.
You can not instantiate a Collection
directly, but its sub types are implemented
Collection collection = new ArrayList();
Output:
WaitingQueue : [Abdul, Bashir,
Karim]
Removed from queue : Abdul
Map Interface
java.util.Map
Map Interface
java.util.Map represents a mapping
between a key and a value.
Maps stores associations between keys
and values
Key/values both are objects and keys are
unique
Not collections, but provide a collections
view
Java Collections API contains the following
Map implementations:
java.util.HashMap
java.util.Hashtable
java.util.EnumMap
java.util.IdentityHashMap
java.util.LinkedHashMap
java.util.Properties (must study on your own)
java.util.TreeMap
java.util.WeakHashMap
Map Interface - Methods
Object put(Object key, Object value)
Associates the specified value with the
specified key in this map
//adds
Object get(Object key)
Returns the value to which this map maps the
specified key.
//get
boolean isEmpty()
Returns true if this map contains no key-value
mappings.
HashMap Example
public static void main(String[] args) {
HashMap<Integer, String> map = new
HashMap<Integer, String>();
map.put(10, "Ali"); map.put(12, "Jamal");
map.put(12, "Asad"); //What happens to
duplicate keys
map.put(14, "Samad");
System.out.println("Size of map is:- " +
map.size());
System.out.println(map);
System.out.println("The value for key is :" +
map.get(10));
Output:
Size of map is: 3
{10=Ali, 12=Asad, 14=Samad}
The value for key is : Ali
{}
Hashtable (depreciated now)
Hashtable<String, MyStudent> hashTbl = new
Hashtable<>();
hashTbl.put ("1", new MyStudent("1","Ali");
hashTbl.put ("2", new
MyStudent("2","Saleem"));
hashTbl.put ("3", new MyStudent("z","Zafar"));
//hashTbl.put (null, new
MyStudent("z","Zafar"));
//hashTbl.put("4", null);
System.out.println (hashTbl);
{3=z,Zafar, 2=2,Saleem, 1=1,Ali}
HashMap vs HashTable
Hashtable<Integer,String> ht=new
Hashtable<>();
ht.put(101,"Samad"); ht.put(101,"Ali");
ht.put(102,"Khalid"); ht.put(103,"Ali");
System.out.println("--------Hash table---------");
System.out.println(ht);
HashMap<Integer,String> hm=new
HashMap<>();
hm.put(101,"Samad"); hm.put(101,"Ali");
hm.put(null,null); hm.put(103,"Ali");
System.out.println("-------Hash map----------");
System.out.println(hm);
Output
-------------Hash table--------------
{103=Ali, 102=Khalid, 101=Ali}
-----------Hash map-----------
{null=null, 101=Ali, 103=Ali}
Hashmap vs Hashtable
HashMap is non synchronized thus not-
thread safe
HashMap allows one null key and multiple
null values whereas Hashtable doesn’t
allow any null key or value.
HashMap is generally preferred over
HashTable if thread synchronization is not
needed
equals() and hashCode()
Java hashCode() and equals()
In Collection both hashCode() and equals()
methods are used to compare objects in
collections.
With
Iterator i = collection.iterator();
while(i.hasNext()) {
Object value = i.next();
}
Using Iterator
ArrayList<String> names = new ArrayList<>();
names.add("Sameer");
names.add("Waleed");
names.add("Sami");
Iterator it = names.iterator();
while ( it.hasNext() ){
String obj = (String)it.next();
System.out.println(obj);
}
Some collections do not allow modification
of the collection while using Iterator.
Throws ConcurrentModificationException on
add( ) but not on remove( )
Iterable
A class that implements the Java Iterable
(java.lang.Iterable) interface can be
iterated with the Java for-each loop.
Several classes in Java that implements
the Java Iterable interface.
The Collection interface extends Iterable,
so all subtypes of Collection also
implement the Iterable interface.
Java List and Set interfaces etc.
//ArrayList is Iterable
List list = new ArrayList();
list.add("one");
list.add("two");
list.add("three");
for(Object o : list){
System.out.println(o.toString());
}
Java.util.Iterable Interface has only method
public Iterator <T> iterator( )
Before Team: [Player{ rank:12, 'Zaeem', age: 32}, Player{ rank:18, 'Samad',
age: 29}, Player{ rank:7, 'Basit', age: 33}, Player{ rank:10, 'Hameed', age:
24}]
---------------------
After Team :[Player{ rank:7, 'Basit', age: 33}, Player{ rank:10, 'Hameed', age:
24}, Player{ rank:12, 'Zaeem', age: 32}, Player{ rank:18, 'Samad', age: 29}]
After Team (age): [Player{ rank:10, 'Hameed', age: 24}, Player{ rank:18,
'Samad', age: 29}, Player{ rank:12, 'Zaeem', age: 32}, Player{ rank:7, 'Basit',
age: 33}]
After Team (name): [Player{ rank:7, 'Basit', age: 33}, Player{ rank:10,
'Hameed', age: 24}, Player{ rank:18, 'Samad', age: 29}, Player{ rank:12,
'Zaeem', age: 32}]
§
Thank you
Learn on your own
java.util.PriorityQueue
Java Properties