Collection:
Group of individual objects in one unit.
It’s a root interface of collections framework, which defines most common methods to act on collection object.
Collections:
There are methods available for
- adding object to list
- Remove an object from list
- get a particular object
- Update an object from list
- loop through a list to get each object
For all of the above operations, there are different method names available (for all of the classes listed in the above tree structure), just the method name may be different, you can get the names of each of classes by creating an object of that class, and perform required operation.
List
Child interface of Collection, used to represent group of collection individual object where
- Duplicates are allowed.
- Insertion order is preserved.
- Can access the objects by providing index.
Implemented classes of List: ArrayList / LinkedList / vector:
- Growable array
- null insertion is possible
- allows heterogeneous objects.
Stack is a sub class of Vector.
ArrayList al = new ArrayList();
or
ArrayList al = new ArrayList(int size);
or
ArrayList al = new ArrayList(Collection c);
Preferences:
ArrayList is best choice, if frequent operation is retrieving (as arraylist implements RandomAccess interface, so we can access any object randomly with same speed.)
Vector is also best choice, if frequent operation is retrieving (Vector implements Serializable, Clonable and RandomAccess interface as well)
LinkedList is best choice, if frequent operations are insertion/deletion (implemented by Serializable and Clonable interface.)
For any of the above classes(LinkedList / ArrayList / vector), create an object of that class, and then put a . (dot) after that object, to get all the operations defined.
let’s see example of the usage of List:
package testPkg; import java.util.LinkedList; public class ListSet { public static void main(String[] args) { LinkedList ll = new LinkedList(); ll.add(1); ll.add("ABC"); ll.add(2, 3.14f); ll.add(null); System.out.println(ll); ll.set(3, 'S'); //update data System.out.println(ll); ll.removeLast(); System.out.println(ll); System.out.println("Size:"+ll.size()); } }
OutPut:
[1, ABC, 3.14, null] [1, ABC, 3.14, S] [1, ABC, 3.14] Size:3
Set
Child interface of Collection, used to represent group of collection individual object where
- Duplicates are NOT allowed.
- Can access the objects by providing index.
Implemented classes of List: HashSet
- Growable array
- null insertion is possible
- allows heterogeneous objects.
- Insertion order is NOT preserved.
LinkedHashSet
Same as HashSet, except in this, Insertion order is preserved.
Program for Set is same as LinkedList (as shown in above code)
- Represents a group of values in form of key and value pair instead of index.
- It’s not a child interface of Collection
- Both key and value are not objects.
- Duplicate key is NOT allowed, but value can be duplicated.
package testPkg; import java.util.HashMap; public class MapTest { public static void main(String[] args) { HashMap hm = new HashMap(); hm.put("Planet", "Earth"); hm.put("Country", "Ind"); hm.put("Continent","Asia"); hm.put("Lang", "Hindi"); System.out.println(hm); System.out.println("Country:"+hm.get("Country")); hm.put("Lang", "Eng"); //update value System.out.println(hm); if (hm.remove("Lang", "Hindi")) //remove a key value pair System.out.println(hm); } }