Zach Cochran
by Zach Cochran
3 min read

Categories

Tags

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: Collection and 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): https://www.geeksforgeeks.org/collections-in-java-2/

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: Collection flow chart

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:

   Product table, chair, lamp;
   Collection<Product> products;

   @Before
   public void setUp() {
       table = new Product(45, "Old Table");
       chair = new Product(10, "Hard Chair");
       lamp = new Product(5, "Dim Lamp");
       products = new ArrayList<Product>();
   }

   @Test
   public void testCollectionSize() {
       products.add(table);
       products.add(lamp);
       Assert.assertEquals("Wrong number of items", 2, products.size());
   }

   @Test
   public void testCollectionIsEmpty() {
       Assert.assertTrue(products.isEmpty());
   }

   @Test
   public void testCollectionIsEmptyFalse() {
       products.add(table);
       Assert.assertFalse(products.isEmpty());
   }

   @Test
   public void testAddAllToCollection() {
       products.add(table);
       products.add(chair);

       Collection<Product> moreProducts = new ArrayList<Product>();
       moreProducts.add(lamp);
       moreProducts.add(table);

       products.addAll(moreProducts);

       Assert.assertEquals(4, products.size());
       Assert.assertEquals(2, moreProducts.size());
   }

   @Test
   public void testRemoveElement() {
       products.add(chair);
       products.add(table);

       Assert.assertEquals(2, products.size());

       products.remove(table);

       Assert.assertEquals(1, products.size());
       Assert.assertFalse(products.contains(table));
   }

   @Test
   public void testRemoveAllElements() {
       products.add(table);
       products.add(chair);

       Collection<Product> moreProducts = new ArrayList<Product>();
       moreProducts.add(lamp);
       moreProducts.add(table);

       products.removeAll(moreProducts);

       Assert.assertEquals(1, products.size());
       Assert.assertFalse(products.contains(table));
   }

   @Test
   public void testRetainAllElements() {
       products.add(table);
       products.add(chair);

       Collection<Product> moreProducts = new ArrayList<Product>();
       moreProducts.add(lamp);
       moreProducts.add(table);

       products.retainAll(moreProducts);

       Assert.assertEquals(1, products.size());
       Assert.assertTrue(products.contains(table));
   }

   @Test
   public void testContainsAllElements() {
       products.add(table);
       products.add(chair);

       Collection<Product> moreProducts = new ArrayList<Product>();
       moreProducts.add(lamp);
       moreProducts.add(table);

       Assert.assertFalse(products.containsAll(moreProducts));
   }

   @Test
   public void testClearAllElements() {
       products.add(table);
       products.add(chair);
       products.add(lamp);

       Assert.assertEquals(3, products.size());

       products.clear();

       Assert.assertEquals(0, products.size());
   }

Closing thoughts

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.

💚 A.B.L.