0% found this document useful (0 votes)
100 views

Java Collections: - Basim Ahmad

Java collections include lists, sets, maps, and queues. Lists maintain element order and allow duplicates. Sets do not allow duplicates. Maps associate keys with values. Queues process elements in FIFO order. Common implementations include ArrayList, LinkedList, HashSet, TreeSet, HashMap, and PriorityQueue. Users can also create their own collection classes by implementing the Iterable and Iterator interfaces.

Uploaded by

pixar_basim
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views

Java Collections: - Basim Ahmad

Java collections include lists, sets, maps, and queues. Lists maintain element order and allow duplicates. Sets do not allow duplicates. Maps associate keys with values. Queues process elements in FIFO order. Common implementations include ArrayList, LinkedList, HashSet, TreeSet, HashMap, and PriorityQueue. Users can also create their own collection classes by implementing the Iterable and Iterator interfaces.

Uploaded by

pixar_basim
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Java Collections

-Basim Ahmad
Java Collections - Overview

Collections come in four basic flavours:
Lists - Lists of things (classes that implement List).
Sets - Unique things (classes that implement Set).
Maps - Things with a unique ID (classes that
implement Map).
Queues - Things arranged by the order in which
they are to be processed.
But there are sub-flavours within those four
flavors of collections:
Sorted
Unsorted
Ordered
Unordered

Types Of Collections Explained
List Interface

A List cares about the index. The one thing that List has that non-lists don't have is a
set of methods related to the index. Those key methods include things like get(int
index), indexOf(Object o), add(int index, Object obj), and so on.

ArrayList :-
Think of this as a growable array. It gives you fast iteration and fast random access. To
state the obvious: it is an ordered collection (by index), but not sorted.
LinkedList:-
A LinkedList is ordered by index position, like ArrayList, except that the elements are
doubly-linked to one another. This linkage gives new methods (beyond what you get from the
List interface) for adding and removing from the beginning or end, which makes it an easy
choice for implementing a stack or queue. A LinkedList may iterate more slowly than an
ArrayList, but it's a good choice when you need fast insertion and deletion.
Set Interface

A Set cares about uniquenessit doesn't allow duplicates. Your good friend the equals()
method determines whether two objects are identical (in which case only one can be in
the set). The three Set implementations are described in the following sections.

HashSet:-
A HashSet is an unsorted, unordered Set. It uses the hashcode of the object being inserted,
so the more efficient your hashCode() implementation the better access performance you'll get.
Use this class when you want a collection with no duplicates and you don't care about order when
you iterate through it.

LinkedHashSet:-
A LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across
all elements. Use this class instead of HashSet when you care about the iteration order. When you
iterate through a HashSet the order is unpredictable, while a LinkedHashSet lets you iterate through
the elements in the order in which they were inserted.

TreeSet:-
The TreeSet is one of two sorted collections (the other being TreeMap). It uses a Red-Black
tree structure (but you knew that), and guarantees that the elements will be in ascending order,
according to natural order.
Map Interface
A Map cares about unique identifiers. You map a unique key (the ID) to a specific
value, where both the key and the value are, of course, objects. The Map
implementations let you do things like search for a value based on the key, ask for
a collection of just the values, or ask for a collection of just the keys.
HashMap:-
The HashMap gives you an unsorted, unordered Map. When you need a Map and you
don't care about the order (when you iterate through it), then HashMap is the way to go.
LinkedHashMap:-
Like its Set counterpart, LinkedHashSet, the LinkedHash-Map collection maintains
insertion order (or, optionally, access order).
TreeMap:-
You can probably guess by now that a TreeMap is a sorted Map. And you already know
that by default, this means "sorted by the natural order of the elements." Like TreeSet,
TreeMap lets you define a custom sort order (via a Comparable or Comparator) when you
construct a TreeMap, that specifies how the elements should be compared to one another
when they're being ordered.
Queue Interface
A Queue is designed to hold a list of "to-dos," or things to be processed in some way.
Although other orders are possible, queues are typically thought of as FIFO (first-in,
first-out). Queues support all of the standard Collection methods and they also add
methods to add and subtract elements and review queue elements.

PriorityQueue:-
This class is new with Java 5. Since the LinkedList class has been enhanced to
implement the Queue interface, basic queues can be handled with a LinkedList. The purpose
of a PriorityQueue is to create a "priority-in, priority out queue as opposed to a typical FIFO
queue. A PriorityQueue's elements are ordered either by natural ordering (in which case the
elements that are sorted first will be accessed first) or according to a Comparator. In either
case, the elements' ordering represents their relative priority.
public class MyCollection<E> implements Iterable<E>{
public Iterator<E> iterator() {
return new MyIterator<E>();
}
}


Using User Your Own Collections
public class MyIterator <T> implements Iterator<T> {
public boolean hasNext() {
//implement...
}
public T next() {
//implement...;
}
public void remove() {
//implement... if supported.
}
}

Corresponding implementation skeleton of the MyIterator class:
public static void main(String[] args) {
MyCollection<String> stringCollection = new MyCollection<String>();
for(String string : stringCollection){
}
}
Here is how to use the MyCollection generified, and with the new for-loop:
Thank You

You might also like