Get startedGet started for free

Sets, Queues

1. Sets, Queues

With `List` types under our belt, let's expand our knowledge of the Collections Framework by exploring `Set` and `Queue`.

2. Set interface

`Set`, like `List`, is a kind of Collection. However, a `Set` does not allow duplicates and objects are not ordered. Think of a `List` like a pill box or organizer for objects. Each object has a specific index in the organizer. A `Set` in contrast is like a sack for objects. Objects can be held in the sack, but there is no order or index to the objects in the sack.

3. List vs Set

`List` and `Set` have different pros and cons. `Set` guarantees uniqueness of its elements which makes them faster when searching for an object. However, `Set` does not maintain order. We see `Set` used in applications doing lots of checking for new members in caching pools. For example keeping track of unique words in a document.

4. List vs Set

`List`, on the other hand, preserves order and allows for duplicates. They are faster at adding/deleting elements and accessing an element by index, but slower on lookups. Use `List` when its important to preserve order like managing a sequence of steps or the songs in a playlist.

5. Set implementation

There are various implementation of the `Set` interface. `HashSet`, one of the most popular implementations, is an unordered bag of objects. A `HashSet` also allows for holding one `null`. `HashSet` are generally faster than other `Set` implementations at inserts, deletes and searching/lookups but do use a bit more memory.

6. HashSet construction

Similar to creating a `List` or a `Set`, to create a `HashSet`, use `new` and the generic type syntax to tell Java the types of objects to be stored in the `HashSet`. `HashSet` is found in the`java.util` package requiring we import when using it.

7. HashSet methods

There are common operations on any `Set`. We use `.add()` and `.remove()` to add/remove objects. Because there is no order, we use `.remove()` and then `.add()` to replace an object in a `Set`. As uniqueness is guaranteed, duplicate adds are ignored. `.contains()` checks if an object is already in the `Set`. A single null is allowed and there is no apparent order to a `Set` as seen by the printout of this example `HashSet`.

8. Queue interface

A `Queue` is a data structure that orders objects based on insertions and deletions. It behaves like a line at a ticket booth, removing objects in the same order they are added. Objects are added to the end, or tail, of the `Queue` and are removed from the start, or head, of the `Queue`. There are several implementations of the `Queue` interface.

9. ArrayBlockingQueue

One of the popular `Queue` implementations is the `ArrayBlockingQueue` that is found in `java.util.concurrent` package. Under the covers, it is implemented as an array.

10. ArrayBlockingQueue construction

When creating an `ArrayBlockingQueue`, a capacity or limit to the number of objects must be specified as an argument to the constructor. Here, a capacity of 4 `String` is specified.

11. ArrayBlockingQueue methods

Adding and removing elements to a `Queue` can be done with two sets of methods. `.add()` will cause an exception when the `Queue` is at capacity whereas `.offer() simply ignores the request to add when at capacity.

12. ArrayBlockingQueue methods

`.remove()` throws an exception when the `Queue` is empty but `.poll()` just returns `null`. A `Queue` does not allow null objects to be added to it.

13. Let's practice!

Let's put together some sets and queues!