Set Interface v/s List Interface

Set Interface List Interface
It is collection of unique elements. It may contains duplicate elements.
It is a distinct list of elements which is unordered. List is an ordered sequvanceof elements.
No new additional method other than collection. New Methods other than collection are defined in a list.
Can use only Iteratorto traverse Set. Can use both Iterator and ListIterator to traverse list.
Set Interface does not allowNULLvalues. List Interface allows NULL values.
Sub class of Set Interface are :
- HashSet [ unordered ]
- TreeSet from SortedSet [ Ordered ]
Sub class of List Interface are :
- ArrayList
- LinkedList
- Vector
It is Based on values. It is Based on Index.
Example :
Set s=new HashSet();
Example :
List l=new ArrayList();


Example of abstract class and interface in Java : 

// Set Interface example

import java.util.*;

class SetInterface{
 
 public static void main(String... str){
  
  // unordered 
  Set s=new HashSet();
  s.add(1);
  s.add(2);
  s.add(5);
  s.add(8);
  
  System.out.println(s);
  
  // ordered
  Set s1=new TreeSet();
  s1.add(4);
  s1.add(2);
  s1.add(9);
  s1.add(1);
  
  System.out.println(s1);
 }
 
}

Output : 
[1,2,5,8]
[1,2,4,9]


// example of List interface

import java.util.*;

class ListInterface{
 public static void main(String... str){
  
  List l=new ArrayList();
  l.add("XYZ");
  l.add(10);
  l.add(1.5f);
  l.add('c');
  
  //null value accept
  
  l.add(null);
  l.add(true);
  
  System.out.println(l);
 }
}

Output : 
[XYZ,10,1.5f,c,null,true ]

Share this

Related Posts

Previous
Next Post »