DEV Community

Cover image for In java Collection Framework and under the sub interface and Implement classes.
Gayathri.R
Gayathri.R

Posted on

In java Collection Framework and under the sub interface and Implement classes.

Interface Collection
Its under the java.util pakage
its helps storing and processing effciently,This interface is a member of the Java Collections Framework.

The framework had to be high-performance. The implementations for the fundamental collections (dynamic arrays, linked lists, trees, and hashtables) were to be highly efficient.

The framework had to allow different types of collections to work in a similar manner and with a high degree of interoperability.

The framework had to extend and/or adapt a collection easily.
Enter fullscreen mode Exit fullscreen mode

ArrayList

import java.util.*;
public class CollectionsDemo {

public static void main(String[] args) {
// ArrayList
List a1 = new ArrayList();
a1.add("Zara");
a1.add("Mahnaz");
a1.add("Ayan");
System.out.println(" ArrayList Elements");
System.out.print("\t" + a1);

  // LinkedList
  List l1 = new LinkedList();
  l1.add("Zara");
  l1.add("Mahnaz");
  l1.add("Ayan");
  System.out.println();
  System.out.println(" LinkedList Elements");
  System.out.print("\t" + l1);

  // HashSet
  Set s1 = new HashSet(); 
  s1.add("Zara");
  s1.add("Mahnaz");
  s1.add("Ayan");
  System.out.println();
  System.out.println(" Set Elements");
  System.out.print("\t" + s1);

  // HashMap
  Map m1 = new HashMap(); 
  m1.put("Zara", "8");
  m1.put("Mahnaz", "31");
  m1.put("Ayan", "12");
  m1.put("Daisy", "14");
  System.out.println();
  System.out.println(" Map Elements");
  System.out.print("\t" + m1);
Enter fullscreen mode Exit fullscreen mode

}
}
Set Interface
A Set is a Collection that cannot contain duplicate elements. It models the mathematical set abstraction.

The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited.

Set also adds a stronger contract on the behavior of the equals and hashCode operations, allowing Set instances to be compared meaningfully even if their implementation types differ.

Set has its implementation in various classes like HashSet, TreeSet, LinkedHashSet. Below are some of the implementations of the Set interface in Java.

import java.util.HashSet;
import java.util.Set;

public class SetDemo {

  public static void main(String args[]) { 
      int count[] = {34, 22,10,60,30,22};
      Set<Integer> set = new HashSet<>();
      try {
         for(int i = 0; i < 5; i++) {
            set.add(count[i]);
         }
         System.out.println(set);
      }
      catch(Exception e) {}
   }
} 
Enter fullscreen mode Exit fullscreen mode

TreeSet

import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

public class SetDemo {

  public static void main(String args[]) { 
      int count[] = {34, 22,10,60,30,22};
      Set<Integer> set = new HashSet<>();
      try {
         for(int i = 0; i < 5; i++) {
            set.add(count[i]);
         }
         System.out.println(set);

         TreeSet<Integer> sortedSet = new TreeSet<>(set);
         System.out.println("The sorted list is:");
         System.out.println(sortedSet);

         System.out.println("The First element of the set is: "+ (Integer)sortedSet.first());
         System.out.println("The last element of the set is: "+ (Integer)sortedSet.last());
      }
      catch(Exception e) {}
   }
}
Enter fullscreen mode Exit fullscreen mode

** Map Interface**

The Map interface maps unique keys to values. A key is an object that you use to retrieve a value at a later date.

Given a key and a value, you can store the value in a Map object. After the value is stored, you can retrieve it by using its key.

Several methods throw a NoSuchElementException when no items exist in the invoking map.

A ClassCastException is thrown when an object is incompatible with the elements in a map.

A NullPointerException is thrown if an attempt is made to use a null object and null is not allowed in the map.

An UnsupportedOperationException is thrown when an attempt is made to change an unmodifiable map.
Enter fullscreen mode Exit fullscreen mode

import java.util.HashMap;
import java.util.Map;
public class CollectionsDemo {

   public static void main(String[] args) {
      Map<String, String> m1 = new HashMap<>(); 
      m1.put("Zara", "8");
      m1.put("Mahnaz", "31");
      m1.put("Ayan", "12");
      m1.put("Daisy", "14");

      System.out.println();
      System.out.println(" Map Elements");
      System.out.print("\t" + m1);
   }
}


Enter fullscreen mode Exit fullscreen mode

Top comments (0)