Docsity

Java Collections: A Comprehensive Guide to Lists, Sets, Maps, and Queues - Prof. Tushar, Lecture notes of Java Programming

Partial preview of the text

Download Java Collections: A Comprehensive Guide to Lists, Sets, Maps, and Queues - Prof. Tushar and more Lecture notes Java Programming in PDF only on Docsity!

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

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 uniqueness—it 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.

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.

Thank You 