Collections A First Glimpse Collections n A collection

  • Slides: 15
Download presentation
Collections A First Glimpse

Collections A First Glimpse

Collections n A collection is a structured group of objects n n An array

Collections n A collection is a structured group of objects n n 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 n n n 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 n Java supplies several types of Collection: n n Set: cannot

Types of Collection n Java supplies several types of Collection: n n 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: n n Map: a “dictionary” that associates keys with values, order is not important Sorted. Map: like a Map, but order is important 3

The Collections hierarchy 4

The Collections hierarchy 4

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

Collections are ADTs n n 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 n n n 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 5

Uniformity through interfaces n n Much of the elegance of the Collections Framework arises

Uniformity through interfaces n n 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): n n n boolean add(Object o) boolean is. Empty() boolean remove() int size() Object[] to. Array() Iterator iterator() 6

Vectors n n n The class Vector has been retrofitted to implement the Collection

Vectors n n n 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 7

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). 8

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 9

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 10

Conversions n n Tree. Set numerals =. . . ; Vector v = new

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

A little about generics n Before Java 5, if you wanted a Vector of

A little about generics n Before Java 5, if you wanted a Vector of Strings, you had to do it this way: n n In Java 5, the preferred way is now as follows: n n Vector strings = new Vector(); strings. add("The end"); String last. Added = (String)strings. last. Element(); Vector<String> strings = new Vector<String>(); strings. add("The end"); String last. Added = strings. last. Element(); Why? n n Type safety (errors caught at compile time, not run time) Convenience (no explicit casts required) 12

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

Back to arrays n n 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: n static void sort(Object[] a) n static int binary. Search(Object[] a, Object key) n static boolean equals(Object[] a, Object[] a 2) n static void fill(Object[] a, Object val) n n static void fill(Object[] a, Object val, int from, int to) static List as. List(Object[] a) 13

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

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

The End 15

The End 15