Java Collections Framework

 The Java™ Tutorials – Trial: Collections – Oracle Java Documentation
(https://docs.oracle.com/javase/tutorial/collections/index.html)

This section describes the Java Collections Framework. Here, you will learn what collections are and how they can make your job easier and programs better. You’ll learn about the core elements — interfaces, implementations, aggregate operations, and algorithms — that comprise the Java Collections Framework.

trail icon Introduction tells you what collections are, and how they’ll make your job easier and your programs better. You’ll learn about the core elements that comprise the Collections Framework: interfaces, implementations and algorithms.

trail icon Interfaces describes the core collection interfaces, which are the heart and soul of the Java Collections Framework. You’ll learn general guidelines for effective use of these interfaces, including when to use which interface. You’ll also learn idioms for each interface that will help you get the most out of the interfaces.

trail icon Aggregate Operations iterate over collections on your behalf, which enable you to write more concise and efficient code that process elements stored in collections.

trail icon Implementations describes the JDK’s general-purpose collection implementations and tells you when to use which implementation. You’ll also learn about the wrapper implementations, which add functionality to general-purpose implementations.

trail icon Algorithms describes the polymorphic algorithms provided by the JDK to operate on collections. With any luck you’ll never have to write your own sort routine again!

trail icon Custom Implementations tells you why you might want to write your own collection implementation (instead of using one of the general-purpose implementations provided by the JDK), and how you’d go about it. It’s easy with the JDK’s abstract collection implementations!

trail icon Interoperability tells you how the Collections Framework interoperates with older APIs that predate the addition of Collections to Java. Also, it tells you how to design new APIs so that they’ll interoperate seamlessly with other new APIs.


Java Collections Framework
(https://en.wikipedia.org/wiki/Java_collections_framework)

The Java Collections Framework (JCF) is a set of classes and interfaces that implement commonly reusable collection data structures.

Although referred to as a framework, it works in a manner of a library. The JCF provides both interfaces that define various collections and classes that implement them.

History
Collection implementations in pre-JDK 1.2 versions of the Java platform included few data structure classes, but did not contain a collections framework. The standard methods for grouping Java objects were via the array, the Vector, and the Hashtable classes, which unfortunately were not easy to extend, and did not implement a standard member interface.

To address the need for reusable collection data structures, several independent frameworks were developed, the most used being Doug Lea’s Collections package, and ObjectSpace Generic Collection Library (JGL), whose main goal was consistency with the C++ Standard Template Library (STL).

The collections framework was designed and developed primarily by Joshua Bloch, and was introduced in JDK 1.2. It reused many ideas and classes from Doug Lea’s Collections package, which was deprecated as a result.[4] Sun chose not to use the ideas of JGL, because they wanted a compact framework, and consistency with C++ was not one of their goals.[7]

Doug Lea later developed a concurrency package, comprising new Collection-related classes. An updated version of these concurrency utilities was included in JDK 5.0 as of JSR 166.

Architecture
Almost all collections in Java are derived from the java.util.Collection interface. Collection defines the basic parts of all collections. The interface states the add() and remove() methods for adding to and removing from a collection respectively. Also required is the toArray() method, which converts the collection into a simple array of all the elements in the collection. Finally, the contains() method checks if a specified element is in the collection. The Collection interface is a subinterface of java.lang.Iterable, so any Collection may be the target of a for-each statement. (The Iterable interface provides the iterator() method used by for-each statements.) All collections have an iterator that goes through all of the elements in the collection. Additionally, Collection is a generic. Any collection can be written to store any class. For example, Collection<String> can hold strings, and the elements from the collection can be used as strings without any casting required. Note that the angled brackets < > can hold a type argument that specifies which type the collection holds.


 


Java Collections Framework Tutorials [RECOMMENDED]
(http://beginnersbook.com/java-collections-tutorials/)

The Java Collections Framework is a collection of interfaces and classes which helps in storing and processing the data efficiently. This framework has several useful classes which have tons of useful functions which makes a programmer task super easy. I have written several tutorials on Collections and below are the links of those. All the tutorials are shared with examples and source codes to help you understand better.

Collections Framework hierarchy

Java Collections

List
A List is an ordered Collection (sometimes called a sequence). Lists may contain duplicate elements. Elements can be inserted or accessed by their position in the list, using a zero-based index.

Set
A Set is a Collection that cannot contain duplicate elements. There are three main implementations of Set interface: HashSet, TreeSet, and LinkedHashSet. HashSet, which stores its elements in a hash table, is the best-performing implementation; however it makes no guarantees concerning the order of iteration. TreeSet, which stores its elements in a red-black tree, orders its elements based on their values; it is substantially slower than HashSet. LinkedHashSet, which is implemented as a hash table with a linked list running through it, orders its elements based on the order in which they were inserted into the set (insertion-order).

Map
A Map is an object that maps keys to values. A map cannot contain duplicate keys. There are three main implementations of Map interfaces: HashMap, TreeMap, and LinkedHashMap.
HashMap: it makes no guarantees concerning the order of iteration
TreeMap: It stores its elements in a red-black tree, orders its elements based on their values; it is substantially slower than HashMap.
LinkedHashMap: It orders its elements based on the order in which they were inserted into the set (insertion-order).

Iterator/ListIterator
Both Iterator and ListIterator are used to iterate through elements of a collection class. Using Iterator we can traverse in one direction (forward) while using ListIterator we can traverse the collection class on both the directions(backward and forward). To know more differences between these two refer this article: Difference between Iterator and ListIterator.


Javatpoint LogoCollections in Java
(http://www.javatpoint.com/collections-in-java#)

Collections in java is a framework that provides an architecture to store and manipulate the group of objects.

All the operations that you perform on a data such as searching, sorting, insertion, manipulation, deletion etc. can be performed by Java Collections.

Java Collection simply means a single unit of objects. Java Collection framework provides many interfaces (Set, List, Queue, Deque etc.) and classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet etc).


JournalDevJava Collections Framework Tutorial
(http://www.journaldev.com/1260/java-collections-framework-tutorial)

Java Collections are one of the core frameworks of Java language. We use Collections almost in every application, this tutorial will explain Java Collections Framework in detail.

Java Collection Framework

  1. What is Java Collections Framework?
  2. Java Collections Interfaces
  3. Java Collection Classes
  4. Collections class
  5. Synchronized Wrappers
  6. Unmodifiable wrappers
  7. Thread Safe Collections
  8. Collections API Algorithms
  9. Java 8 Collections API Features
  10. Collection classes in a Nutshell

What is Java Collections Framework?

Collections are like containers that groups multiple items in a single unit. For example; a jar of chocolates, list of names etc. Collections are used almost in every programming language and when Java arrived, it also came with few Collection classes; Vector, Stack, Hashtable, Array. Java 1.2 provided Collections Framework that is architecture to represent and manipulate Collections in a standard way. Java Collections Framework consists of following parts:

  • Interfaces: Java Collections Framework interfaces provides the abstract data type to represent collection. java.util.Collection is the root interface of Collections Framework. It is on the top of Collections framework hierarchy. It contains some important methods such as size(), iterator(), add(), remove(), clear() that every Collection class must implement.Some other important interfaces are java.util.List, java.util.Set, java.util.Queue and java.util.Map. Map is the only interface that doesn’t inherits from Collection interface but it’s part of Collections framework. All the collections framework interfaces are present in java.util package.
  • Implementation Classes: Java provides core implementation classes for collections. We can use them to create different types of collections in our program. Some important collection classes are ArrayList, LinkedList, HashMap, TreeMap, HashSet, TreeSet.These classes solve most of our programming needs but if we need some special collection class, we can extend them to create our custom collection class.Java 1.5 came up with thread-safe collection classes that allowed to modify Collections while iterating over it, some of them are CopyOnWriteArrayList, ConcurrentHashMap, CopyOnWriteArraySet. These classes are in java.util.concurrent package. All the collection classes are present in java.util and java.util.concurrent package.
  • Algorithms: Algorithms are useful methods to provide some common functionalities, for example searching, sorting and shuffling.

Below class diagram shows Collections Framework hierarchy. For simplicity I have included only commonly used interfaces and classes.
Java-Collections-Framework

 

 

 

 

 

 

 

 


Java Programming Tutorial – The Collection Framework
(https://www3.ntu.edu.sg/home/ehchua/programming/java/J5c_Collection.html)

Introduction to the Collection Framework

Although we can use an array to store a group of elements of the same type (either primitives or objects). The array, however, does not support so-called dynamic allocation – it has a fixed length which cannot be changed once allocated. Furthermore, array is a simple linear structure. Many applications may require more complex data structure such as linked list, stack, hash table, sets, or trees.

In Java, dynamically allocated data structures (such as ArrayList, LinkedList, Vector, Stack, HashSet, HashMap, Hashtable) are supported in a unified architecture called the Collection Framework, which mandates the common behaviors of all the classes.

A collection, as its name implied, is simply an object that holds a collection (or a group, a container) of objects. Each item in a collection is called an element. A framework, by definition, is a set of interfaces that force you to adopt some design practices. A well-designed framework can improve your productivity and provide ease of maintenance.

The collection framework provides a unified interface to store, retrieve and manipulate the elements of a collection, regardless of the underlying and actual implementation. This allows the programmers to program at the interfaces, instead of the actual implementation.

The Java Collection Framework package (java.util) contains:

  1. A set of interfaces,
  2. Implementation classes, and
  3. Algorithms (such as sorting and searching).

Similar Collection Framework is the C++ Standard Template Library (STL).

Prior to JDK 1.2, Java’s data structures consist of array, Vector, and Hashtable that were designed in a non-unified way with inconsistent public interfaces. JDK 1.2 introduced the unified collection framework, and retrofits the legacy classes (Vector and Hashtable) to conform to this unified collection framework.

JDK 1.5 introduced Generics (which supports passing of types), and many related features (such as auto-boxing and unboxing, enhance for-loop). The collection framework is retrofitted to support generics and takes full advantages of these new features.

To understand this chapter, you have to be familiar with:

  • Polymorphism, especially the upcasting and downcasting operations.
  • Interfaces, abstract methods and their implementations.
  • Generics, Auto-boxing & unboxing, and enhanced for-loop (introduced in JDK 1.5).

You need to refer to the JDK API specification while reading this chapter. The classes and interfaces for the Collection Framework are kept in package java.util.


Get started with the Java Collections Framework – Find out how Sun’s new offering can help you to make your collections more useful and accessible
(http://www.javaworld.com/article/2076800/java-se/get-started-with-the-java-collections-framework.html)

JDK 1.2 introduces a new framework for collections of objects, called the Java Collections Framework. “Oh no,” you groan, “not another API, not another framework to learn!” But wait, before you turn away, hear me out: the Collections framework is worth your effort and will benefit your programming in many ways. Three big benefits come immediately to mind:

  • It dramatically increases the readability of your collections by providing a standard set of interfaces to be used by many programmers in many applications.
  • It makes your code more flexible by allowing you to pass and return interfaces instead of concrete classes, generalizing your code rather than locking it down.
  • It offers many specific implementations of the interfaces, allowing you to choose the collection that is most fitting and offers the highest performance for your needs.

And that’s just for starters.

Our tour of the framework will begin with an overview of the advantages it provides for storing sets of objects. As you’ll soon discover, because your old workhorse friends Hashtable and Vector support the new API, your programs will be uniform and concise — something you and the developers accessing your code will certainly cheer about.


The Java Collections Framework

This is from Chapter 15 of the textbook Horstmann, Cay (2012) Java For Everyone – Late Objects, Second Edition, Wiley

PowerPoint – Ch15 – The Java Collections Framework

Java for Everyone-Late Objects – Chapter15 – The Java Collections Framework (PDF)

Java Collections Framework Example Code (ZIP)