java tutorial

java tutorial

Java Collection Framework

Collection Framework

Collection basically represents a group i.e., set of object units. JAVA provides Collection framework to manipulate a group of objects with operations like sorting, searching, insertion etc.

Util Package Interfaces, List, Set, Map

The util package constitutes the collection framework, date and time and other utility classes.

  • List<E> stores the data in sequence.
  • Set<E> is the set of unique elements
  • Map<K, V> is the set which works on the key and value concept. In this key is the unique value

List Interface & its classes 

The list is under java.util.List interface which is the collection of ordered elements which can be duplicate. The insertion is on the basis of indexing of elements. Using ArrayList, Vector, Linked List, and Stack classes, List can be implemented. E.g.,

List a=new ArrayList();

Set Interface and its Classes

Set is on mathematical abstraction set containing unique storage of elements. This is the only interface having this feature in its method. Major methods of Set Interface are:

  • Add()
  • Clear()
  • Contains()
  • Remove()
  • Size()

And many more.

Map Interface and its Classes

Map works on the criteria of key and value. Key and value itself make a pair in which key plays the unique role. It is best when operations need to be performed on the basis of a key value like insertion or deletion.

Map.Entry is the subinterface which provides way to access both key and values

  • getKey()- used to get key in map
  • getValue()- used to get value from map
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;

public class ArrayListTest {
   public static void main(String[] args) {
      List lst = new ArrayList();  
      lst.add("hi");     
      lst.add("how");
      lst.add("are you");
      System.out.println(lst);     

      Iterator iter = lst.iterator();
      while (iter.hasNext()) {      
         String str = (String)iter.next();
         System.out.println(str);
      }
   }	
}

Leave a Reply

Your email address will not be published. Required fields are marked *