When to use Java collections?

If you are just starting out with Java, you may have heard of the Java collection classes. So, what are they and when are they useful?

Collection classes are basically data structures

If you’re not familiar with the Java collection classes – a good way to think of the classes is as an easy way for programmers to use data structures that are built into Java itself. So, if you are writing a program in Java and need to use something like a hashtable, then Java provides classes (in the Collection classes) which you simply add data to – without you having to worry about the implementation details of that data structure – like how that data is stored, retrieved etc. You just use the methods provided with the classes to perform actions on your data structures.

What is a collection?

Now, let’s talk a little bit more about the term collection itself. A collection is an object used to hold data, and is sometimes also called a container. A collection typically supports storage, retrieval/insertion, and iteration.

Is an array a collection in Java?

While arrays are not part of the Java collection classes, they are collections in the sense that they store data and also provide the key operations of insert, retrieve, and iterate that collections usually do support. Arrays could be said to be the only collection that is built into the Java language.

Arrays do have the advantages of being easy to use, simple to create, and they also provide efficient access to elements. But, some of the disadvantages of arrays include: inserting into the middle of an array means that the rest of the data has to be shifted in order to create space for the new element, they only store primitive data types, and arrays are also a fixed size.

Fixed size of arrays is a major disadvantage

The limitation of fixed size is a big disadvantage of arrays and is one of the main reasons why the Java collection classes (as part of the standard library) are preferred over arrays – since they provide more sophisticated collections.

Example of Java collection classes

The Java collections classes are inside the java.util package. These are some example classes inside the Java library:

Set classes: HashSet, TreeSet
List classes: LinkedList, ArrayList
Map classes: HashMap, TreeMap

When would you actually use a collections class in Java?

Collections classes are used extensively by professional Java programmers. For example, let’s say you have some data and you think it would make sense to store that data in a hashtable. You could write the code for a hashtable data structure in Java yourself (which you may have done as an exercise in a computer programming class in school), but that would not make any sense when you could just use the HashMap class provided by the Java collections framework. You can then add your data to the HashMap by using the “put” method, and retrieve data from the “get” method – and you don’t have to worry about what goes on when you call those methods, because Java has already provided implementations for those. There are of course other methods provided that allow you to do other things to a HashMap instance – just take a look at the HashMap methods in the Oracle documentation.

How is memory managed in the Java collections classes?

The collection classes take care of all memory management issues internally. Because there is no fixed size limitation (as there is with arrays), maps, sets, and lists all grow automatically to provide space for whatever data is added to them.

What kind of data can i put inside the collection classes?

If you want to use a hashtable in Java, you can use the HashMap collection class. But what kind of data can you actually put into a HashMap – or any other collection class for that matter? Well, the collections classes are generic – which means that they can store data of type Object reference. This means that the collections can hold any class type or any primitive type – but primitive types can only be used with the help of the primitive type wrapper classes.

The decision to use Object references to make the collection classes generic was considered controversial. There are two commonly discussed disadvantages to this approach: 1. The use of wrapper classes is required to store primitive types and 2. It’s possible to create mixed type collections.

Follow Varoon Sahgal, author, on Google Plus