Java Collections Framework

Part of Collections Framework

  • Collection          Collection Subtypes: The following interfaces (collection types) extends the Collection interface:
      1.  List
      2.  Set
      3. SortedSet
      4. NavigableSet
      5. Queue
      6. Deque 
    Example :
    import java.util.*;
    import java.util.Collection;
    public class collectioninterface {
    public static void main(String[] args) {
    Collection collection = new ArrayList();
    collection.add("Dev");
    collection.add("Manuals");
    System.out.print(" Elements Of the Array List are ");
    System.out.print(collection + ".");
    }
    }
     
     
  • Set    

    Set Interface in Java:-

    The Set interface extends the Collection interface. A Set is a Collection that cannot contain duplicate elements. Not only must these elements be unique, but while they are in the set, each element must not be modified. It permits a single element to be null. The Set interface contains only methods inherited from Collection.

    The Collection Framework provides two concrete set implementations:
    1- HashSet
    2- TreeSet.
    The HashSet represents a set backed by a hash table providing constant lookup−time access to unordered elements.
    The TreeSet maintains its elements in an ordered fashion within a balanced tree.
    The Set interface contains only methods inherited from Collection interface, these are shown  given below:
  • add( )
  • clear( )
  • contains( )
  • isEmpty( )
  • iterator( )
  • remove( )
  • size( )
The Example for the Set interface which impliments the TreeSet class ia as-SetDemo.java
package devmanuals.com;
import java.util.*;
public class SetDemo {
        public static void main(String[] args) {

                Set st = new TreeSet();

                st.add("Gyan");
                st.add("Rohit");
                st.add("Anand");
                st.add("Arunesh");

                Iterator itr = st.iterator();

                while (itr.hasNext()) {
                        String str = (String) itr.next();

                        System.out.println("Name :" + str);
                }
        }
}

Comments

  1. visit

    http://www.javaproficiency.com/2015/05/java-collections-framework-tutorials.html

    ReplyDelete
  2. Thanks Sharing Nice article .There is also good nice Java Collection Framework Tutorials

    ReplyDelete

Post a Comment

Popular posts from this blog

Bluetooth Data Transfer Example

How to Create & Extract tar.gz and tar.bz2 Files in Linux