Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

Java_Collections_new_topics

Java_Collections_new_topics updated.

Uploaded by

Anurag Rajak
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Java_Collections_new_topics

Java_Collections_new_topics updated.

Uploaded by

Anurag Rajak
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

The Java Collection Framework The Java Collections Framework is

a library of classes and interfaces for working with collections of


objects. A collection is an object which can store other objects,
called elements. Collections provide methods for adding and
removing elements, and for searching for a particular element
within the collection.

The Main Types of Collections • Lists • Sets •Maps

Lists Lists: List type collections assign an integer (called an index)


to each element stored. Indices of elements are 0 for the element at
the beginning of the list, 1 for the next element, and so on. Lists
permit duplicate elements, which are distinguished by their
position in the list.

Sets Set: a collection with no notion of position within the


collection for stored elements. Sets do not permit duplicate
elements. 6 Maps A map is a collection of pairs of objects: 1. A
value: this is the object to be stored. 2. A key: this is another object
associated with the value, and which can be used to quickly find
the value within the collection. A map is really a set of keys, with
each each key having a value attached to it. Maps do not allow
duplicate keys. 7 Part of the JCF Hierarchy 8 9 The Collection
Interface • Lists and Sets are similar in many ways. • The
Collection Interface describes the operations that are common to
both. •Maps are fundamentally different from Lists and Sets and
are described by a different interface. 10 Some Methods in the
Collection Interface Method Description add(o : E) : boolean Adds
an object o to the Collection. The method returns true if o is
successfully added to the collection, false otherwise. clear() : void
Removes all elements from the collection. contains(o : Object):
boolean Returns true if o is an element of the collection, false
otherwise. isEmpty() : boolean Returns true if there are no
elements in the collection, false otherwise. iterator() : Iterator
Returns an object called an iterator that can be used to examine all
elements stored in the collection. remove(o : Object) : boolean
Removes the object o from the collection and returns true if the
operation is successful, false otherwise. size() : int Returns the
number of elements currently stored in the collection. 11
AbstractCollection The AbstractCollection class provides a skeleton
implementation for a Collection class by implementing many of the
methods of the Collection interface. Programmers can create a
working collection class by providing implementations for
iterator(), size(), and overriding add(o : Object). 12 Iterators An
iterator is an object that is associated with a collection. The iterator
provides methods for fetching the elements of the collection, one at
a time, in some order. Iterators have a method for removing from
the collection the last item fetched. 13 The Iterator Interface
Iterators implement the Iterator interface. This interface specifies
the following methods: hasNext() : boolean next() : E remove() :
void The remove() method is optional, so not all iterators have it. 14
Methods of the Iterator Interface Method Description hasNext() :
boolean Returns true if there is at least one more element from the
collection that can be returned, false otherwise. next() : E Returns
the next element from the collection. remove() : void Removes from
the collection the element returned by the last call to next(). This
method can be called at least one time for each call to next(). 15
The List Interface The List interface extends the Collection
interface by adding operations that are specific to the position-
based, index-oriented nature of a list. 16 List Interface Methods
The methods in the List interface describe operations for adding
elements and removing elements from the list based on the index of
the element. There are also methods for determining the index of
an element in the list when the value of an element is known. 17
The List Interface Methods add(index:int, el:E) : void Adds the
element el to the collection at the given index. Throws
IndexOutOfBoundsException if index is negative, or greater than
the size of the list. get(index:int):E Returns the element at the
given index, or throws IndexOutBoundsException if index is
negative or greater than or equal to the size of the list.
indexOf(o:Object):int Returns the least (first) index at which the
object o is found; returns -1 if o is not in the list.
lastIndexOf(o:Object):int Returns the greatest (last) index at which
the object o is found; returns -1 if o is not in the list.
listIterator():ListIterator< E> Returns an iterator specialized to
work with List collections. remove(index:int):E Removes and
returns the element at the given index; throws
IndexOutOfBoundsException if index is negative, or greater than or
equal to the size of the list. set(index:int, el:E):E Replaces the
element at index with the new element el.

You might also like