Collections

A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit. Collections are used to store, retrieve and manipulate data, and to transmit data from one method to another. Collections typically represent data items that form a natural group, like poker hand (a collection of cards), a mail folder (a collection of letters), or a telephone directory (a collection of name-to-phone-number mappings).

 

If you have used Java-- or just about any other programming language--  you are already familiar with collections. Collection implementations in earlier versions of Java included vector, hashtable, an array. While earlier versions of Java contained collection implementations, they did not contain collections framework.

 

 

What is a collections framework?

A collections Framework is a unified architecture for representing manipulating collections. All collections framework contain three things.

 

Interfaces: 

abstract data types representing collections. Interfaces allow collections to be manipulated independently of the details of their representation. In object-oriented languages like Java,  these interfaces generally from a hierarchy.

 

Implementation:

concrete implementations of the collection interfaces. In essence,  these are reusable data structures.

 

Algorithms: 

methods that perform useful computations, like searching and sorting, on objects that implement collection interfaces. These algorithms are said to be polymorphic because the same method can be used on many different implementations of the appropriate collection interface.   In essence, algorithms are reusable functionality.

 

The best-known examples of the collections frameworks are the C++  Standard Template  Library (STL), and Smalltalk’s collections classes.

 

 

Benefits of the collections framework

It reduces programming effort: 

by providing useful data structures an algorithms,  collection framework please YouTube You to concentrate on the important part of your program rather than the low-level plumbing required to make it work. By facilitating interoperability among unrelated APIs ( as described below),  collections Framework frees you from writing oodles of adaptor objects of conversion code to connect APIs.

 

It increases program speed and quality:

the collections Framework does this primarily by providing high-performance, high-quality implementation of useful data structures and algorithms. Also, because the various implementations of each interface are interchangeable, programs can be easily tuned by switching collections implementations. Finally, because you are freed from the drudgery of writing your own data structures.  you will have more time to devote to improving the quality and performance of the rest of the program.

 

It allows interoperability among unrelated APIs: 

The collection interfaces will become the “lingua franca” by which APIs pass collections back and forth. If my network administration API furnishes a collection of node names, and your the GUI toolkit expect a collection of columns heading, our APIs will interoperability seamlessly even though they were written independently.

 

It reduces the effort to learn and use new APIs:

Mini API naturally take collections on input and output. in the past, each such  API had a little “ sub-API”  devoted to manipulating its collections. There was little consistency among these ad-hoc collections  Sub- APIs,  so you had to learn each one from scratch and it was easy to make mistakes when using them.  With the advent of standard collections interfaces, the problem goes away.

 

It reduces effort to design new APIs:

This is the flip-side of the previous advantages: designer and implementers don't have to reinvent the wheel each time they create an API that relies on collections. They just use the standard collection interfaces.

 

It fosters software reuse:

New data structures that conform to the standard collection interfaces are by nature reusable. The same goes for me algorithms that operate on the object that implements these interfaces. 

 

 

Drawbacks of the collections framework

Historically, collections frameworks have been quite complex, which gave them a reputation for having a steep learning curve. We believe that java's new collections Framework breaks with this tradition, as you will learn for yourself in the following lessons.

 

Interfaces 

The core collection interfaces are the interfaces used to manipulate collections and to pass them from one method to another. The basic purpose of these interfaces is to allow collections to be manipulated independently of the details of their representation. The core collection interfaces are the heart and soul of the collections framework. When you understand how to use these interfaces, you know most of what there is to know about the framework. The core collections interfaces are shown below:

p40

 

The core collection interfaces from a hierarchy: a set is a special kind of collection, and a SortedSet is a special kind of set, and so forth. Note also that the hierarchy consists of two distinct trees: a map is not a true collection.

 

To keep the number of core collection interfaces manageable, the JDK doesn't provide separate interfaces for each variant of each collection type. (Among the possible variants are immutable, fixed-size, and appended-only.) Instead, the modification of operations in each interface are designated optional: a given implementation may not support some of these operations. If an unsupported operation is invoked, a collection throws an UnsupportedOperationException. Implementations are responsible for documenting which of the optional operation they support. All the JDKs general-purpose implementations support all the optional operations.

 

The four sections that follow teach you to know how to use each of the four basic core collection interfaces. In particular, describe the idioms that allow you to use these interfaces effectively.

 

 

Generics 

When you take an element out of a collection,  you must cast it to the type of element that type of element that is stored in the collection. besides being inconvenient, this is unsafe. The compiler does not check that your cast is the same as the collection’s type, so that cost can fail at run time.

 

Generic provides a way for you to communicate the type of a collection to the compiler so that it can be checked. Once the compiler knows the element type of the collection, the compiler can check that you have used the collection consistently and can insert the correct casts on values being taken out of the collection.

 

 

Collection

the collection interface is the root of the collection hierarchy. A Collection represents a group of the objects known as its elements. Some Collection implementation allows duplicate elements and others do not. Some are ordered and unordered. The JDK doesn't provide any direct implementations of this interface: It provides implementations of more specific subinterfaces like Set and List. This interface is the least common denominator that all collections implement. The collection is used to pass collections around and manipulate them when maximum generality is desired.

 

 

Set

A set is a collection that cannot contain duplicate elements. As you might expect, this interface model the mathematical set abstraction. It is used to represent sets like the cards comprising a poker hand, the courses making up a student’s schedule, of the processes of running on a machine.

 

 

List

List and ordered collection (Sometimes called a sequence.)  The list can contain duplicate elements. The user of a List generally has precise control over where in the List each element is inserted. The user can access elements by their integer index (position). If you have use Vector,  you are already familiar with the general flavor of List.

 

 

Map

A map is an object that maps keys to values. The map cannot contain duplicate keys: Each key can map to at most one value. If you have used Hashtable, you are already familiar with the general flavor of Map.

 

The last two core collection interfaces (Sortedset and SortedMap) are merely Sorted version of Set and Map. In order to Understand these interfaces, you have to know how the order is maintained among objects. Even if you don't plan to use Sortedset or SortedMap, read the following section if you plan to sort Lists.

 

 

Object Ordering

There are two ways to order objects: The comparable interface provides automatic natural order on classes that implement it, while the comparator interface gives the programmer complete control over object ordering. Note that these are not core collection interfaces, but underlying infrastructure.

 

Now that you know all about object order, here are the last two core collection interfaces:

SortedSet

A SortedSet is a Set that maintains its elements in ascending order. Several additional operations are provided to take advantages of the ordering. The SortedSet interface is used for things like word lists and membership rolls.

 

SortedMap

A SortedMapis a Map that maintains its mapping in ascending key order. It is the Map analog of SortedSet. The SortedMap interface is used for apps like dictionaries and telephone directories.

 

 

General info

A collection is a single object representing a group of objects known as its elements. Six collection interfaces: Collection, Set, List, SortedSet, Map, SortedMap.

 

Set

  • A group of objects with no duplication.
  • SortedSet -- subinterface of Set.
  • TreeSet -- sorted.
  • HashSet -- best performance, unordered.
  • LinkedHashSet -- since jdk 1.4.

 

List

  • A subinterface of Collection.
  • A group of objects with duplicate elements.
  • ArrayList -- like Vector, but its methods.
  • LinkedList --  sequential access.

 

vector 

  • Has been retrofitted Implement List interface.
  • Provides methods for working with dynamic arrays of varied element types.
  • Its methods are synchronized.
  • Slower access. 

 

Vector and ArrayList

  • One slower, the other faster.
  • One has synchronized methods by default, the other is not. 
  • prefer ArrayList.

 

Map

  • Maps keys and values.
  • Hashtable --  historical implementation.
  • SortedMap.
  • HashMap.
  • TreeMap.
  • LinkedHashMap.
  • IdentityHashMap.
  • WeakHashMap.

 

About Tec News

Tec News is optimized for learning Web Development, Programming, Database System and Tutorials, Articles, References for your development. This web help developers, learners learn these technologies easy and free.

Contact Us

If you have any suggestions or have any questions regarding our web please feel free to contact us.

info@tec-news.com        

https://www.tec-news.com