Collec in Java
Collec in Java
S
Classes with Description
N
AbstractCollection
1 Implements most of the Collection
interface.
AbstractList
2 Extends AbstractCollection and
implements most of the List interface.
3 AbstractSequentialList
Extends AbstractList for use by a
collection that uses sequential rather
than random access of its elements.
LinkedList
4 Implements a linked list by extending
AbstractSequentialList.
ArrayList
5 Implements a dynamic array by
extending AbstractList.
AbstractSet
6 Extends AbstractCollection and
implements most of the Set interface.
HashSet
7 Extends AbstractSet for use with a
hash table.
LinkedHashSet
8 Extends HashSet to allow insertion-
order iterations.
9 TreeSet
Implements a set stored in a tree.
Extends AbstractSet.
AbstractMap
10
Implements most of the Map interface.
HashMap
11 Extends AbstractMap to use a hash
table.
TreeMap
12
Extends AbstractMap to use a tree.
WeakHashMap
13 Extends AbstractMap to use a hash
table with weak keys.
LinkedHashMap
14 Extends HashMap to allow insertion-
order iterations.
IdentityHashMap
Extends AbstractMap and uses
15
reference equality when comparing
documents.
LinkedList Class:-
The LinkedList class extends
AbstractSequentialList and implements the List
interface. It provides a linked-list data structure.
boolean add(Object o)
2
Appends the specified element to the end of this list.
3 boolean addAll(Collection c)
Appends all of the elements in the specified
collection to the end of this list, in the order that they
are returned by the specified collection's iterator.
Throws NullPointerException if the specified
collection is null.
void addFirst(Object o)
5
Inserts the given element at the beginning of this list.
void addLast(Object o)
6
Appends the given element to the end of this list.
void clear()
7
Removes all of the elements from this list.
Object clone()
8
Returns a shallow copy of this LinkedList.
boolean contains(Object o)
Returns true if this list contains the specified
9 element. More formally, returns true if and only if this
list contains at least one element e such that (o==null
? e==null : o.equals(e)).
Object getFirst()
1
Returns the first element in this list. Throws
1
NoSuchElementException if this list is empty.
Object getLast()
1
Returns the last element in this list. Throws
2
NoSuchElementException if this list is empty.
int indexOf(Object o)
1 Returns the index in this list of the first occurrence of
3 the specified element, or -1 if the List does not
contain this element.
int lastIndexOf(Object o)
1 Returns the index in this list of the last occurrence of
4 the specified element, or -1 if the list does not
contain this element.
Object removeFirst()
1
Removes and returns the first element from this list.
8
Throws NoSuchElementException if this list is empty.
Object removeLast()
1
Removes and returns the last element from this list.
9
Throws NoSuchElementException if this list is empty.
2 int size()
1 Returns the number of elements in this list.
Object[] toArray()
2 Returns an array containing all of the elements in this
2 list in the correct order. Throws NullPointerException
if the specified array is null.
Object[] toArray(Object[] a)
2 Returns an array containing all of the elements in this
3 list in the correct order; the runtime type of the
returned array is that of the specified array.
Example:
The following program illustrates several of the methods supported by LinkedList:
import java.util.*;
ArrayList(Collection c)
ArrayList(int capacity)
Apart from the methods inherited from its parent classes, ArrayList defines following
methods:
S
Methods with Description
N
boolean add(Object o)
2
Appends the specified element to the end of this list.
boolean addAll(Collection c)
Appends all of the elements in the specified
collection to the end of this list, in the order that they
3
are returned by the specified collection's iterator.
Throws NullPointerException if the specified
collection is null.
5 void clear()
Removes all of the elements from this list.
Object clone()
6
Returns a shallow copy of this ArrayList.
boolean contains(Object o)
Returns true if this list contains the specified
7 element. More formally, returns true if and only if this
list contains at least one element e such that
(o==null ? e==null : o.equals(e)).
int indexOf(Object o)
Returns the index in this list of the first occurrence
10
of the specified element, or -1 if the List does not
contain this element.
int lastIndexOf(Object o)
Returns the index in this list of the last occurrence of
11
the specified element, or -1 if the list does not
contain this element.
Object remove(int index)
Removes the element at the specified position in this
12
list. Throws IndexOutOfBoundsException if index out
of range (index < 0 || index >= size()).
int size()
15
Returns the number of elements in this list.
Object[] toArray()
Returns an array containing all of the elements in
16
this list in the correct order. Throws
NullPointerException if the specified array is null.
Object[] toArray(Object[] a)
Returns an array containing all of the elements in
17
this list in the correct order; the runtime type of the
returned array is that of the specified array.
void trimToSize()
18 Trims the capacity of this ArrayList instance to be
the list's current size.
Example:
The following program illustrates several of the
methods supported by ArrayList:
import java.util.*;
public class ArrayListDemo {
public static void main(String args[]) {
// create an array list
ArrayList al = new ArrayList();
System.out.println("Initial size of al: "
+ al.size());
// add elements to the array list
al.add("C");
al.add("A");
al.add("E");
al.add("B");
al.add("D");
al.add("F");
al.add(1, "A2");
System.out.println("Size of al after
additions: " + al.size());
// display the array list
System.out.println("Contents of al: " +
al);
// Remove elements from the array list
al.remove("F");
al.remove(2);
System.out.println("Size of al after
deletions: " + al.size());
System.out.println("Contents of al: " +
al);
}
}