• No results found

Working with Java Collections

N/A
N/A
Protected

Academic year: 2021

Share "Working with Java Collections"

Copied!
25
0
0

Loading.... (view fulltext now)

Full text

(1)
(2)

Java 2 Collections

• Lists - linear, supporting many operations

• Sets - unordered, unique items

• Sorted sets - like sets, but items can be

visited in sorted order

(3)

Java 2 Collections

• Maps - unordered, items accessed by keys

• Sorted maps - same as maps, but items can

(4)

Java 2 Collection Interfaces

Collection

List Set Map

(5)

Java 2 Collection Classes

AbstractCollection

AbstractList AbstractSet AbstractMap

HashSet HashMap Object

AbstractSequentialList

TreeSet TreeMap ArrayList LinkedList

(6)

Lists and Sets

List list = new LinkedList(); // Add 1 to 10 to list for (int i = 1; i <= 10; i++)

(7)

Lists and Sets

List list = new LinkedList(); // Add 1 to 10 to list for (int i = 1; i <= 10; i++)

list.add("" + i);

Set set = new HashSet(); // Add 5 random ints to set for (int i = 1; i <= 5; i++)

set.add("" + (((int) (Math.random() * 10)) + 1);

(8)

Lists and Sets

List list = new LinkedList(); // Add 1 to 10 to list for (int i = 1; i <= 10; i++)

list.add("" + i);

Set set = new HashSet(); // Add 5 random ints to set for (int i = 1; i <= 5; i++)

set.add("" + (((int) (Math.random() * 10)) + 1);

for (int i = 0; i < list.size(); i++) // Display contents of list System.out.println(list.get(i));

(9)

Lists and Sets

List list = new LinkedList(); // Add 1 to 10 to list for (int i = 1; i <= 10; i++)

list.add("" + i);

Set set = new HashSet(); // Add 5 random ints to set for (int i = 1; i <= 5; i++)

set.add("" + (((int) (Math.random() * 10)) + 1);

for (int i = 0; i < list.size(); i++) // Display contents of list System.out.println(list.get(i));

Iterator iter = set.iterator(); // Display contents of set while (iter.hasNext())

System.out.println(iter.next());

(10)

Transferring Data

List list = new LinkedList(); // Add 1 to 10 to list for (int i = 1; i <= 10; i++)

list.add("" + i);

Set set = new HashSet(list.iterator()); // Transfer to a new set

(11)

Transferring Data

List list = new LinkedList(); // Add 1 to 10 to list for (int i = 1; i <= 10; i++)

list.add("" + i);

Set set = new HashSet(list); // Transfer to a new set

(12)

Transferring Data

List list = new LinkedList(); // Add 1 to 10 to list for (int i = 1; i <= 10; i++)

list.add("" + i);

Set set = new HashSet(); // Add 5 random ints to set for (int i = 1; i <= 5; i++)

set.add("" + (((int) (Math.random() * 10)) + 1);

list.addAll(set); // Add all data in set // to list

(13)

Filtering Data

List list = new LinkedList(); // Add 1 to 10 to list for (int i = 1; i <= 10; i++)

list.add("" + i);

Set set = new HashSet(); // Add 5 random ints to set for (int i = 1; i <= 5; i++)

set.add("" + (((int) (Math.random() * 10)) + 1);

list.removeAll(set); // Remove all data from list // that are in set

(14)

Filtering Data

List list = new LinkedList(); // Add 1 to 10 to list for (int i = 1; i <= 10; i++)

list.add("" + i);

Set set = new HashSet(); // Add 5 random ints to set for (int i = 1; i <= 5; i++)

set.add("" + (((int) (Math.random() * 10)) + 1);

list.retainAll(set); // Remove all data from list // that are not in set

(15)

The

Collection

Interface

boolean add(Object o) boolean addAll(Collection c) void clear() boolean contains(Object o) boolean containsAll(Collection c) boolean equals(Object o) int hashCode() boolean isEmpty() Iterator iterator() boolean remove(Object o) boolean removeAll(Collection c) boolean retainAll(Collection c) int size() Object[] toArray() Object[] toArray(Object[] a)
(16)

The

Collection

Interface

boolean add(Object o) boolean addAll(Collection c) void clear() boolean contains(Object o) boolean containsAll(Collection c) boolean equals(Object o) int hashCode() boolean isEmpty() Iterator iterator() boolean remove(Object o) boolean removeAll(Collection c) boolean retainAll(Collection c) int size() Object[] toArray() Object[] toArray(Object[] a)
(17)

Not All Collections

Implement

Collection

Collection

List Set Map

SortedSet SortedMap

(18)

One-Way Transfers

List list = new LinkedList(); // Add 1 to 10 to list for (int i = 1; i <= 10; i++)

list.add("" + i);

Tiny tiny = new ArrayTiny(list); // Allowed because List // extends Collection

List list2 = new LinkedList(tiny); // Not allowed because Tiny // does not extend Collection

(19)

Collection-View

A collection-view is an object that

– implements the

Collection

interface

– has a collection as a backing store which does

not implement the collection interface

– allows clients to use many of the

Collection

methods with such collections

(20)

A Collection-View Is

Similar to an Iterator

backing store

collection

backing store

collection

iterator

object

collection-view

object

client using

hasNext

,

next

client using

removeAll

,

retainAll

,

etc.

A collection-view allows an object to masquerade

as a collection

(21)

The Method

collectionView

Iterator iterator() // Returns an iterator

Collection collectionView() // Returns a collection-view

Any class that implements

collectionView

will be

compatible with the

Collection

interface without

implementing that interface

(22)

Using the Method

collectionView

List list = new LinkedList(); // Add 1 to 10 to list for (int i = 1; i <= 10; i++)

list.add("" + i);

Tiny tiny = new ArrayTiny(list); // Allowed because List // extends Collection

List list2 = new LinkedList(tiny); // Not allowed because Tiny // does not extend Collection

(23)

Using the Method

collectionView

List list = new LinkedList(); // Add 1 to 10 to list for (int i = 1; i <= 10; i++)

list.add("" + i);

Tiny tiny = new ArrayTiny(list); // Allowed because List // extends Collection

List list2 = new LinkedList(tiny); // Not allowed because Tiny // does not extend Collection List list3 = new LinkedList(tiny.collectionView()); // OK

(24)

Using the Method

collectionView

List list = new LinkedList(); // Add 1 to 10 to list for (int i = 1; i <= 10; i++)

list.add("" + i);

Tiny tiny = new ArrayTiny(list); // Allowed because List // extends Collection Collection view = tiny.collectionView(); // Save a view

list.addAll(view); // Add tiny’s items to list view.addAll(list); // Add list’s items to tiny

(25)

Using the Method

collectionView

List list = new LinkedList(); // Add 1 to 10 to list for (int i = 1; i <= 10; i++)

list.add("" + i);

Tiny tiny = new ArrayTiny(list); // Allowed because List // extends Collection Collection view = tiny.collectionView(); // Save a view

list.addAll(view); // Add tiny’s items to list view.addAll(list); // Add list’s items to tiny view.removeAll(list); // Throws an exception because

// TinyIterator does not support // remove

References

Related documents

Draw the corresponding truth table that produces 1 only when all the doors are closed. Determine

public interface List extends Collection { // adds the following to Collection boolean addAll(int index, Collection c);. Object

LANDMARK ESTATE AGENCY SERVICES Appoint a legal conveyancer • Optimus is a panel manager: provides legal conveyancers to buyers and sellers. • Works with estate agents, mortgage

public boolean endsWith( String suffix ) public boolean equals( Object anObject ).

The program must however if date value V exists in was array however must remove a first occurrence of V, and the high of array elements to copy.. Let us consider modelling the

Functions in the C programming Language ie in function call Void Function Definition Using Value Parameters Example C requires variable declarations at?. Thus in cipher

The cost of products sold within our refining segment included $24.7 million and $8.8 million in net realized and unrealized economic hedging gains for the three and nine months