Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
17 views

Complete Java Collection Tutorial For The Beginner

Uploaded by

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

Complete Java Collection Tutorial For The Beginner

Uploaded by

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

Complete Java Collection tutorial for the beginner

posted by Jitendra on April 15, 2011 under category JAVA and tagged as JAVA with 16 Comments

Complete JAVA Collection tutorial for the beginner

Collection represents the group of objects. Depending on the method of storing and retrieving, collections are
basically divided into three parts – Set, Map and List. Where Set does not contain duplicate values, Map
contains key value type of data whereas List can have a duplicate values stored in it sequentially. This
framework is provided in “java.util” package. Collection is the parent interface of all collections in java.

Following list describes the core collection interfaces.

Java Collection Interfaces


Collection “” the root of the collection hierarchy. A collection represents a group of objects known as its
elements. Some types of collections allow duplicate elements, and others do not. Some are ordered and others
are unordered. The Java platform doesn’t provide any direct implementations of this interface but provides
implementations of more speci c sub interfaces, such as Set and List.

Set “” a collection that cannot contain duplicate elements

List “” an ordered collection (sometimes called a sequence). Lists can contain duplicate elements. The user of
a List generally has precise control over where in the list each element is inserted and can access elements by
their integer index (position).

Queue “” a collection used to hold multiple elements prior to processing. Besides basic Collection operations,
a Queue provides additional insertion, extraction, and inspection operations.
Queues typically, but do not necessarily, order elements in a FIFO ( rst-in, rst-out) manner. Among the
exceptions are priority queues, which order elements according to a supplied comparator or the elements’
natural ordering.

Map “” an object that maps keys to values. A Map cannot contain duplicate keys; each key can map to at most
one value. If you’ve used Hashtable, you’re already familiar with the basics of Map.

Concrete Classes :
Java Collection interfaces and concrete classes

HashSet :
This class implements the Set interface, backed by a hash table (actually a HashMap instance). It makes no
guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain
constant over time. This class permits the null element.

LinkedHashSet :
Hash table and linked list implementation of the Set interface, with predictable iteration order. This
implementation di ers from HashSet in that it maintains a doubly-linked list running through all of its
entries. This linked list de nes the iteration ordering, which is the order in which elements were inserted
into the set (insertion-order). Note that insertion order is not a ected if an element is re-inserted into the
set. (An element e is reinserted into a set s if s.add(e) is invoked when s.contains(e) would return true
immediately prior to the invocation).
Interface SortedSet:
A set that further guarantees that its iterator will traverse the set in ascending element order, sorted
according to the natural ordering of its elements (see Comparable), or by a Comparator provided at sorted set
creation time. Several additional operations are provided to take advantage of the ordering. (This interface is
the set analogue of SortedMap).

TreeSet:
This class implements the Set interface, backed by a TreeMap instance. This class guarantees that the sorted set
will be in ascending element order, sorted according to the natural order of the elements (seeComparable), or by
the comparator provided at set creation time, depending on which constructor is used.

ArrayList:
Resizable-array implementation of the List interface. Implements all optional list operations, and permits all
elements, including null. In addition to implementing the List interface, this class provides methods to
manipulate the size of the array that is used internally to store the list.

Vector:
This class is roughly equivalent to ArrayList except it is Synchronized.

LinkedList:
Linked list implementation of the List and Queue interfaces. Implements all optional operations, and permits
all elements (including null).
JAVA Map interface and concrete classes

HashMap:
The HashMap class is roughly equivalent toHashtable, except that it is unsynchronized and permits nulls.

HashTable:
Hash table based implementation of the Map interface. This implementation provides all of the optional map
operations, and permits null values and the null key. This class makes no guarantees as to the order of the map;
in particular, it does not guarantee that the order will remain constant over time.

LinkedHashMap:
Hash table and linked list implementation of the Map interface, with predictable iteration order. This
implementation di ers from HashMap in that it maintains a doubly-linked list running through all of its
entries. This linked list de nes the iteration ordering, which is normally the order in which keys were
inserted into the map (insertion-order). Note that insertion order is not a ected if a key is re-inserted into
the map.
TreeMap:
This class guarantees that the map will be in ascending key order, sorted according to the natural order for the
key’s class (seeComparable), or by the comparator provided at creation time, depending on which constructor
is used.

Note :
To synchronize the Set Concrete classes, use below code:

1 Set s = Collections.synchronizedSet(CONCREATECLASS_OBJECT);

To Synchronize the List Concreate classes :

1 List list = Collections.synchronizedList(CONCREATECLASS_OBJECT);

Collection Summary Chart

Share this post

        1

Related posts
Create Excel File in Java using Apache POI Library
Creating First Application in Heroku using Eclipse
Merge PDF in Salesforce Using Java, ITextPDF and OAuth 2
Simple guide to setup SSL in Tomcat
Complete Salesforce Deployment Guide using Ant Migration Tool
Pass by Value and Pass by Reference
How to create Immutable Class in Java
Create SOAP message using Java
Quartz framework tutorial with example – Schedule job in Java
Step By Step Hibernate Tutorial Using eclipse WTP

16 Comments Jitendra's Blog  Login

Sort by Best
 Recommend ⤤ Share

Join the discussion…

LOG IN WITH
OR SIGN UP WITH DISQUS ?

All about Web Name


and Cloud

Email

Password

By signing up, you agree to the Disqus Basic Rules, Terms of Service, and Privacy Policy.

I'd rather post as a guest →

lin • 5 years ago


your diagram of hash table and description are different. In description you say it permits nulls.
I'm tired but I think I am reading it right.
7△ ▽ • Reply • Share ›

Hanumant Bandgar • 5 years ago


Really nice for Developer
4△ ▽ • Reply • Share ›

Rahul • 5 years ago


great article !!!
3△ ▽ • Reply • Share ›

Pmvignes • 5 years ago


Good to see all the basic information in the single page.
2△ ▽ • Reply • Share ›

Jitendra Zaa Mod > Pmvignes • 5 years ago


Thanks
△ ▽ • Reply • Share ›

Java Training In Chennai • 5 years ago


It is not type safe collection. Currently java supports type safe collections (generics)
1△ ▽ • Reply • Share ›

Jitendra Zaa Mod > Java Training In Chennai • 5 years ago


All above are Type Safe only
△ ▽ • Reply • Share ›

vasantha aarabhi • 5 years ago


really usefull and easy to understand. thank you
1△ ▽ • Reply • Share ›

hemalathav • 5 years ago


ur notes is amazing
1△ ▽ • Reply • Share ›
Jitendra Zaa Mod > hemalathav • 5 years ago
Thanks Hemal
1△ ▽ • Reply • Share ›

xlkaksje todd asde • 6 years ago


Perfect work you have done, this web site is really cool with good info.
1△ ▽ • Reply • Share ›

Daniel Charlie • 4 months ago


nice article
△ ▽ • Reply • Share ›

hasi_wk • 3 years ago


thank you sire
△ ▽ • Reply • Share ›

RAGHU • 3 years ago


Thank you very much for the information and explanation with images.

Hashtable wont allow null as a key and null as a value even, it throws the NullPointerException in both the scenarios for
example when we try to put(null,123) or put(123,null) but the same combination works with Hashmap
△ ▽ • Reply • Share ›

yesh • 4 years ago


really easily learn collection this website
nice website
△ ▽ • Reply • Share ›

vikas goyal • 5 years ago


Good tutorial
△ ▽ • Reply • Share ›

ALSO ON JITENDRA'S BLOG


Summer 16 – Process builder with multiple decision Behold Yourself – Summer 17 is here to break legacy
criteria Visualforce Navigation code
5 comments • a year ago• 6 comments • 4 months ago•
AvatarJitendra Zaa — Fixed it.. Thanks for pointing it out AvatarJitendra Zaa — Solution not working yet. I have case reopened
till date !!!

Use Hierarchy custom settings to avoid hard coding in Use Lightning Component in Visualforce Page
formula field, custom … 7 comments • a year ago•
1 comment • 2 months ago• Avatarchandrakanth chandu — Hi Jitendra,I have a working app (
AvatarHariKrishna Sompalli — Thanks for this @Jitendra Created using Salesforce trail head :
https://trailhead.salesforc... .Now i …

✉ Subscribe d Add Disqus to your siteAdd DisqusAdd 🔒 Privacy

Since 2009 By Jitendra Zaa (formerly Shivasoft.in)

You might also like