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.*;
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
Post a Comment