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

Introduction of Java

Java is a high level programming language which is used for creating various types of software's or applications. Types of java applications:- 1)     Stand alone application 2)     Desktop based application          3)     Web based application 4)     Enterprise application etc. Stand alone application:- Stand alone application run on single machine without user interface and every stand alone application having main method. Desktop based application:- Desktop based application also run on single machine with user interface and every Desktop based application having main method. Web based application:- Web based application run on web, web based application not required main method but for such types of application need server. Enterprise application:- Enterprise application is category of web application where doing some business or involve some money related issues. Java Editions: - There are four Java platforms. ·       

Small concept about OOPs

Some small concept about OOPs:- In many books and website write about OOPs concepts as hundred percent object oriented, pure object oriented and partial object oriented language. Hundred percent object oriented:- In the hundred percent object oriented movie hero is object, without object program is incomplete that means can not execute program without object. Without class and object programming or application development is not possible. Example of hundred percent object oriented language is  Smalltalk . Pure object oriented:- In the object oriented cinema obviously object is the hero. In pure object oriented language object having same importance but in this having some relax that is without creating object we can successfully execute our application. So in the object oriented programming without class we cannot create or run our application, class is must. Example of pure object oriented language is  Java . Partial object oriented:- In this type of language cl

java print

Syntax of main method in java is    public static void main(String[]args)  before understanding about main method we have to understand that who is responsible for main method.if we write a simple program or write a simple java class without main method like      Test.java           class Test      {      } above program will compile successfully.for compilation of  java file use  javac tool  or  javac.exe  file. javac Test.java it means compiler checks only syntax of class that means compiler does not check the main method in the file or program,so that compiler is not responsible for main method in java. After successfully compilation of above program we have to run the generated byte code. java Test Error: Main method not found in class Test, please define the main method as:    public static void main(String[] args) So JVM (java virtual machine) or java interpreter is responsible for main method,first jvm check that the main method is pres