You are on page 1of 20

Java: generic collection

chapter 20
collection
• A collection is a data structure—actually, an object—that can hold
references to other objects.
• Usually, collections contain references to objects that are all of the
same type. The collections-framework interfaces declare the
operations to be performed generically on various types of
collections.
• This technique receive the benefits of compile-time type checking—the compiler
ensures that you’re using appropriate types with your collection and, if not, issues
compile-time error messages.

• Each primitive type has a corresponding type-wrapper class (in package java.lang).
These classes are called Boolean, Byte, Character, Double, Float, Integer, Long and
Short. These enable you to manipulate primitive-type values as objects.

• Each of the numeric type-wrapper classes—Byte, Short, Integer, Long, Float and
Double—extends class Number. Also, the type-wrapper classes are final classes, so
you cannot extend them
• Primitive types do not have methods.
• So the methods related to a primitive type are located in the
corresponding type-wrapper class (e.g., method parseInt, which
converts a String to an int value, is located in class Integer).
• If you need to manipulate a primitive value in your program, first refer
to the documentation for the type-wrapper classe
boxing conversion and the unboxing
conversion
• Simplify converting between primitive-type values and type-wrapper
objects with no additional coding on the part of the programmer.
• A boxing conversion converts a value of a primitive type to an object
of the corresponding type-wrapper class.
• An unboxing conversion converts an object of a type-wrapper class to
a value of the corresponding primitive type.
• These conversions can be performed automatically (called autoboxing
and auto-unboxing).
• Interface Collection is the root interface in the collection hierarchy from which
interfaces Set, Queue and List are derived.
• Interface Set defines a collection that does not contain duplicates.
• Interface Queue defines a collection that represents a waiting line—typically,
insertions are made at the back of a queue and deletions from the front, though
other orders can be specified.
• Interface Collection contains bulk operations (i.e., operations performed on an
entire collection)
• A Collection can also be converted to an array.
• Has a method that returns an Iterator object, which allows a program to walk
through the collection and remove elements from it during the iteration.
Stack
• Also stack implements list interface. Ex:List <data-type> list4 = new
Stack();
• Stack<type> stk = new Stack<>();
• Class Collections provides static methods that search, sort and
perform other operations on collections. this class’s wrapper methods
enable you to treat a collection as a synchronized collection (or an
unmodifiable collection.
• Synchronized collections are for use with multithreading which
enables programs to perform operations in parallel.
• Unmodifiable collections are useful when clients of a class need to
view a collection’s elements, but they should not be allowed to
modify the collection by adding and removing elements
• A List (sometimes called a sequence) is an ordered Collection that can
contain duplicate elements. Like array indices, List indices are zero
based.
• Interface List is implemented by several classes, including ArrayList,
LinkedList and Vector.
• A LinkedList enables efficient insertion (or removal) of elements in the
middle of a collection.
• Unsynchronized collections provide better performance than
synchronized ones. For this reason, ArrayList is typically preferred
over Vector in programs that do not share a collection among threads.
Limitation of array
• Arrays are of fixed length. You can not change the size of the arrays
once they are created.
• You can not accommodate an extra element in an array after they are
created.
• Memory is allocated to an array during it’s creation only, much before
the actual elements are added to it.
ArrayList class has many methods to manipulate the stored objects.
ArrayList class has methods to perform solo modifications ( add(),
remove()… ), bulk modifications ( addAll(), removeAll(), retainAll()… ),
searching( indexOf(), lasIndexOf() ) and iterations( iterator() ).
If generics are not used, ArrayList can hold any type of objects.
Array insertion at any index
Vector is thread safe, but slower
thatArrayList
1) You can achieve Thread Safety without
Vector.
• using synchronizedList() method of Collections class. Below is the
sample code.
ArrayList<Integer> list = new ArrayList<Integer>();
Collections.synchronizedList(list);
Speed
USE Arraylist<> in most case
• Linked list insertion is even trickier. Performance chart for 10000 elements of string collection -
• sample size = 10000
• Time taken to insert at last position:
• Time taken by Array : 16ms
• Time taken by ArrayList : 0ms
• Time taken by LinkedList : 0ms
• Time taken by Stack : 16ms
• Time taken by Vector : 0ms
• Time for insertion at random positions:
• Array :0ms
• ArrayList :47ms
• Linked List :406ms
• Vector :31ms
• Stack :0ms

You might also like