Java Collections: Understanding ArrayList, Set, List, Map, and their Operations, Slides of Data Structures and Algorithms

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

2012/2013

Uploaded on 04/23/2013

saratey
saratey 🇮🇳

4.3

(10)

86 documents

1 / 29

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d

Partial preview of the text

Download Java Collections: Understanding ArrayList, Set, List, Map, and their Operations and more Slides Data Structures and Algorithms in PDF only on Docsity!

A collection (sometimes called a

container

is simply an object that groups multipleelements into a single unit.

Collections are used to store, retrieve andmanipulate data, and to transmit data fromone method to another.

Examples:^ ◦

Arrays, hashtables, vector, set, map, tree

See others athttp://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html

Given an ArrayList A of objects write amethod removeDups that removes allduplicate elements from the list



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

reusable

data structures

.



AlgorithmsAlgorithmsAlgorithmsAlgorithms: methods that perform usefulcomputations, like searching and sorting, on objectsthat implement collection interfaces. These algorithmsare said to be

polymorphic

because the same method

can be used on many different implementations of theappropriate collections interface. In essence,algorithms are

reusable functionality

.

A Java interface contains specifications for aknown set of functions.

These functions are to be implemented byclasses^ ◦

Eg:

public class foo implements List {

Interfaces from java collections framework

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

List

is an ordered

Collection

(sometimes called

a

sequence

Lists may contain duplicate elements. Inaddition to the operations inherited from Collection

, the

List

interface includes

operations for:

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

range operations

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