Java. Introduction to Java Collections Framework

Introduction to Java Collections Framework


Contents


Search other resources:

1. Package java.util. Collections overview

The java.util package contains a wide range of tools that provide powerful functionality for developing programs in the Java language. One of these features is the management and organization of work with sets (groups) of objects, which are called collections. Collections have been introduced in the Java language since J2SE 1.2. Collections form a so-called framework, called the Java Collection Framework, which is a complex hierarchy of classes and interfaces. The collections framework implements a set of standard operations on known types of object groups, which include:

  • queue;
  • set;
  • list;
  • array;
  • hash table;
  • tree.

The main reasons (benefits) for developing a collections framework are:

  • avoiding “manual” programming of access to organized data and the use of ready-made solutions;
  • ensuring high (maximum) performance when working with large groups of data;
  • providing a single management method for all datasets;
  • if necessary, providing a convenient mechanism for expanding collections and / or adapting them through the use of a set of standard interfaces;
  • ease of creating your own collections based on the use of the existing framework;
  • providing integration with such basic data structures as arrays.

The entire collection package includes the following content elements:

  • interfaces that provide the ability to expand existing functionality at your discretion;
  • classes – contain tools for organizing work with stacks, queues, arrays, etc.;
  • algorithms – a set of static methods that implement standard operations on collections (sorting, searching, etc.);
  • iterators – provide the only standardized way to access the elements of a collection one by one.

Starting with JDK 5, the following innovations have been introduced into the collection mechanism:

  • support for generalizations (all collections are generalized);
  • autoboxing and unboxing;
  • organization of the for loop in the style of for each loop. This means that all of the classes in the collection’s framework support the Iterable interface.

Support for generics made it possible to provide type safety in collections. Generics allow you to explicitly specify the type of data stored in a collection. This avoids subtle errors during execution.

Collections can only store references, not values of primitive types. This means that to create a collection of integers, you must specify the wrapper type Integer instead of the primitive type int. Otherwise, the compiler will throw an error. In this case, packing and auto-unpacking from int to Integer and vice versa is performed automatically. You can read more about the features of autoboxing and unboxing in Java here.

 

2. Standard interfaces. Overview

Collections are extended and adapted by implementing the standard interfaces that are included in the Java Collection Framework. These interfaces include:

  • Collection is the base interface located at the top of the collection hierarchy;
  • Queue is an implementation of a one-way queue in which elements are removed only from the beginning of the queue. This interface extends the Collection interface;
  • Deque – extends the Queue interface to organize a two-way queue;
  • List – extends the Collection interface to manage sequences (lists of objects);
  • Set – implements a set by extending the Collection interface. There are no identical elements in the set (each element occurs only 1 time);
  • SortedSet – extends the Set interface by adding sorting. It turns out a sorted set;
  • NavigableSet – extends the SortedSet interface. Ensures that elements are retrieved by the first match.

 

3. Collections classes. Overview

The Java Collection Framework implements a number of standard collection classes that implement collection interfaces (see section 2.). Standard collection classes are divided into 2 groups:

  • classes representing the complete implementation of the corresponding interfaces. These classes can be used directly;
  • classes that are templates. These classes serve as the basis for creating concrete collections.

Although collection classes are not synchronized, further synchronization of these classes is allowed.

The list of standard collection classes is as follows:

  • AbstractCollection – provides an implementation of most of the Collection interface;
  • AbstractList – inherited from the AbstractCollection class and implements most of the List interface;
  • AbstractQueue – inherited from the AbstractCollection class and implements part of the Queue interface;
  • AbstractSequentalList – inherits (extends) the AbstractList class for use in collections that use sequences when accessing elements;
  • LinkedList – inherited from the AbstractSequentialList class and implements a linked list;
  • ArrayList – a class inherited from AbstractList and implementing a dynamic array;
  • ArrayDeque – inherited from AbstractCollection and implements the Deque interface;
  • AbstractSet – inherited from the AbstractCollection class and implements most of the Set interface;
  • EnumSet – inherited from the AbstractSet class for use with enum type elements;
  • HashSet – inherited from AbstractSet class. This class implements the so-called hash tables, whose elements are key:value pairs (key:value);
  • LinkedHashSet – a class inherited from HashSet and characterized by the fact that the elements are entered in a certain order;
  • PriorityQueue – inherited from the AbstractQueue class and implements a priority queue;
  • TreeSet – inherited from AbstractSet class. This class implements a set stored as a tree structure.

 

4. Algorithms

The algorithms are defined in the Collections class through static methods and are designed to operate on collections. Since algorithms are static methods, they are available to all collection classes, that is, they are common for use.

The following is a list of algorithms that can be applied to collections:

  • addAll – inserts the specified elements into the given collection;
  • asLifoQueue – converts the collection to a stack;
  • binarySearch – implements the search for a value in the given list;
  • checkedCollection, checkedList, checkedMap, checkedNavigableMap, checkedNavigableSet, checkedQueue, checkedSet, checkedSortedMap, checkedSortedSet – return a dynamically typed display representation for lists, display maps;
  • copy – copies elements from one list to another;
  • disjoint – compares elements of two collections;
  • emptyEnumeration – returns an empty enumeration;
  • emptyIterator – returns an empty iterator;
  • emptyList, emptyListIterator – returns an empty list and an empty list iterator;
  • emptyMap – returns an empty mapping of the Map type;
  • emptyNavigableMap – returns an immutable empty mapping of the type derived from the NavigableMap interface;
  • emptySet – returns an immutable empty set;
  • emptyNavigableSet – returns an immutable empty set of NavigableSet type;
  • emptySortedMap, emptySortedSet – return an immutable empty mapping of the type obtained from the SortedMap and SortedSet interfaces;
  • enumeration – returns an enumeration of elements based on the given collection;
  • fill – fills the specified list with the value of the object;
  • frequency – counts the number of occurrences of the given object in the given collection;
  • indexOfSubList – returns the position of the first occurrence of the given sublist in the list;
  • lastIndexOfSubList – returns the position of the last occurrence of the specified sublist in the list;
  • list – returns an array of type ArrayList based on the given enumeration;
  • max – returns the maximum element from the given collection;
  • min – returns the minimum element in the collection;
  • newSetFromMap—returns the set that is formed based on the given mapping;
  • nCopies – returns a list containing the specified number of copies of the specified object;
  • replaceAll – replaces all occurrences of the given element with a new value in the given list;
  • reverse – reverses the given list;
  • reverseOrder – returns a comparator inverse to the given one;
  • rotate – implements the shift of the given list by the given number of positions;
  • shuffle – implements shuffling (randomly) of elements of a given list;
  • singleton – returns the specified object as an immutable set;
  • singletonList – returns the specified object as an immutable list;
  • singletonMap – converts a key:value pair into a mapping;
  • sort – sorts the elements of the given list;
  • swap – swaps the elements of the given list;
  • synchronizedCollection, synchronizedList, synchronizedMap, synchronizedNavigableMap, synchronizedNavigableSet, synchronizedSet, synchronizedSortedMap, synchronizedSortedSet – return a synchronized sequence that is thread safe. Collections, lists, displays, sets are processed;
  • unmodifialbleCollection, unmodifiableList, unmodifiableMap, unmodifiableNavigableSet, unmodifiableSet, unmodifiableSortedMap, unmodifiableSortedSet – return an immutable sequence based on the given sequence. Implementations for collections, lists, mappings, sets are considered.

 

5. Iterators

Iterators are special classes that implement interfaces for standardized access to individual elements of a collection. Iterators provide a method to iterate over the contents of collections. Any iterator is a class object that implements one of two interfaces:

  • Iterator – provides a loop for iterating over a collection with access to the elements of this collection;
  • ListIterator – inherited from the Iterator interface and implements a two-way list traversal with access to elements.

The declaration of the Iterator and ListIterator interfaces is

interface Iterator<E>
interface ListIterator<E>

here

  • E – the type of objects to iterate over.

 


Related topics