NavBar

Saturday 17 February 2018

What is collection in java

Collection:-

  • Collection is a group of object which could be homogeneous or heterogeneous with some size.
  • Collection in java are data structures primary defined through a set of classes and interfaces.
  • It also refer to as a container which contain the group of multiple element into a single unit.

Collections are used for:-

  • To generalized the array concept.
  • To store multiple type of object.
  • To define set of interfaces for storing java objects.
  • Collection is group of classes which gives the functionality of handling vectors,arraylist,hashmap,hashtable,stack etc.

Collection interfaces uses following methods:-

1.add():-To add element in collection.

2.remove():-To remove element from collection.

3.toarray():-Convert collection into array of element.

4.contains():-To check if specified element is in the collection respectively.     

Types Of Collections:-

What is collection in java

Set:-

  • Extends collection interface
  • Does't allow duplicate element.
  • Support all methods present in collection interface.

Types of set:-

1. HashSet class:-

  • Implemented using a hash table.
  • No ordering of element.
Example:-

 import java.util.*;
class TestHashSet
{
        public static void main(String arg[])
        {
                  HashSet obj=new HashSet();
                  obj.add("Apple");
                  obj.add("Orange");
                  obj.add("Apple");
                  obj.add("Banana");
System.out.println("elements are"+obj);
System.out.println("Size is:"+obj.size());
        }
}

Output:- elements are Apple,Orange,Banana
              Size is:- 3


 

   2.TreeSet class:-


  • In this values are sorted in asending order.
  • It implement tree structure.
  • Guarantee ordering of element.
 Example:-

 import java.util.*;
class TestTreeSet
{
        public static void main(String arg[])
        {
                  TreeSet obj=new TreeSet();
                  obj.add("Z");
                  obj.add("A");
                  obj.add("F");
                  obj.add("E");
System.out.println("elements are"+obj);
System.out.println("Size is:"+obj.size());
        }
}

Output:- elements are A E F Z
              Size is:- 4


3.LinkHashSet class:-


  • It is an subclass of hashset.
  • LinkHashSet are doubly linked lists.
  • They are in fix order but not in sorted manner.
 Example:-


 import java.util.*;
class TestLinkHashSet
{
        public static void main(String arg[])
        {
                 LinkHashSet obj=new LinkHashSet();
                  obj.add("One");
                  obj.add("Two");
                  obj.add("Three");
                  obj.add("Four");
System.out.println("elements are"+obj);
System.out.println("Size is:"+obj.size());
        }
}

Output:- elements are One Two Three Four
              Size is:- 4

List:-


  • List is order collection called sequence.
  • Array list and linked list are implementation classes of listinterface.
  • It extends collection interface.
  • It may have duplicate element.
  • In list elements are added at end of list.

 Types of  list:-

1. ArrayList Class:-

  • Array list is an array based implementation where elements can be access directly via get and set menthod.
  • It can dynamically increase or decrease.

2.LinkList Class:-

  • It will give better performance onadd or remove compare to arraylist.
  • It will give poor performance on get and set method compare to arraylist.

Queue:-

  • Queue is a collection used to hold multiple elements prior to processing.
  • Beside basic collection operation a queue provide additional insertion,extraction and innspection operation.
  • In queue ordering is First Come First Out basis(FIFO).

No comments:

Post a Comment