Collection

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 11

Sorting Collections

ArrayList:
it is an ordered collection (by index), but not
sorted.

A Vector is basically the same as an


ArrayList, but Vector methods are
synchronized
for thread safety.
LinkedList:
A LinkedList is ordered by index
position, like ArrayList, except
that the elements are doubly-linked
to one another.
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.
The three Set implementations are
described in the following
HashSet:
A HashSet is an unsorted, unordered
Set. It uses the hashcode of the
object being inserted
LinkedHashSet:
A LinkedHashSet is an ordered
version of HashSet that
maintains a doubly-linked List across
all elements

TreeSet: The TreeSet is one of two


sorted collections (the other being
TreeMap).

Map Interface
A Map cares about unique identifiers.

HashMap :
The Hash Map gives you an unsorted,
unordered Map.
HashMap allows one null key and
multiple null values in a collection.
Hashtable is the synchronized
counterpart to HashMap.
Tree Map is a sorted Map.

Like TreeSet, TreeMap lets you define


a custom sort order (via a
Comparable or Comparator) when

LinkedHashMap Like
counterpart, LinkedH
LinkedHashMap collection maintain
order (or, optionally, ac
Although it
will be somewhat slowe
HashMap for adding an
elements, you can
expect faster iteration w
LinkedHashMap.
TreeMap You can pro
by now that a TreeMa
Map.

when you use a class that


implements Map, any classes that
you
use as a part of the keys for that map
must override the hashCode() and
equals()
methods
you don't
synchronize a class, so when we say
that Vector and Hashtable are
synchronized, we
just mean that the key methods of
the class are synchronized.

1. Use the hash Code() method to find


the correct bucket
2. Use the equals() method to find the
object in the bucket

You might also like