1. Containers and collections

Containers or collections are classes that let you store and process several objects at once. You already know two kinds of containers: arrays and lists.

Java has several dozen collections, each of which stores elements in its own specific way. Here are a few of them:

Collection Class Description
List
ArrayList
List
LinkedList
Linked list
Vector
Vector
Stack
Stack
Set
HashSet
Set
TreeSet
LinkedHashSet
Queue
PriorityQueue
Queue
ArrayQueue
Map
HashMap
Map/Dictionary
TreeMap
HashTable

The names are somewhat ambiguous here. In most programming languages, all of these data structures are called collections, but not in Java. In Java, some of these classes implement the Collection interface, while others do not.

Accordingly, collections are divided into collections in the broad sense and collections in the narrow sense (only those that implement the Collection interface).

So to avoid confusion, when we speak of collections we mean in the narrow sense of the word, i.e. classes that implement the Collectioninterface. The List, Set and Queue types are all collections. Collections in the broad sense are generally called containers. These include types like Map and arrays.


2. HashSet collection

The HashSet class is a typical set collection. In many ways, it is similar to the ArrayList class. In some ways, it is a more primitive version.

You can create a HashSet object using a statement like:

HashSet<Type> name = new HashSet<Type>();

Where Type is the type of the elements we will store in the HashSet collection.

The HashSet class has methods like this:

Method Description
void add(Type value)
Adds the value element to the collection
boolean remove(Type value)
Removes the value element from the collection.
Returns true if there was such an element
boolean contains(Type value)
Checks whether the collection has a value element
void clear()
Clears the collection, removing all the elements
int size()
Returns the number of elements in the collection

Here's an example of using a set.

Let's write a program that says goodbye to the user if he or she says hello. To make it more interesting, we'll give our program the ability to understand "hello" in several languages.

Code Note
HashSet<String> set = new HashSet<String>();

set.add("Hallo");
set.add("Hello");
set.add("Hola");
set.add("Bonjour");
set.add("Ciao");
set.add("Namaste");

Scanner console = new Scanner(System.in);
String str = console.nextLine();

if (set.contains(str))
   System.out.println("Goodbye!");
Create a HashSet object that stores String elements.


We add greetings in various languages to the set variable.




Read a line from the console.


If the string is in our set of greetings, then we say goodbye.


3. Set

The Set collection is designed to hold a set of elements. That's why it's called a Set (set). This collection has three features.

Operations on a set

There are only three things you can do with a set: add elements to the set, remove elements from the set, and check whether the set contains a specific element. That's it.

No order

Elements in this collection do not have indices. You can't get an element by an index, or write a value to a collection at a specific index. A set has no get() and set() methods.

Unique elements

All elements in a set are unique. Unlike a list, a set can contain only one instance of an element. An object is either in the set or not — there is no third option. You can't add black three times to a set of colors. It is either there or it is not.

Finding elements

When you add a new element, remove an element, or check whether an element exists in a set, a search for the element is performed in the method. The passed element is compared with the elements of the collection first by hashCode(), and then if the values returned by hashCode() match, by equals().



4. Comparing collections: List vs Set

Let's compare two types of collections: List and Set We'll look at the main differences, when one is more advantageous than the other, and vice versa.

Let's try to compare List and Set using toys as an example.

The List (list) collection is like a set of toys arranged along the wall in a playroom. You can add a toy to the end of the list. If you really need to, you can also insert it in the middle (but some of existing toys will have to be moved).

Each toy has a index. You can refer to a toy by its index and also replace toy number 7 with toy number 13. You can remove toy number 4 from the list. Finally, you can learn the index of each toy in the list.

The Set (set) collection is more like a pile of toys in the middle of the floor. You can add a toy to the pile, and you can remove a toy from the pile. But these toys don't have a fixed index associated with them.

Or suppose you're choosing a toy for your child's birthday. First, you think of whether he has the toy already. All the toys that he already has form a set of toys that you won't choose to buy.

From this point of view, you can see that the order of toys in a set of "toys that already exist" does not matter, nor does it matter if the birthday boy has two instances of a particular toy. You are not interested in the order or number of each toy. What you care about is knowing each unique toy that exists in the set.

For cases like this, you need the Set collection. Its most popular implementation is the HashSet class.