The Java Collections Framework is a collection of interfaces and classes which helps in storing and processing the data efficiently. This framework has several useful classes which have tons of useful functions which makes a programmer task super easy. I have written several tutorials on Collections in Java. All the tutorials are shared with examples and source codes to help you understand better.
Java Collections – Table of Contents
1. ArrayList
2. LinkedList
3. Vector
4. HashSet
5. LinkedHashSet
6. TreeSet
7. HashMap
8. TreeMap
9. LinkedHashMap
10. Hashtable
11. Iterator and ListIterator
12. Comparable and Comparator
13. Java Collections Interview Questions
Collections Framework hierarchy
Java Collections – List
A List is an ordered Collection (sometimes called a sequence). Lists may contain duplicate elements. Elements can be inserted or accessed by their position in the list, using a zero-based index.
ArrayList
Here is the list of all the tutorials published on the ArrayList.
ArrayList Basics
ArrayList Sorting
- Sort ArrayList
- Sort ArrayList in Descending order
- Sort ArrayList of Objects using Comparable and Comparator
ArrayList Add/Remove
- Add element to ArrayList
- Add element at particular index of ArrayList
- Append Collection elements to ArrayList
- Copy All List elements to ArrayList
- Insert all the collection elements to the specified position in ArrayList
- Remove element from the specified index in ArrayList
- Remove specified element from ArrayList
Get/Search in ArrayList
- Get Sub List of ArrayList
- Get the index of last occurrence of the element in the ArrayList
- Get element from ArrayList
- Get the index of first occurrence of the element in the ArrayList
- Check whether element exists in ArrayList
Other Tutorials on ArrayList
- Compare two ArrayList
- Synchronize ArrayList
- Swap two elements in ArrayList
- Override toString() method – ArrayList
- Serialize ArrayList
- Join two ArrayList
- Clone ArrayList to another ArrayList
- Make ArrayList Empty
- Check whether ArrayList is empty or not
- Trim the Size of ArrayList
- Replace the value of existing element in ArrayList
- Increase the capacity(size) of ArrayList
ArrayList Conversions:
Differences:
LinkedList
Here is the list of all the tutorials published on the LinkedList.
LinkedList Basics
LinkedList Add/Remove
- Adding an element to LinkedList
- Add element at specific index in LinkedList
- Add element at the beginning and end of LinkedList
- Adding an element to the front of LinkedList
- Remove First and last elements from LinkedList
- Remove element from specific index
- Remove specified element from LinkedList
- Remove All elements from LinkedList
- Append all the elements of a List to LinkedList
Get/Search in LinkedList
- Get first and last elements from LinkedList
- Get element from specific index of LinkedList
- Search element in LinkedList
- Get Sub list of LinkedList
LinkedList Iterator/ListIterator
Other Tutorials on LinkedList
- Replace element with a new value in LinkedList
- Check whether a particular element exists in LinkedList
- Clone a LinkedList to another LinkedList
- Get the index of last occurrence of an element in LinkedList
- LinkedList push() and pop() methods
- LinkedList poll(), pollFirst() and pollLast() methods
- LinkedList peek(), peekFirst() and peekLast() methods
Conversion
Vector
Here is the list of all the tutorials published on the Vector.
Vector basics
- Vector in Java
- Get sub list from Vector
- Sort Vector using Collections.sort()
- Search element in Vector using index
- Copy Elements of one Vector to another
Remove/Sort/Replace in Vector
- Remove element from Vector
- Remove element from specified index in Vector
- Remove all elements from Vector
- Replace element in Vector
- Set Vector size
Vector -Iterator/ListIterator/Enumeration
Conversions
Java Collections – Set
A Set is a Collection that cannot contain duplicate elements. There are three main implementations of Set interface: HashSet, TreeSet, and LinkedHashSet. HashSet, which stores its elements in a hash table, is the best-performing implementation; however it makes no guarantees concerning the order of iteration. TreeSet, which stores its elements in a red-black tree, orders its elements based on their values; it is substantially slower than HashSet. LinkedHashSet, which is implemented as a hash table with a linked list running through it, orders its elements based on the order in which they were inserted into the set (insertion-order).
HashSet
Here is the list of all the tutorials published on the HashSet.
- HashSet in Java
- Delete all elements from HashSet
- How to iterate through a HashSet
- Convert a HashSet to an array
- Convert a HashSet to a TreeSet
- Convert HashSet to a List/ArrayList
- HashSet vs HashMap
LinkedHashSet
TreeSet
Java Collections – Map
A Map is an object that maps keys to values. A map cannot contain duplicate keys. There are three main implementations of Map interfaces: HashMap, TreeMap, and LinkedHashMap.
HashMap: it makes no guarantees concerning the order of iteration
TreeMap: It stores its elements in a red-black tree, orders its elements based on their values; it is substantially slower than HashMap.
LinkedHashMap: It orders its elements based on the order in which they were inserted into the set (insertion-order).
HashMap
Here is the list of all the tutorials published on the HashMap.
HashMap Basics
- HashMap in Java
- How to iterate HashMap
- Sort HashMap by Keys and values
- Get Size of HashMap
- Remove Key-value mapping from HashMap
- Remove all mapping from HashMap
- How to check if HashMap is empty or not?
Get/Search in HashMap
Serialize/Synchronize
Differences
Other Tutorials on HashMap
- HashMap Iterator example
- Copy one HashMap to another
- Get value from HashMap using Key
- Get Set view of keys from HashMap
- Clone a HashMap
TreeMap
- TreeMap in Java
- Iterate TreeMap
- Sort TreeMap
- Iterate TreeMap in Reverse order
- Get Sub Map from TreeMap
LinkedHashMap
Hashtable
Java Collections – Iterator/ListIterator
Both Iterator and ListIterator are used to iterate through elements of a collection class. Using Iterator we can traverse in one direction (forward) while using ListIterator we can traverse the collection class on both the directions(backward and forward). To know more differences between these two refer this article: Difference between Iterator and ListIterator.
Comparable and Comparator Interfaces
These interfaces are used for sorting objects of user defined class (custom class).
Collections Interview Questions
Collections Framework Interview Questions
Reference: Official Document on Collections
Hi Sir,
I thank you very much for posting such an awesome java content.
The way you explained the Concepts are very clear.
But I am not able to find topics on List and Set.
Could you please let me know the link for accessing the List and set related topics ?
Rgds,
Manvi
Thank you for your kind words. I have updated the page by providing links of List and Set related tutorials, you can find the same above.
Hi sir,
This site is simply awesome.Thanks for your efforts.
we need more concepts from you like struts,hibernate ….!
Hai Sir,
This site is very useful for learners.I had one doubt, the doubt is, in hashmap and array concept we will display line and bar graph in one page with data input and cluster in JSP.what is the source code .Kindly tell me.Please send source code in my mail.
Thank you.
This site helps me a lot.
it is very easy program .i have understood and you will understand
Hi Chaitanya,
Your content,depth of knowledge in the concepts is simply superb,mind blowing and awesome,i have visited many blogs and sites to learn the concepts in a easily understandable way, but i didn’t find any, but when i came across your site accidentally, i was amazed by way you make readers understand the concepts.
All these years i was feared of looking at the concepts of collections even,but now, after going through your site. That fear in me no more exist!!!! I thanks you from my bottom of heart for your kind knowledge sharing.
Regards,
Sriram.
I was looking for this kind of organized concept based tutorial on the Internet for an hour. indeed I succeeded! thank you sir! do you also have tutorials on JSF, hibernate, EJP, JPA, Spring, struts ? I really need to learn this topics from you if it is available. keep the good work!
Awsome! can you add why we need Iterator? why can’t we use loops(for loop, while loop, do while)?
Hi kalkidan,
Iterator is used to iterate the element in one direction.
It’s having three method.
1. boolean hasNext( )
2. Object next( )
3. void remove( )
The best example you can use Iterator is
List list = new ArrayList();
list.add(object1)
list.add(object2);
Iterator it = list.iterator();
while(it.hasNext() ){
Object obj = (Object)it.next();
}
Not all Collections provide index-based access, so we cannot always use while and do-while. There is an enhanced version of ‘for’ that can be used without index access.
In cases, where there is no index based access (i.e. set), we use an Iterator, otherwise it use your choice to choose the iteration method if the index is available to you.
Great work buddy !!!!
I learned lot of thing from your site. Your explanations are very simple and clear.
keep it up.
best of luck !!!
Good One Sir…
Hello Chaitanya, this tutorial is awesome. Many thanks for your efforts. Do we option to post our java queries in this site? If Yes, please let me know. If not, I think it would be a great enhancement for this site.
Just one word..Brilliant tutorials.You helped me remove my fear over collections. I am thankful to you very much
Keep posting more tutorials
Hi Sir,
the way your explanation is very very good, Beginners book is exactly suitable word for people who are learning new technology this website is 100% perfect for learners
Awesome explanation!!!!!
Thanks for such a simple and deep explanation of all concepts with example
Nice Material
The content is very simple but yet very strong in communicating the concepts. Excellent set of tutorials.
Thanks a lot!!
Hi Chaitanya!
Very good job.One of the best collections examples I have seen till date.Keep up the good work.All the best for all your future endeavors!
where is hashtable ?
Hi Chaitanya
The explanations are simple and clear.I have learnt alot.
Thanks for your efforts!!
I need collection programs in java with all interfaces and classes
Hi,
Kindly suggest whether Vector comes under List interface???
I thought it was a legacy class.
Absolutely awesome site for beginner…..
Most of java interview will ask question on JAVA COLLECTION FRAMEWORK,,, this site helps me lot…. thank you very much….
Really awesome site……each and every concept explained with very easy and simple example .for me you made collection is very easy.
Thank you so much for your efforts :)
By far, this is the best website that I found easy for learning the concepts in Java. All the concepts are explained in a manner that it’s can be grasped quite easily. The topics seem to be very clear.
THANK YOU!! I’ll definitely recommend this site to my friends.
superb…explanation with perfect matching examples. This is the best blog forever for COLLECTIONS concepts compare to others……..
This site is really awesome. Now my all concepts are cleared regarding the Collections. It would be awesome if you provide tutorial for Spring, Struts and Hibernate too.
nice one … very clear and time saving tutorial.. !!!
That’s really good job.Well done.Everything has been expained very clear and the examples has wrapped your theoritical parts in a great way.A good tutorial not only for beginners.Carry on the good job.
In collection framework we have a class called “Stack”, i didn’t find it in diagram can you explain about this.
Hi bro your site really good particularly in collection section. Previously I dont known about collection but after saw your site I learnt collections part from your site. It was very much helpful to me. The same way I expecting Spring, SpringMVC, Hibernate from you.
hello sir …
this tutorial very help full to me . and want to learn spring framework
if you know please tell me or any good spring reference link send to my mail…
thankyou sir….
I just started learning Java . The Problem I faced initially was too many content in the internet. But your example seems to be one stop shop where I can get everything at one place. Keep Uploading the new topics in similar way. Just a advise , why dont you start Youtube channel ,you can explain theoretical concepts also :)
Thanks a lot for such an easy to understand programs and examples.Hats off to you.
Really awsome material and please add hibernate,struts2,springs and ajax.
Thank You for giving a clear & precise understanding of Collections Framework. You have literally removed my fear of Collections. Keep up the Good Work. Thanks once again from the bottom of my heart!!!:)
Probably your best source for information on the Stack class is Oracle.com for the version of Java you are using. It will act similar to the other collection classes described here.
P.S. Nice job of explaining collection classes. I really like ArrayList because it dynamically grows as elements are added, eliminating array out of bounds exceptions. Thanks, Brent
Sir i really like ur site and its content, everything is very organized and detailed, every topic is being covered with fine details. for a beginner this site is very useful,i request u sir to please introduce such type matter for data structure also.please
hi sir
your site is simply superb and easy to learn java! especially collections are very easy to understand and examples are really awesome! compared to other
sites this one is very nice and easy to learn! :)
if collection is an interface and list also being an interface then..what i understand is list implements collection …but i also know that interface cannot implement another interface…than what is the justification for this?
List is interface .
Collection is interface .
one interface always extends another interface .
so List extends Collection
Thank you SO much! Literally saved me from failing my midterm.
Awesome material!!!
is map an object or interface?
map is a interface…
Hi ,
Hash Map can contain duplicate key values. Please correct it.
Great Job !! The Beginner’s book is one of the best sources to learn and make concepts clear.Thank you very much for your efforts.
Swetha.
HashMap can contain only one null key (and that key cannot have any value, because you can’t access the null key!). So having a null key as good as useless.
Hi,
This is awesome site :) the way you have explained all concepts are amazing…tysm
hello….sir
your tutorial is nice,can you tell me where is the use of collection in project and how?
You are the best :) thanks for this tutorials
Hi Chaitanya, this beginners book has helped me a lot to grasp java concepts so easily. Your passion for java is making way for lots to understand these vast/complex java concepts more easily. You have structured tutorials for each and every topics soo nicely that only after reading this we can get clear understanding of each and every topic to implement them in practice. Thanks a lot for putting your passion into internet where it is helping us to grow our career.
It is nice..can we use arraylist inside hashmap?
Hello All,
Could you please provide me link to follow the Spring Framework tutorials.
Thanks and Regards,
Uma Shankar Gupta
Hai Chaitanya,
Iam a HardCore fan of this Beginners Book . I would like to know about Colletion in depth. Queue Interface and some of the concepts are not provided.Could you please Provide all Collection Topics.
Thanks&Regards
Uday Kumar Mashetti.
Hi Chitanya,
Awesome explanation on collections…looking forward for explanations about ConcurrentHashMaps,etc thru you.
Thanks
Priya M
Hello Chaitanya,
You have not provided tutorials for “HASHTABLE”.
Could you please provide the same?
Regards,
Rajnayan
Hi Chaitanya,
Please correct this – TreeMap: It stores its elements in a red-black tree, orders its elements based on their VALUES.
It should be “orders its elements based on their KEYS”
Excellent article, thanks a ton!
Add hastable to the content and we are done. Great work though! :)
HI Chaitanya
You did a brilliant job on Beginnersbook’s Collection Framework. I appreciate you for such a best study content.
Thank you Sir.
why we need listIterator can you please explain?
Hi, This site contents are very clear and given neat explanation about collection framework. As a beginner, This site could be very useful. thank you for your effort to making this blog and it will be very helpful us to learn about collection. We were expecting more updates on collection framework.
Thank you!..
Best online site….but in collections chapter Queue Interface is missing…plz upload it also…thanks