ArticleslgStudy

computer science

Java collections framework

Java collections framework is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Java collections framework rather than just read about it. In short: The Java collections framework is a set of classes and interfaces that implement commonly reusable collection data structures (collections). Although referred to as a framework, it works in a manner of a library.

Java collections framework — main illustration
Java collections framework — illustration

Key takeaways

  • Java collections framework belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Java collections framework to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Java collections framework from memory before moving on to harder problems.

Reference excerpt

The Java collections framework is a set of classes and interfaces that implement commonly reusable collection data structures (collections). Although referred to as a framework, it works in a manner of a library. The collections framework provides both interfaces that define various collections and classes that implement them.

Differences from Arrays Collections and arrays are similar in that they both hold references to objects and they can be managed as a group. However, unlike arrays, Collections do not need to be assigned a certain capacity when instantiated. Collections can grow and shrink in size automatically when objects are added or removed. Collections cannot hold primitive data types such as int, long, or double. Instead, Collections can hold wrapper classes such as java.lang.Integer, java.lang.Long, or java.lang.Double. Collections are generic and hence invariant, but arrays are covariant. This can be considered an advantage of generic objects such as Collection when compared to arrays, because under circumstances, using the generic Collection instead of an array prevents run time exceptions by instead throwing a compile-time exception to inform the developer to fix the code. For example, if a developer declares an Object[] object, and assigns the Object[] object to the value returned by a new Long[] instance with a certain capacity, no compile-time exception will be thrown. If the developer attempts to add a String to this Long[] object, the java program will throw an ArrayStoreException. On the other hand, if the developer instead declared a new instance of a Collection<Object> as ArrayList<Long>, the Java compiler will (correctly) throw a compile-time exception to indicate that the code is written with incompatible and incorrect type, thus preventing any potential run-time exceptions.The developer can fix the code by instantianting Collection<Object> as an ArrayList<Object> object. If the code is using Java SE7 or later versions, the developer can instantiate Collection<Object> as an ArrayList<> object by using the diamond operator Collections are generic and hence reified, but arrays are not reified.

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. Sun Microsystems chose not to use the ideas of JGL, because they wanted a compact framework, and consistency with C++ was not one of their goals. 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 Most collections in Java that are not maps are derived from the java.util.Collection interface. Collection defines the basic parts of all collections. The interface has the add(E e) and remove(E e) methods for adding to and removing from a Collection respectively. It also has the toArray() method, which converts the Collection into an array of Objects in the Collection (with return type of Object[]). Finally, the contains(E e) method checks if a specified element exists 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 implement java.util.Iterator to scan all of the elements in the Collection. Collection is generic. Any Collection can store any Object. For example, any implementation of Collection<String> contains String objects. No casting is required when using the String objects from an implementation of Collection<String>. Note that the angled brackets < > can hold a type argument that specifies which type the Collection holds. Collection Framework Collection Framework just holds all the collections (general concept) like list etc, which follows collection interface. Types of collection There are several kinds of collections: queues, maps, lists and sets. Queues allow the programmer to insert items in a certain order and retrieve those items in the same order. An example is a waiting list. The base interfaces for queues are called Queue. Dictionaries/Maps store references to objects with a lookup key to access the object's values. One example of a key is an identification card. The base interface for dictionaries/maps is called Map. Lists are finite collections where it can store the same value multiple times. Sets are unordered collections that can be iterated and contain each element at most once. The base interface for sets is called Set.

List interface Lists are implemented in the collections framework via the java.util.Listinterface. It defines a list as essentially a more flexible version of an array. Elements have a specific order, and duplicate elements are allowed. Elements can be placed in a specific position. They can also be searched for within the list.

List implementations There are several concrete classes that implement List, including AbstractList and all of its corresponding subclasses, as well as CopyOnWriteArrayList.

AbstractList class The direct subclasses of AbstractList class include AbstractSequentialList, ArrayList and Vector. AbstractList is an example of a skeletal implementation, which leverages and combines the advantages of interfaces and abstract classes by making it easy for the developer to develop their own implementation for the given interface.

ArrayList class The java.util.ArrayList class implements the List as an array. Whenever functions specific to a List are required, the class moves the elements around within the array in order to do it.

… excerpt ends here. Continue reading the full article.

Illustrations

Java collections framework: Java collections framework class and interface hierarchy, excluding types in the java.util.concurrent package and a few other classes
Java collections framework class and interface hierarchy, excluding types in the java.util.concurrent package and a few other classes

Worked examples

Example 1 — a first encounter with Java collections framework

Start with the simplest possible case. Write down what Java collections framework claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Java collections framework before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Java collections framework ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Java collections framework

In research
Java collections framework appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Java collections framework in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Java collections framework is common in secondary-school and first-year university syllabi. It links to neighbouring topics Data structures libraries and frameworks, JDK components, so understanding it makes those chapters shorter.
In everyday life
Look for Java collections framework outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Java collections framework in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Java collections framework means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Java collections framework out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Java collections framework in simple terms?

The Java collections framework is a set of classes and interfaces that implement commonly reusable collection data structures (collections). Although referred to as a framework, it works in a manner of a library.

Why does Java collections framework matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Java collections framework?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Java collections framework.

Tags

  • Data structures libraries and frameworks
  • JDK components

Keep exploring