TIL: Java Collections
Today we’re going to start looking at collections and why they’re useful.
I’ve moved on to a new course, this time dealing with
Collections and all of the various implementations. Today we’re going to focus on the basics of the base
Collection interface and the capabilities shared across all of the different implementations.
What are Collections?
Typically when we’re working with data, we have a need to work with more than just a single element at a time. When we need to work with more than just one element at a time, we need some way of keeping these elements organized and in a single place. So, like its name implies,
Collections allow you to collect all of your items in one place.
Before Collections were introduced, options of grouping data was quite limited. There were Arrays, Vectors, or Hashtables. Each of these had their own implementations and no common interface. That made working with them more difficult as each one had their own way of doing things.
Collections aimed to alleviate all of that by providing a common interface for each of the Collection types to inherit from. There are two “base” interfaces that are related to collections:
Map. Each of these Interfaces have their own Interfaces that extend the functionality for different implementations.
Below is a good picture that shows relationship between Interfaces and their Implementation classes (source credit to geeksforgeeks.org):
As you can see, there are a lot of choices when using collections. The one that you end up using will depend on the problem that you’re trying to solve. The following chart (credit to Richard Warburton’s pluralsight course) provides a nice flow for deciding when to use what:
What can we do with Collections?
Like I mentioned before, today we’re just going to be focusing on the
Collection interface and the methods that all Collection implementations share. These are a pretty solid grouping of methods that allow us to do some pretty handy things:
size()- number of elements in collection
isEmpty()- boolean response if collection has no elements
add(element)- adds element at beginning of collection
addAll(collection)- adds all elements from one collection to another
remove(element)- remove specific element
removeAll(collection)- removes all elements in collection from another
retainAll(collection)- Remove all the elements not present in the collection
contains(element)- boolean if element is present in collection
containsAll(collection)- boolean if all collection elements in collection
clear()- remove all elements from the collection
Just looking at the list you can see how all of these would be useful when trying to work with sets of data.
In order to try to illustrate how useful each of them can be, I went ahead and created a test case in today’s repo with a bunch of different collection operations, but I’ll add them here as well:
So that’s a brief look at Collections. I’ll be going over each of the implementations in the coming days as I make my way through the course. Initial thoughts: these are sure a lot better than working with Arrays. I’m already sold.