"Hi, Amigo!"

"Hi, Ellie!"

"Today we're going to wrap our heads around the structure of collections once and for all."

"I've waited a long time for this."

"You already know what collections are, and you know how to work with them. It's time to organize your knowledge into a strict system. Then many 'why' and 'how' questions will go away, and most things will become obvious."

"Collection interfaces. The inheritance structure of collection interfaces looks something like this:"

Collection interfaces - 1

Notice two things.

First, everything you see here is an interface.

Second, the arrows mean «inherits».

"In other words, List, Set, and Queue inherit Collection, but Map doesn't?"

"Yep. Abstract classes then inherit these interfaces and, in turn, the implementations you know about inherit those abstract classes: ArrayList, Hashtable, TreeSet,…"

"Right you are."

"Now let's see what these interfaces' methods are for:"

Methods of the Iterable<E>: interface:

Methods Description
Iterator<T> iterator(); Returns an iterator object.

"That doesn't seem like enough."

"Well, that's how many there are. That's all for iterators. We won't cover them right now, but we will cover them in detail soon."

Methods of the Collection<E>: interface:

Methods Description
boolean add(E e); Adds an element to the collection
boolean addAll(Collection<? extends E> c); Adds elements to the collection
void clear(); Removes all elements from the collection
boolean contains(Object o); Checks to see if the collection contains the element.
boolean containsAll(Collection<?> c); Checks to see if the collection contains the elements.
boolean equals(Object o); Checks to see if collections are equivalent
int hashCode(); Returns the hash code
boolean isEmpty(); Checks to see if the collection is empty.
Iterator<E> iterator(); Returns an iterator object
boolean remove(Object o); Removes an element from the collection
boolean removeAll(Collection<?> c); Removes elements from the collection
boolean retainAll(Collection<?> c); Removes all elements not in c
int size(); Returns the collection's size
Object[] toArray(); Converts the collection to an array
<T> T[] toArray(T[] a); Converts the collection to an array

"I'm already solid on everything here. I've used half of these methods, and I've encountered the other half."

"Great, let's carry on then."

Methods of the  List<E>: interface:

Methods Description
void add(int index, E element); Adds elements to the middle of the collection
boolean addAll(int index, Collection<? extends E> c); Adds elements to the collection
E get(int index); Returns an element by index
int indexOf(Object o); Returns the index (number) of an element
int lastIndexOf(Object o); Returns the index of the last element.
ListIterator<E> listIterator(); Returns an iterator for the list
ListIterator<E> listIterator(int index); Returns an iterator for the list
E remove(int index); Removes an element by its index
E set(int index, E element); Sets a new value by index
List<E> subList(int fromIndex, int toIndex); Returns a subcollection

"Nothing radically new here either. I already know almost everything about collections, which I can't help but be happy about."

"Well, I think I can find something that will surprise you. But let's continue examining interfaces:"

Methods of the Set<E>: interface:

Methods Description
No methods

"The Set interface doesn't have any new methods — only those it inherits."

"Yeah, I saw that the Interable interface also didn't have anything.

"On the other hand, fewer methods means less to remember!"

"Your life-affirming optimism makes me happy."

"The Set interface is inherited by two interfaces with methods: SortedSet and NavigableSet. But I won't go over them, or we'll never finish."

"Instead, let me give you a general picture of the classes and interfaces that describe collections in Java."

"Bring it on."

"Then hold on tight:"

Collection interfaces - 2

"Wow, that's huge!"

"Well, it's not that big. Also, remember that abstract classes are entirely optional. But it is good to remember which class implements which interfaces. That can come in handy quite frequently."

"Oh, I would also like to note that some collections are considered obsolete."

"Which are those?"

"I'm talking about the Vector, Stack, Dictionary, and Hashtable classes. These are synchronized (thread-safe) versions of ordinary collections."

"But Java has added a special concurrency library with a lot of collections that not only can be accessed from other threads, but also have a much more efficient implementation. ConcurrentHashMap is much more efficient than Hashtable."

"You can use the Vector, Stack, Dictionary, and Hashtable collections, but it isn't recommended."

"Got it, I'll keep that in mind."

"Thanks, Ellie!"