





















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of java collections, focusing on arraylist, set, list, and map interfaces, their implementations, and various operations such as adding, removing, searching, and sorting elements. It also covers the benefits of using java collections and provides examples of using hashset to remove duplicates and implementing a frequency table.
Typology: Slides
1 / 29
This page cannot be seen from the preview
Don't miss anything!






















See others athttp://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html
InterfacesInterfacesInterfacesInterfaces: abstract data types representing collections.Interfaces allow collections to be manipulatedindependently of the details of their representation. Inobject-oriented languages like Java, these interfacesgenerally form a hierarchy.
ImplementationsImplementations: concrete implementations of theImplementationsImplementationscollection interfaces. In essence, these are
.
AlgorithmsAlgorithmsAlgorithmsAlgorithms: methods that perform usefulcomputations, like searching and sorting, on objectsthat implement collection interfaces. These algorithmsare said to be
because the same method
can be used on many different implementations of theappropriate collections interface. In essence,algorithms are
.
Java does not allow above
Here is a hack
^
Set is a collection with NO order
^
Similar to the Mathematical Abstraction of Set public interface Set {
// Basic Operations// Basic Operations// Basic Operations// Basic Operations^ int size();boolean isEmpty();boolean contains(Object element);boolean add(Object element); // Optional// Optional// Optional// Optional^ boolean remove(Object element);Iterator iterator();boolean removeAll(Collection c);boolean retainAll(Collection c);void clear(); // Bulk Operations// Bulk Operations// Bulk Operations// Bulk Operations^ boolean containsAll(Collection c);boolean addAll(Collection c);//Array OperationsObject[] toArray(); Object[] toArray(Object a[]); } ^
Example:
Collections noDups = new HashSet(c );
import java.util.*;public class FindDups {
public static void main(String args[]) {
Set s = new HashSet();for (int i=0; i<args.length; i++)
if (!s.add(args[i]))
System.out.println("Duplicate detected: "+args[i]);
System.out.println(s.size()+" distinct words detected: "+s); } } ^
What is the output of : java FindDups I am sam sam I am
Index based Access: manipulate elements based on theirnumerical position in the list.
Search: search for a specified object in the list and returnits index position.
List Iteration: extend
Iterator
semantics to take advantage of
the list's sequential nature.
Range-view: perform arbitrary
on the list.
public interface List extends Collection{
// index based Access
Object get(int index);Object set(int index, Object element); // Optional void add(int index, Object element);Object remove(int index);abstract boolean addAll(int index, Collection c); //Search int indexOf(Object o);int lastIndexOf(Object o);ListIterator listIterator();ListIterator listIterator(int index); // Range-view List subList(int from, int to); }
Iterators allow collection to be accessed using a pre-defined“pointer”
public interface ListIterator extends Iterator {
boolean hasNext();Object next();boolean hasPrevious();Object previous();int nextIndex();int previousIndex();void remove();// Optionalvoid set(Object o);void add(Object o);
}
What is the purpose of the following code?for (ListIterator i=L.listIterator(l.size()); i.hasPrevious(); )
{ Foo f = (Foo) i.previous(); ... }
◦^
Steps through a list backwards