Wednesday, February 10, 2016

Collections Basics

Collections Basics

Here we will first discuss about most basic collection data structures used in Java. Then will dive into details of Collections.

3 basic Collections interfaces.
1. List: This is used for representing set of elements with duplicate elements allowed. 
2. Set: This is used for representing set of elements with no duplicate element allowed.
3. Map: This interface is used for implementing hash table data structure with key-value pair.

Most frequently used implementation class of java.util.List is java.util.LinkedList and java.util.ArrayList

Example of ArrayList:
=====ListExamples.java====

import java.util.List;
import java.util.ArrayList;

public class ListExamples {
public static void main(String[] args) {
List<String> listOfStudents=new ArrayList<String>();
listOfStudents.add("James");
listOfStudents.add("Jack");
listOfStudents.add("Mike");

displayList(listOfStudents);
System.out.println(listOfStudents.size());
listOfStudents.get(1);
listOfStudents.remove(1);
displayList(listOfStudents);
System.out.println(listOfStudents.size());
listOfStudents.remove("James");
displayList(listOfStudents);
System.out.println(listOfStudents.size());
}

public static void displayList(List<String> listOfStudents){
for(String name:listOfStudents){
System.out.print(name+" ");
}
}
}
=====
Output:
James Jack Mike 3
James Mike 2
Mike 1

Most frequently used implementation class of java.util.Set is java.util.HashSet.

Example of HashSet:
====SetExamples.java======
import java.util.HashSet;
import java.util.List;
import java.util.ArrayList;
import java.util.Set;

public class SetExamples {
public static void main(String[] args) {

Set<String> setOfStudents=new HashSet<String>();
setOfStudents.add("James");
setOfStudents.add("James"); //added twice
setOfStudents.add("Jack");
setOfStudents.add("Mike");

displayList(setOfStudents);
System.out.println(setOfStudents.size());

System.out.println(setOfStudents.contains("Jack"));

System.out.println(setOfStudents.remove(1));// notice the difference with list here,
//Don't think element at index 1 will not be deleted

displayList(setOfStudents);
System.out.println(setOfStudents.size());

System.out.println(setOfStudents.remove("James"));//notice it returns true since it is in the Set
displayList(setOfStudents);

System.out.println(setOfStudents.size());

}

public static void displayList(Set<String> listOfStudents){
for(String name:listOfStudents){
System.out.print(name+" ");
}
}
}
====
Output:
Jack James Mike 3
true
false
Jack James Mike 3
true
Jack Mike 2

Most frequently used implementation class of java.util.Map is java.util.HashMap.

Example of HashMap:
=====MapExamples.java======
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.Set;

public class MapExamples {
public static void main(String[] args) {

Map<String,String> mapOfStudents=new HashMap<String,String>();
mapOfStudents.put("RollNo_1", "James");
mapOfStudents.put("RollNo_2","James"); //added twice
mapOfStudents.put("RollNo_1","Jack");
mapOfStudents.put(null,"Mike");

displayList(mapOfStudents);
System.out.println(mapOfStudents.size());

System.out.println(mapOfStudents.keySet().contains("RollNo_1"));

System.out.println(mapOfStudents.remove("RollNo_1"));// notice removed value will be shown here

displayList(mapOfStudents);
System.out.println(mapOfStudents.size());

System.out.println(mapOfStudents.remove("James"));//notice it returns null if not found, James is not a KEY here
displayList(mapOfStudents);

System.out.println(mapOfStudents.size());

}

public static void displayList(Map<String,String> students){
for(String name:students.values()){
System.out.print(name+" ");
}
}
}
=============
Output:
Mike James Jack 3
true
Jack
Mike James 2
null
Mike James 2

No comments:

Post a Comment