Java collections
AProperties object
is a persistent Hashtable object. Class Properties extends Hashtable.The Properties no-argument constructor creates an empty Properties table with no defaul tproperties. There is also an overloaded constructor that is passed a reference to a default Properties object containing default property values.
Interface List
is implemented by classes ArrayList, LinkedList and Vector. Class ArrayList is a resizable-array implementation of a List. A LinkedList is a linked list implementation of a List.
The classes and interfaces of the collections framework are in package
java.util.
Algorithm binarySearch
locates an Object in a sorted List.
Propertiesmethod getProperty
locates the value of the key specified as an argument.
Class Collections provides static methods for
manipulating collections. Many of the methods are implementations of polymorphic algorithms for searching, sorting and so on.
Maps
map keys to values and cannot contain duplicate keys. Maps differ from Sets in that Maps contain both keys and values, whereas Sets contain only values.
PriorityQueue,
one of the Queue implementations, orders elements by their natural ordering (i.e., the implementation of the compareTo method) or by a Comparator object that is supplied through the constructor.
A List is an
ordered Collection that can contain duplicate elements.
The Comparator interface
provides a means of sorting a Collection's elements in an order other than their natural order.
The Collections API
provides a set of public static methods for converting collections to unmodifiable versions. Unmodifiable wrappers throw UnsupportedOperationExceptions if attempts are made to modify the collection.
Algorithm shuffle
randomly orders the elements of a List
Method clear
removes elements from a List.
removeAllElements
removes every element from the Vector. Method removeElement At removes the element at the specified index.
Vector method remove
removes the first occurrence of its argument from the Vector. Method
pop
removes the top element of the stack.
Method load
restores the contents of the Properties object from the InputStream object pecified as the argument.
Collections method reverseOrder
returns a Comparator object that can be used with sort to sort elements of a collection in reverse order.
method get
returns a List element.
Vector method firstElement
returns a reference to the first element. Method lastElement returns a reference to the last element
Method peek
returns a reference to the top element without removing it.
HashMap method keySet
returns a set of the keys. Map methods size and isEmpty return the number of key-value pairs in the Map and a boolean indicating whether the Map is empty, respectively.
Method toArray
returns the contents of a collection as an array.
Algorithm reverse
reverses the elements of a List, fillsets every List element to a specified Object, and copy copies elements from one List into another
Method store
saves the contents of the Properties object to the OutputStream object specified as the first argument.
Algorithm sort
sorts the elements of a List.
Properties method setProperty
specifies the value associated with the key argument.
HashMaps
store elements in a hash table
TreeMaps
store elements in a tree.
Hashtables and HashMaps
store elements in hash tables, and TreeMaps store elements in trees.
Interface Collection
the root interface in the collection hierarchy from which interfaces Set and List are derived. Interface Collection contains bulk operations for adding, clearing, comparing and retaining objects in a collection. Interface Collection provides a method iterator for getting an Iterator.
HashMap
is a generic class that takes two type arguments. The first type argument specifies the type of key, and the second the type of value.
Class Arrays static methods for manipulating arrays,
including sort for sorting an array, binary Search for searching a sorted array, equals for comparing arrays and fill for placing items in an array.
Method insertElementAt
inserts an element at the specified position. Method set sets the element at a specific position.
A collection framework contains
Interfaces: These are abstract data types that represent collections. Thanks to interfaces, collections can be manipulated independently of the details of their representation Implementations: these are the concrete implementations of the collection interfaces. They are reusable data structures Algorithms: are the methods that perform useful computations such as searching, sorting on objects that implement collection interfaces. These algorithms are said to be polymorphic, meaning the same method can be used on many different implementations of the appropriate collection interface thereby being reusable.
classs Objectives
1. An understanding of the collection, list and set interfaces 2. An understanding of the queue interfaces 3. An understanding of the arraylist, hashset ,priorityqueue and treeset classes 4. An understanding of treemap and hashmap classes 5. Understanding of collection algorithms
what is a collection
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.
Method subList returns a view of a portion of a List.
Any changes made to this view are also made to the List.
Why use the java collections Framework?
Reduction of programming effort: Increasing program speed and quality: Facilitation of interoperability among unrelated APIs: Reduction in the effort needed to learn and use new APIs: Reduction in the effort need to design new APIs: Encourage software reuse:
whats is the java collection framework used for
The Java collections framework gives the programmer access to prepackaged data structures as well as to algorithms for manipulating them.
Arrays method asList returns
a List view of an array, which enables a program to manipulate the array as if it were a List. Any modifications made through the List view change the array, and any modifications to the array change the List view.
Queue,
a new collection interface introduced in Java SE 5, extends interface Collection and provides additional operations for inserting, removing and inspecting elements in a queue.
HashMap method put
adds a key and a value into a HashMap. Method get locates the value associated with the specified key. Method isEmpty determines whether the map is empty.
Vector method add
adds its argument to the end of the Vector.
Stack method push
adds its argument to the top of the stack.
Algorithms addAll
appends all the elements in an array to a collection, frequency calculates how many elements in the collection are equal to the specified element, and disjoint determines whether two collections have elements in common.
PriorityQueue operations
are offer to insert an element at the appropriate location based on priority order, poll to remove the highest-priority element of the priority queue(i.e., the head of the queue), peek to get a reference to the highest-priority element of the priorityqueue, clear to remove all elements in the priority queue and size to get the number of elements in the priority queue.
Synchronization wrappers
are provided for collections that can be accessed by multiple threads simultaneously.
Vector method contains
determines whether the Vector contains the searchKey specified as an argument. Vector method indexOf gets the index of the first location of its argument. The method returns -1 if the argument is not found in the Vector.
Vector method isEmpty
determines whether the Vector is empty. Methods size and capacity determine the number of elements currently in the Vector and the number of elements that can be stored in the Vector without allocating more memory, respectively.
Stack method empty
determines whether the stack is empty.
Class Vector manages
dynamically resizable arrays. At any time, a Vector contains a number of elements that is less than or equal to its capacity. If a Vector needs to grow, it grows by its capacity increment. If no capacity increment is specified, Java doubles the size of the Vector each time additional capacity is required. The default capacity is 10 elements.
Interface SortedMap
extends Map and represents a map that maintains its keys in sorted order.Class TreeMap implements SortedMap.
Interface SortedSet
extends Set and represents a set that maintains its elements in sorted order.Class TreeSet implements SortedSet.
Algorithms min and max
find the smallest and largest items in a collection.
Collections from the collections synchronized
framework are unsynchronized.
TreeSet method headSet
gets a view of a TreeSet that is less than a specified element. Method tailSet gets a view that is greater than or equal to a specified element. Any changes made to the view are made to the TreeSet.
Method size
gets the number of items in a List,
Iterator method
hasNext determines whether a Collection contains another element. Method next returns a reference to the next object in the Collection and advances the Iterator.
ASet
is a Collection that contains no duplicate elements. HashSet stores its elements in a hashtable. TreeSet stores its elements in a tree.
