Collections A First Glimpse Collections A collection is

  • Slides: 13
Download presentation
Collections A First Glimpse

Collections A First Glimpse

Collections • A collection is a structured group of objects – An array is

Collections • A collection is a structured group of objects – An array is a kind of collection – A Vector is a kind of collection – A linked list is a kind of collection • Java 1. 2 introduced the Collections Framework and provided many great implementations – Vectors have been redefined to implement Collection – Trees, linked lists, stacks, hash tables, and other classes are implementations of Collection – Arrays do not implement the Collection interfaces 2

Types of Collection • Java supplies several types of Collection: – Set: cannot contain

Types of Collection • Java supplies several types of Collection: – Set: cannot contain duplicate elements, order is not important – Sorted. Set: like a Set, but order is important – List: may contain duplicate elements, order is important • Java also supplies some “collection-like” things: – Map: a “dictionary” that associates keys with values, order is not important – Sorted. Map: like a Map, but order is important 3

Collections are ADTs • I’m not going to cover Collections just yet, but I

Collections are ADTs • I’m not going to cover Collections just yet, but I want to use them as an example • Collections are one of the best-designed parts of Java, because – They are elegant: they combine maximum power with maximum simplicity – They are uniform: when you know how to use one, you almost know how to use them all – You can easily convert from one to another 4

Uniformity through interfaces • Much of the elegance of the Collections Framework arises from

Uniformity through interfaces • Much of the elegance of the Collections Framework arises from the intelligent use of interfaces • For example, the Collection interface specifies (among many other operations): – boolean add(Object o) – boolean is. Empty() – boolean remove() – int size() – Object[] to. Array() – Iterator iterator() 5

Vectors • The class Vector has been retrofitted to implement the Collection interface •

Vectors • The class Vector has been retrofitted to implement the Collection interface • Vector supplies add(Object) and iterator() methods (among others) • Let’s look at creating a Vector and iterating through the elements 6

The Iterator interface iterator { // java. lang. util boolean has. Next(); // Returns

The Iterator interface iterator { // java. lang. util boolean has. Next(); // Returns true if the iteration has more // elements. Object next(); // Returns the next element in the // interation. void remove(); // Removes from the underlying collection // the last element returned by the // iterator (optional operation). 7

Using a Vector Collection numerals = new Vector(); numerals. add("one"); numerals. add("two"); numerals. add("three");

Using a Vector Collection numerals = new Vector(); numerals. add("one"); numerals. add("two"); numerals. add("three"); Iterator iter = numerals. iterator(); while (iter. has. Next()) { System. out. println(iter. next()); } Results: one two three 8

Using a Tree. Set Collection numerals = new Tree. Set(); numerals. add("one"); numerals. add("two");

Using a Tree. Set Collection numerals = new Tree. Set(); numerals. add("one"); numerals. add("two"); numerals. add("three"); Iterator iter = numerals. iterator(); while (iter. has. Next()) { System. out. println(iter. next()); } Results: one three two 9

Conversions • Vector v = new Vector(numerals); • Tree. Set ts = new Tree.

Conversions • Vector v = new Vector(numerals); • Tree. Set ts = new Tree. Set(v); 10

Back to arrays • Arrays are not part of the new Collections Framework, but

Back to arrays • Arrays are not part of the new Collections Framework, but they haven’t been ignored • Java 1. 2 introduced the new Arrays class, with some useful operations, for example: – static – static void sort(Object[] a) int binary. Search(Object[] a, Object key) boolean equals(Object[] a, Object[] a 2) void fill(Object[] a, Object val, int from, int to) – static List as. List(Object[] a) 11

What to remember • We haven’t really begun to study the Collections framework yet,

What to remember • We haven’t really begun to study the Collections framework yet, but— – You should learn to use the Iterator interface – You should examine and learn more about the Arrays package 12

The End 13

The End 13