Skip to main content

collection interface in java


Collection:Collection is an interface which is present in java.util package and used for representing a group of individual object as a single entity. Collection interface does not having any direct implementation class, collection having some  direct child interfaces and they are having implementation classes based on some specific data structure and provide  ready made method support for various operations .
Child interfaces of Collection are List, Set and Queue……
Collection interface having some common methods are used for every collection objects.
No.
Method
Description
1
public boolean add(Object element)
Used to insert an object in this collection.
2
public boolean addAll(Collection c)
 Used to insert the specified collection objects in the invoking collection.
3
public boolean remove(Object element)
Used to delete an object from collection.
4
public boolean removeAll(Collection c)
Used to delete all the objects of specified collection from the invoking collection.
5
public boolean retainAll(Collection c)
Used to delete all the objects of invoking collection except the specified collection.
6
public int size()
return the total number of objects in the collection.
7
public void clear()
remove all elements from the collection.
8
public boolean contains(Object element)
is used to search an element.
9
public boolean containsAll(Collection c)
is used to search the specified collection in this collection.
10
public Iterator iterator()
returns an object of iterator or  return cursor.
11
public Object[] toArray()
Converts collection into array.
12
public boolean isEmpty()
Check for empty collection.
13
public boolean equals(Object element)
Comparison of two collections.
14
public int hashCode()
returns the hashcode number for collection.

List:-List is a child interface of Collection interface is used for representing group of individual objects as a single entity where duplicate objects are allowed and maintain insertion order .

Methods of List interface:-

public abstract int size();
public abstract boolean isEmpty();
public abstract boolean contains(java.lang.Object);
public abstract java.util.Iterator<E> iterator();
public abstract java.lang.Object[] toArray();
public abstract <T> T[] toArray(T[]);
public abstract boolean add(E);
public abstract boolean remove(java.lang.Object);
public abstract boolean containsAll(java.util.Collection<?>);
public abstract boolean addAll(java.util.Collection<? extends E>)
public abstract boolean addAll(int, java.util.Collection<? extend
public abstract boolean removeAll(java.util.Collection<?>);
public abstract boolean retainAll(java.util.Collection<?>);
public void replaceAll(java.util.function.UnaryOperator<E>);
public void sort(java.util.Comparator<? super E>);
public abstract void clear();
public abstract boolean equals(java.lang.Object);
public abstract int hashCode();
public abstract E get(int);
public abstract E set(int, E);
public abstract void add(int, E);
public abstract E remove(int);
public abstract int indexOf(java.lang.Object);
public abstract int lastIndexOf(java.lang.Object);
public abstract java.util.ListIterator<E> listIterator();
public abstract java.util.ListIterator<E> listIterator(int);
public abstract java.util.List<E> subList(int, int);
public java.util.Spliterator<E> spliterator();


Java ArrayList class

 Java ArrayList class is an implementation class of List interface uses a dynamic array for storing objects. It inherits AbstractList class and implements List interface.
Some important points about Java ArrayList class are:
  • Java ArrayList class can contain duplicate elements.
  • Java ArrayList class maintains insertion order.
  • Java ArrayList class is non synchronized.
  • Java ArrayList allows random access because array works at the index basis.
  • In Java ArrayList class, manipulation (insertion and deletion) is slow because a lot of shifting.
Example1:-TestList.java

 import java.util.*;
class TestList
{
public static void main(String[]args)
{
Collection c=new ArrayList();
System.out.println("No of Elements : "+c.size());
System.out.println("Elements are : "+c);
c.add("Raj");
c.add("sonu");
System.out.println("Elements are : "+c);
c.add(123);
c.add("321");
c.add(null);
System.out.println("Elements are : "+c);
c.add("sonu");
c.add(123);
c.add(null);
System.out.println("Elements are : "+c);
}
}
Output:-
C:\JAVATECH>javac TestList.java
         C:\JAVATECH>java TestList
No of Elements : 0
Elements are : []
Elements are : [Raj, sonu]
Elements are : [Raj, sonu, 123, 321, null]
Elements are : [Raj, sonu, 123, 321, null, sonu, 123, null]

Example2:-TestList.java
import java.util.*;
class TestList
{
String name;
int roll;
TestList(String name,int roll)
{
this.name=name;
this.roll=roll;
}
public static void main(String[]args)
{
Collection c=new ArrayList();
TestList l1=new TestList("Sonu",101);
TestList l2=new TestList("Monu",102);
TestList l3=new TestList("Neha",103);
c.add(l1);
c.add(l2);
c.add(l3);
System.out.println("Elements are : "+c);
}
}
Output:-
C:\JAVATECH>javac TestList.java
C:\JAVATECH>java TestList
Elements are : [TestList@52e922, TestList@25154f, TestList@10dea4e]

Example3:-TestList.java
import java.util.*;
class TestList
{
String name;
int roll;
TestList(String name,int roll)
{
this.name=name;
this.roll=roll;
}
public static void main(String[]args)
{
Collection c=new ArrayList();
TestList l1=new TestList("Sonu",101);
TestList l2=new TestList("Monu",102);
TestList l3=new TestList("Neha",103);
c.add(l1);
c.add(l2);
c.add(l3);
Iterator itr=c.iterator();
while(itr.hasNext())
{
TestList p=(TestList)itr.next();
System.out.println("Name is :"+p.name+"  RollNo is :  "+p.roll);
}
}
}
Output:-
C:\JAVATECH>javac TestList.java
C:\JAVATECH>java TestList
Name is :Sonu  RollNo is :  101
Name is :Monu  RollNo is :  102
Name is :Neha  RollNo is :  103

Example4:-TestList.java
import java.util.*;
class TestList
{
String name;
int roll;
TestList(String name,int roll)
{
this.name=name;
this.roll=roll;
}
public static void main(String[]args)
{
Collection c=new ArrayList();
TestList l1=new TestList("Sonu",101);
TestList l2=new TestList("Monu",102);
TestList l3=new TestList("Neha",103);
c.add(l1);
c.add(l2);
c.add(l3);
Collection c1=new ArrayList();
TestList l4=new TestList("Sonu1",1011);
TestList l5=new TestList("Monu1",1022);
TestList l6=new TestList("Neha1",1033);
c1.add(l4);
c1.add(l5);
c1.add(l6);
c.addAll(c1);
Iterator itr=c.iterator();
while(itr.hasNext())
{
TestList p=(TestList)itr.next();
System.out.println("Name is :"+p.name+"  RollNo is :  "+p.roll);
}
}
}
Output:-
C:\JAVATECH>javac TestList.java
C:\JAVATECH>java TestList
Name is :Sonu  RollNo is :  101
Name is :Monu  RollNo is :  102
Name is :Neha  RollNo is :  103
Name is :Sonu1  RollNo is :  1011
Name is :Monu1  RollNo is :  1022
Name is :Neha1  RollNo is :  1033


LinkedList:-LinkedList is a another implementation class of List interface where underlying data structure is resizable array and doubly link list so LinkedList class is best suitable for manipulation like insertion ,deletion ,updating and LinkedList is a worst choice for searching whereas ArrayList is a best for searching. 


Example1:-TestList.java
import java.util.*;
class TestList
{
String name;
int roll;
TestList(String name,int roll)
{
this.name=name;
this.roll=roll;
}
public static void main(String[]args)
{
Collection c=new LinkedList();
TestList l1=new TestList("Sonu",101);
TestList l2=new TestList("Monu",102);
TestList l3=new TestList("Neha",103);
c.add(l1);
c.add(l2);
c.add(l3);
Collection c1=new LinkedList();
TestList l4=new TestList("Sonu1",1011);
TestList l5=new TestList("Monu1",1022);
TestList l6=new TestList("Neha1",1033);
c1.add(l4);
c1.add(l5);
c1.add(l6);
c.addAll(c1);
Iterator itr=c.iterator();
while(itr.hasNext())
{
TestList p=(TestList)itr.next();
System.out.println("Name is :"+p.name+"  RollNo is :  "+p.roll);
}
}
}
Output:-
C:\JAVATECH>javac TestList.java
C:\JAVATECH>java TestList
Name is :Sonu  RollNo is :  101
Name is :Monu  RollNo is :  102
Name is :Neha  RollNo is :  103
Name is :Sonu1  RollNo is :  1011
Name is :Monu1  RollNo is :  1022
Name is :Neha1  RollNo is :  1033

Example2:-TestLL.java
import java.util.*;
class TestLL
{
public static void main(String[]args)
{
LinkedList l=new LinkedList();
l.addFirst(100);
System.out.println(l);
l.addFirst(99);
l.addFirst(90);
System.out.println(l);
l.addLast(105);
l.addLast(110);
System.out.println(l);
System.out.println(l.peek());
System.out.println(l);
System.out.println(l.poll());
System.out.println(l);
System.out.println(l.element());
System.out.println(l);
System.out.println(l.offer(111));
System.out.println(l);
System.out.println(l.offerFirst(20));
System.out.println(l);
System.out.println(l.offerLast(200));
System.out.println(l);
System.out.println(l.pollFirst());
System.out.println(l);
System.out.println(l.pollLast());
System.out.println(l);
}
}
Output:-
C:\JAVATECH>javac TestLL.java
C:\JAVATECH>java TestLL
[100]
[90, 99, 100]
[90, 99, 100, 105, 110]
90
[90, 99, 100, 105, 110]
90
[99, 100, 105, 110]
99

[99, 100, 105, 110]

Comments

Popular posts from this blog

Inner classes

Inner Classes:- class inside class or interface called inner class or nested class. Advantage of inner classes is grouping multiple classes and interfaces into a single class. Another big advantage of inner class is that inner class can happily access all the data or variables of outer class directly. Types of Nested classes:- There are two types of nested classes static and non-static nested classes. (A)Non –static inner classes:- 1)     Member inner class 2)     Anonymous inner class 3)     Local inner class (B)Static nested class Example1:-Outer.java class Outer { class Inner { void disp() { System.out.println("Hello This is Inner class Method"); } } public static void main(String[]args) { Inner in=new Inner(); in.disp(); } } Output:- C:\JAVATECH>javac Outer.java Outer.java:12: error: non-static variable this cannot be referenced from a static context Inner in=new Inner(); ...

Inheritance-4

Example10:- class Student { Student() { System.out.println("This is super class Zero argument constructor"); } Student(String s) { System.out.println("This is super class One argument constructor"); } } class Test extends Student { Test() { super(null); System.out.println("This is sub class Zero argument constructor"); } public static void main(String[]args) { Test t=new Test(); } } Output:- C:\JAVATECH>javac Test.java C:\JAVATECH>java Test This is super class One argument constructor This is sub class Zero argument constructor Example11:- class Student { Student() { System.out.println("This is super class Zero argument constructor"); } Student(String s) { System.out.println("This is super class One argument constructor"); } } class Test extends Student { Test() { super(); super(null); System.out.println("This is su...

Object,Data Hiding

Object:- Object is real time entity exist in the real world , the basic purpose of object is to transfer data from one location to another location.object is small memory , allocated inside primary memory(RAM).in java object created inside heap memory,actually in java RAM memory  is divided into some segment according to the type of variables for example. RAM==>Heap area+Stack area+String constant pool+class area  etc. when object created then inside object only non static class variables occupied memory and initialize with default values. non static class variables also known as instance variable reside into object in heap area,static variables occupied memory in class area at the time of class loading from HARD disk to RAM and all local variables occupied memory in stack area when reaching to variables. object  created from the class then what object contains is depend on class structure or class members.             ...