Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

Which Collection should I use in my Java program? Part 1. Hashtable vs hashmap?

JavaFAQ Home » General Java Go to all tips in General Java


Bookmark and Share

Which Collection should I use in my Java program? Hashtable vs hashmap? Hashmap vs hashset? Hashmap vs hashtable? hashmap vs arraylist? More? I compared all the collections in my table below! Oooh...

All the these questions I asked myself at least 10 times, if not even more. And I have not found any good Java Collections overview page on the Internet, where I could pick one collection and compare with another in one page, without going to multiple places.

This Comprehensive Java Collections overview page was the result of my half-day work. I think that the info I have now in two tables below can be good enough to make fast and right choice when you do not know which Java collection can be used in your application.

I hope that I have not done serious mistakes, if you find them, please inform me and I will correct the page. At the bottom of tables you can find Java code examples for all the collections described below.

General-purpose Implementations
Interfaces Implementations
  Hash table Resizable array Tree Linked list Hash table + Linked list
Set HashSet   TreeSet   LinkedHashSet

 

List   ArrayList

Vector

  LinkedList  
Queue          
Map HashMap

Hashtable

  TreeMap   LinkedHashMap

 

http://JavaFAQ.nu

Set: A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction.

Map: cannot contain duplicate keys; each key can map to at most one value.

The Map interface provides three collection views, which allow a map's contents to be viewed as a set of keys, collection of values, or set of key-value mappings. The order of a map is defined as the order in which the iterators on the map's collection views return their elements. Some map implementations, like the TreeMap class, make specific guarantees as to their order; others, like the HashMap class, do not.

46 Java Free lessons course on our site!

All Java Lessons contents page | Java Lesson 1 | Java Lesson 2 | Java Lesson 3 | Java Lesson 4 | Java Lesson 5 | Java Lesson 6 | Java Lesson 7 | Java Lesson 8 | Java Lesson 9 | Java Lesson 10 | Java Lesson 11 | Java Lesson 12 | Java Lesson 13 | Java Lesson 14 | Java Lesson 15 | Java Lesson 16 | Java Lesson 17 | Java Lesson 18 | Java Lesson 19 | Java Lesson 20 | Java Lesson 21 | Java Lesson 22 | Java Lesson 23 | Java Lesson 24 | Java Lesson 25 | Java Lesson 26 | Java Lesson 27 | Java Lesson 28 | Java Lesson 29 | Java Lesson 30 | Java Lesson 31 | Java Lesson 32 | Java Lesson 33 | Java Lesson 34 | Java Lesson 35 | Java Lesson 36 | Java Lesson 37 | Java Lesson 38 | Java Lesson 39 | Java Lesson 40 | Java Lesson 41 | Java Lesson 42 | Java Lesson 43 | Java Lesson 44 | Java Lesson 45 | Java Lesson 46

Important! Although Array  is not class in Java API, I have included it into the table below, because it is similar to collections in terms of storage. It can be used as a simplest "collection type" Smile.

That's why some functions and properties are not applicable to Array in the table below (marked as N/A).

Java Collections Comprehensive Overview Table, Part 1

  HashSet HashMap ArrayList TreeSet TreeMap LinkedList LinkedHashSet LinkedHashMap
Random
access
no no yes no no yes no no
Serializable yes yes yes yes yes yes yes yes
Synchronized no no no no no no no no
guarantees to the iteration order no no yes yes, yes yes yes yes
constant order over time no no yes,
see note 6
yes,
see note 7
yes,
see note 7
yes yes yes
permits null element yes yes,

null key,
null value

yes no no yes yes yes
is iteration
time proportional to size?****
yes note 1 yes almost,
see note 6
no,
log(n)
no,
log(n)
2n,
see note 8
yes,
just slightly below that of HashSet
yes
Is fail-fast? note 2 yes yes yes yes yes yes yes yes
is resizable? yes yes yes yes yes yes yes  
Only unique elements
(no duplicates)?
 
yes yes no,
allow duplicate elements
yes yes no yes yes
Notes   The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls. This class is roughly equivalent to Vector, except that it is unsynchronized.     Implements all optional list operations.
Implements the Queue interface, providing first-in-first-out queue operations for add, poll, etc.
- FIFO semantics
LinkedHashSet: Hash table and linked list implementation of the Set interface, with predictable iteration order. This implementation differs from HashSet in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is the order in which elements were inserted into the set (insertion-order). Note that insertion order is not affected if an element is re-inserted into the set. LinkedHashMap: Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order). Note that insertion order is not affected if a key is re-inserted into the map.
Since Java
version
1.2 1.2 1.2 1.2 1.2 1.2 1.4 1.4
Java Code Examples code examples for HashSet code examples for HashMap code examples for ArrayList code examples for TreeSet code examples for TreeMap code examples for LinkedList code examples for LinkedHashSet code examples for LinkedHashMap
Always visit http://JavaFAQ.nu when you need more info about Java!

To be continued


 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy