Kolekcije u Javi

Pre Java 2, Java je obezbedila ad hoc klase kakve su Dictionary, Vector, Stack i Properties za čuvanje i manipulaciju grupama objekata.

Iako su ove klase bile prilično korisne, nedostajala im je neka centralna, jedinstvena tema. Stoga, način na koji smo koristili Vector se razlikovao od načina na koji smo koristili Properties.

Framework (radni okvir) kolekcija je dizajniran da zadovolji nekoliko ciljeva, kao što su −

  • Framework (radni okvir) mora imati visoki učinak. Implementacije za fundamentalne kolekcije (dynamic arrays, linked lists, trees, and hashtables) moraju biti visoko efikasne.
  • Framework mora omogućiti raličitim vrstama kolekcija da rade na sličan način i sa visokim stepenom međusobne operabilnosti.
  • Framework mora lako proširiti i/ili prilagoditi kolekciju.

U cilju zadovoljenja postavljenih ciljeva, čitav framework kolekcija je dizajniran oko skupa standardnih interfejsa. Ovi interfejsi obezbeđuju nekoliko standardnih implementacija, kao što su LinkedList, HashSet i TreeSet. Mogu se koristiti u datom obliku, ili se može implementirati sopstvena kolekcija.

Framework kolekcija jeste jedinstvena arhitektura za prikazivanje i manipulaciju kolekcijama. Svi frameworksi kolekcija sadrže sledeće −

  • Interfejse − Ovo su apstraktni tipovi podataka koji predstavljaju kolekcije. Interfejsi omogućavaju da se kolekcijama rukuje  nezavisno od detalja reprezentacije tih kolekcija. U objektno orijentisanim jezicima, interfejsi generalno formiraju hijerarhiju.
  • Implementacije, npr Klase − Ovo su konkretne implementacije interfejsa kolekcija. Suštinski gledano, ovo su ponovno upotrebljive strukture podataka.
  • Algoritme − Ovo su metode koje vrše korisna računanja, kakva su pretrage i sortiranja, na objektima koji implementiraju interfejse kolekcija. Za ove algoritme se kaže da su polimorfni: to znači da ista metoda može biti korišćena na mnogo različitih implementacija odgovarajućeg interfejsa kolekcija.

Pored kolekcija, framework definiše nekoliko klasa i interfejsa mapa. Mape čuvaju parove ključ/vrednost. I pored toga što mape nisu kolekcije u doslovnom smislu reči, one su u potpunosti integrisane sa kolekcijama.

The Collection Interfaces

Sr.No.Interfejs  i opis
1The Collection Interface Omogućava rad sa grupama objekata; na vrhu je hijerarhije kolekcija.
2The List Interface Nadgrađuje Kolekciju, instanca Liste pohranjuje uređenu kolekciju elemenata.
3The SetThis extends Collection to handle sets, which must contain unique elements.
4The SortedSetThis extends Set to handle sorted sets.
5The Map Mapira jedinstvene ključeve prema vrednostima.
6The Map.EntryThis describes an element (a key/value pair) in a map. This is an inner class of Map.
7The SortedMap Nadgrađuje Mapu tako da su ključevi uređeni u rastućem poretku.
8The EnumerationThis is legacy interface defines the methods by which you can enumerate (obtain one at a time) the elements in a collection of objects. This legacy interface has been superceded by Iterator.

The Collection Classes

Java provides a set of standard collection classes that implement Collection interfaces. Some of the classes provide full implementations that can be used as-is and others are abstract class, providing skeletal implementations that are used as starting points for creating concrete collections.

The standard collection classes are summarized in the following table −

Sr.No.Class & Description
1AbstractCollectionImplements most of the Collection interface.
2AbstractListExtends AbstractCollection and implements most of the List interface.
3AbstractSequentialListExtends AbstractList for use by a collection that uses sequential rather than random access of its elements.
4LinkedListImplements a linked list by extending AbstractSequentialList.
5ArrayListImplements a dynamic array by extending AbstractList.
6AbstractSetExtends AbstractCollection and implements most of the Set interface.
7HashSetExtends AbstractSet for use with a hash table.
8LinkedHashSetExtends HashSet to allow insertion-order iterations.
9TreeSetImplements a set stored in a tree. Extends AbstractSet.
10AbstractMapImplements most of the Map interface.
11HashMapExtends AbstractMap to use a hash table.
12TreeMapExtends AbstractMap to use a tree.
13WeakHashMapExtends AbstractMap to use a hash table with weak keys.
14LinkedHashMapExtends HashMap to allow insertion-order iterations.
15IdentityHashMapExtends AbstractMap and uses reference equality when comparing documents.

The AbstractCollection, AbstractSet, AbstractList, AbstractSequentialList and AbstractMap classes provide skeletal implementations of the core collection interfaces, to minimize the effort required to implement them.

The following legacy classes defined by java.util have been discussed in the previous chapter −

Sr.No.Class & Description
1VectorThis implements a dynamic array. It is similar to ArrayList, but with some differences.
2StackStack is a subclass of Vector that implements a standard last-in, first-out stack.
3DictionaryDictionary is an abstract class that represents a key/value storage repository and operates much like Map.
4HashtableHashtable was part of the original java.util and is a concrete implementation of a Dictionary.
5PropertiesProperties is a subclass of Hashtable. It is used to maintain lists of values in which the key is a String and the value is also a String.
6BitSetA BitSet class creates a special type of array that holds bit values. This array can increase in size as needed.

The Collection Algorithms

The collections framework defines several algorithms that can be applied to collections and maps. These algorithms are defined as static methods within the Collections class.

Several of the methods can throw a ClassCastException, which occurs when an attempt is made to compare incompatible types, or an UnsupportedOperationException, which occurs when an attempt is made to modify an unmodifiable collection.

Collections define three static variables: EMPTY_SET, EMPTY_LIST, and EMPTY_MAP. All are immutable.

Sr.No.Algorithm & Description
1The Collection AlgorithmsHere is a list of all the algorithm implementation.

How to Use an Iterator ?

Often, you will want to cycle through the elements in a collection. For example, you might want to display each element.

The easiest way to do this is to employ an iterator, which is an object that implements either the Iterator or the ListIterator interface.

Iterator enables you to cycle through a collection, obtaining or removing elements. ListIterator extends Iterator to allow bidirectional traversal of a list and the modification of elements.

Sr.No.Iterator Method & Description
1Using Java IteratorHere is a list of all the methods with examples provided by Iterator and ListIterator interfaces.

How to Use a Comparator ?

Both TreeSet and TreeMap store elements in a sorted order. However, it is the comparator that defines precisely what sorted order means.

This interface lets us sort a given collection any number of different ways. Also this interface can be used to sort any instances of any class (even classes we cannot modify).

Sr.No.Iterator Method & Description
1Using Java ComparatorHere is a list of all the methods with examples provided by Comparator Interface.

Summary

The Java collections framework gives the programmer access to prepackaged data structures as well as to algorithms for manipulating them.

A collection is an object that can hold references to other objects. The collection interfaces declare the operations that can be performed on each type of collection.

The classes and interfaces of the collections framework are in package java.util.

Comments

comments