Sie sind auf Seite 1von 25

COLLECTIONS

framework

Overview
2

A Collection is a group of objects. Package: java.util.*; Uses of util package


Generating random numbers Manipulating date and time

Observing events
Manipulating sets of bits Tokenizing strings

Frameworks Goals
3

had to be high performance has to allow different type of collections to work

similar manner with high degree of interoperability

Algorithms
Static methods in Collections class

Iterator

Gives general purpose standardized way of accessing elements in Collection, one at a time. Stores key/value pair.

Maps

Collection Hierarchy
4

Collection [interface]

Map [interface]

List [interface]

Set [interface] SortedSet [interface]

HashMap [class]

TreeMap [class]

ArrayList [class]

LinkedList [class]

TreeSet [class]

HashSet [class]

Collection interfaces
5

Collection - Enables to work with group of objects

List - Handles sequences


Set - Handles sets-unique

SortedSet - Handles Sorted Set


Comparator - Compares two objects Iterator,ListIterator - Enumerate the objects RandomAccess - Indicates Random Access

Collection interface
6

Basic interface for all collections It declares all main methods

All collections implements Collection interface


Primitive types cant be stored

Exceptions:

UnSupportedOperationException ClassCastException

Collection methods
7

interface Collection{

boolean add(Object);
boolean remove(Object); boolean contains(Object); boolean isEmpty(); Iterator iterator(); boolean equals(Object); }

List interface
8

Extends Collection interface Elements can be inserted/accessed by their positions Allows duplicate elements

interface List{ Object get(int);

Object set(int, Object);


int indexOf(Object); ListIterator listIterator();

List subList(int, int);


}

Set, SortedSet interfaces


9

Set

Extends Collection interface Does not allow duplicate elements

SortedSet

Extends Set interface null is not allowed Exceptions:


NoSuchElementException ClassCastException NullPointerException

Collection Classes
AbstractCollection - Implements Collection interface. AbstractList - Extends AbstractCollection and implements List
10

interface. AbstractSequentialList - Extends AbstractList for use by a collection that uses sequential rather than random access of its elements. LinkedList - Implements a linked list by extending AbstractSequentialList. ArrayList - Implements a dynamic array by extending AbstractList. AbstractSet - Extends AbstractCollection and implements Set interface. HashSet - Extends AbstractSet for use with a hash table. LinkedHashSet - Extends HashSet to allow insertion-order iterations. TreeSet - Implements a set stored in a tree. Extends AbstractSet.

ArrayList
11

An ArrayList is a variable-length array of object

references. That is, an ArrayList can dynamically increase or decrease in size. Constructors:

ArrayList( ) ArrayList(Collection c) ArrayList(int capacity)

Example
12

// Demonstrate ArrayList. import java.util.*; class ArrayListDemo { public static void main(String args[]) { // create an array list ArrayList al = new ArrayList(); System.out.println("Initial size of al: "+al.size()); // add elements to the array list al.add("C"); al.add("A"); al.add("E"); al.add("B"); al.add("D"); al.add("F");

13

al.add(1, "A2"); System.out.println("Size of al after additions: " + al.size()); // display the array list System.out.println("Contents of al: " + al); // Remove elements from the array list al.remove("F"); al.remove(2); System.out.println("Size of al after deletions: " + al.size()); System.out.println("Contents of al: " + al); } }

Example
import java.util.*;
14

class A{ int x; void sum() { System.out.println("Class A } } class B{ int y; void test() { System.out.println("Class B } }

"+x);

"+y);

class ALDemo1{ public static void main(String args[]){ A o1=new A(); 15 o1.x=500; B o2=new B(); o2.y=700; ArrayList al=new ArrayList(); al.add(o1); al.add(o2); Object s1=al.get(0); Object s2=al.get(1); A ss1=(A)s1; ss1.sum(); B ss2=(B)s2; ss2.test(); } }

Example
16

import java.util.*; class ALDemo { public static void main(String args[]) { String s1=new String("Hai"); String s2=new String("Gud"); String s3=new String("Morning"); String s4=new String("Welcome"); ArrayList al=new ArrayList(); al.add(s1); al.add(s2);

17

al.add(s3); al.add(s4); Object o1=null; Iterator ie=al.iterator(); while(ie.hasNext()) { o1=ie.next(); System.out.println(" "+o1); } } }

LinkedList
18

It provides a linked-list data structure. Constructors: LinkedList( ) LinkedList(Collection c) Special methods: void addFirst(Object obj) void addLast(Object obj) Object getFirst( ) Object getLast( ) Object removeFirst( ) Object removeLast( )

Example
19

// Demonstrate LinkedList. import java.util.*; class LinkedListDemo { public static void main(String args[]) { // create a linked list LinkedList ll = new LinkedList(); ll.add("F"); ll.add("B"); ll.add("D"); ll.add("E"); ll.add("C"); ll.addLast("Z"); ll.addFirst("A"); ll.add(1, "A2");

20

System.out.println("Original contents of ll: " + ll); // remove elements from the linked list ll.remove("F"); ll.remove(2); System.out.println("Contents of ll after deletion:"+ll); // remove first and last elements ll.removeFirst(); ll.removeLast(); System.out.println("ll after deleting first & ast:"+ll); // get and set a value Object val = ll.get(2); ll.set(2, (String) val + " Changed"); System.out.println("ll after change: " + ll); } }

HashSet
21

It creates a collection that uses a hash table for

storage. The advantage of hashing is that it allows the execution time of basic operations, such as add( ), contains( ), remove( ), and size( ), to remain constant even for large sets. Constructors:

HashSet( ) HashSet(Collection c) HashSet(int capacity) HashSet(int capacity, float fillRatio)

Example
22

import java.util.*; class HSDemo { public static void main(String args[]) { HashSet hs=new HashSet(); hs.add("E"); hs.add("D"); hs.add("C"); hs.add("B"); hs.add("A"); System.out.println(" "+hs); } }

TreeSet
23

Objects are stored in sorted, ascending order. Access and retrieval times are quite fast. Constructors: TreeSet( ) TreeSet(Collection c) TreeSet(Comparator comp) TreeSet(SortedSet ss)

Example
24

import java.util.*; class TSDemo { public static void main(String args[]) { TreeSet ts=new TreeSet(); ts.add("H"); ts.add("G"); ts.add("F"); ts.add("E"); ts.add("D");

25

ts.add("C"); ts.add("B"); ts.add("A"); System.out.println(" System.out.println(" System.out.println(" System.out.println(" System.out.println(" System.out.println(" System.out.println(" } }

"+ts); "); "+ts.headSet("D")); "); "+ts.tailSet("D")); "); "+ts.subSet("D","G"));

Das könnte Ihnen auch gefallen