Introduction to Java 2 Programming Lecture 5 The

  • Slides: 21
Download presentation
Introduction to Java 2 Programming Lecture 5 The Collections API

Introduction to Java 2 Programming Lecture 5 The Collections API

Overview • Collection classes – Types of collection – Working with Collections – Sorting

Overview • Collection classes – Types of collection – Working with Collections – Sorting

Collections • Implementations of common data structures, such as Linked Lists, Sets, etc. –

Collections • Implementations of common data structures, such as Linked Lists, Sets, etc. – Part of the java. util package. – Also known as “containers” • Advantages – Can hold any kind of object – Much more flexible than arrays • Disadvantages – Not as efficient as arrays (for some uses) – Can’t store primitive types – Not type-safe. Store references to Object

Types of Collection • Two Types of Containers • Collections – Group of objects,

Types of Collection • Two Types of Containers • Collections – Group of objects, which may restricted or manipulated in some way – E. g. ordered to make a List or Linked. List – E. g. a Set, an unordered group which can only contain one of each item • Maps – Associative array, Dictionary, Lookup Table, Hash – A group of name-value pairs

Java Collections

Java Collections

Collection Implementations • Several implementations associated with each of the basic interfaces • Each

Collection Implementations • Several implementations associated with each of the basic interfaces • Each has its own advantages/disadvantages • Maps – Hash. Map, Sorted. Map • Lists – Array. List, Linked. List • Sets – Hash. Set, Sorted. Set

The Basics • Hash. Map and Array. List are most commonly encountered • Usual

The Basics • Hash. Map and Array. List are most commonly encountered • Usual object creation syntax • Always refer to the objects via one of the Collections interfaces – Take advantage of polymorphism List my. List = new Array. List(); List other. List = new Array. List(5); Map database = new Hash. Map(); Set things = new Hash. Set();

Adding Items • For Collections, use add() List my. List = new Array. List();

Adding Items • For Collections, use add() List my. List = new Array. List(); my. List. add(“A String”); my. List. add(“Other String”); • For Maps, use put() Map my. Map = new Hash. Map(); my. Map. put(“google”, “http: //www. google. com”); mp. Map. put(“yahoo”, “http: //www. yahoo. com”);

Copying • Very easy, just use add. All() List my. List = new Array.

Copying • Very easy, just use add. All() List my. List = new Array. List(); //assume we add items to the list List other. List = new Array. List(); my. List. add. All(my. List);

Getting Individual Items • Use get() • Note that we have to cast the

Getting Individual Items • Use get() • Note that we have to cast the object to its original type. • Collections… String s = (String)my. List. get(1); //get first element String s 2 = (String)my. List. get(10); //get tenth element • Maps… String s = (String)my. Map. get(“google”); String s 2 = (String)mp. Map. get(“yahoo”);

Getting All items • For Lists, we could use a for loop, and loop

Getting All items • For Lists, we could use a for loop, and loop through the list to get() each item • But this doesn’t work for Maps. • To allow generic handling of collections, Java defines an object called an Iterator – An object whose function is to walk through a Collection of objects and provide access to each object in sequence

Getting All items • Get an iterator using the iterator() method • Iterator objects

Getting All items • Get an iterator using the iterator() method • Iterator objects have three methods: – next() – gets the next item in the collection – has. Next() – tests whether it has reached the end – remove() – removes the item just returned • Basic iterators only go forwards – Lists objects have a List. Iterator that can go forward and backward

Getting All items (List) • Simple example: List my. List = new Array. List();

Getting All items (List) • Simple example: List my. List = new Array. List(); //we add items Iterator iterator = my. List. iterator(); while (iterator. has. Next()) { String s = (String)iterator. next(); //do something with it }

Getting All Items (Map) • Example of using a Map • Note that we

Getting All Items (Map) • Example of using a Map • Note that we can get a Set of all keys (key. Set) or Collection of all values (values) Map my. Map = new Hash. Map(); //we add items Iterator iterator = my. Map. key. Set. iterator(); while (iterator. has. Next()) { String the. Key = (String)iterator. next(); Object the. Value = my. Map. get(the. Key); //do something useful }

Other Functions • The java. util. Collections class has many useful methods for working

Other Functions • The java. util. Collections class has many useful methods for working with collections – min, max, sort, reverse, search, shuffle • Virtually all require your objects to implement an extra interface, called Comparable

Comparable • The Comparable interface labels objects that can be compared to one another.

Comparable • The Comparable interface labels objects that can be compared to one another. – Allows sorting algorithms to be written to work on any kind of object – so long as they support this interface • Single method to implement public int compare. To(Object o); • Returns – A negative number of parameter is less than the object – Zero if they’re equal – A positive number if the parameter is greater than the object

Comparable Example public class Person implements Comparable { private String email; private String last.

Comparable Example public class Person implements Comparable { private String email; private String last. Name; public int compare. To(Object object) { Person other = (Person)object; //compare based on email address only return other. get. Email(). compare. To(email); } }

Comparable Example Person a = new Person(“me@ldodds. com”, Dodds”); Person b = new Person(“bob@builder.

Comparable Example Person a = new Person(“me@ldodds. com”, Dodds”); Person b = new Person(“bob@builder. com”, “Builder”); List people = new Array. List(); People. add(a); People. add(b); Java. util. Collections. sort(people); //collection is now sorted by email

Comparator • Like Comparable, but is a stand-alone object used for comparing other objects

Comparator • Like Comparable, but is a stand-alone object used for comparing other objects – Useful when you want to use your criteria, not that of the implementor of the object. – Or altering the behaviour of a system • Many of the methods in the Collections object also allow a Comparator to be specified • Again method has single method: public int compare(Object obj 1, Object obj 2)

Comparator Example • Java String comparison is lexicographic not alphabetic, I. e. based on

Comparator Example • Java String comparison is lexicographic not alphabetic, I. e. based on the character set, not alphabetic order public class Alpha. Comparison implements Comparator { public int compare(Object obj 1, Object obj 2) { String s 1 = ((String)o 1). to. Lower. Case(); String s 2 = ((String)o 2). to. Lower. Case(); return s 1. compare. To(s 2); } }

Comparator Example String one = “One”; String two = “Two”; String three = “Three”;

Comparator Example String one = “One”; String two = “Two”; String three = “Three”; List strings = new Array. List(); strings. add(one); strings. add(two); strings. add(three); Collections. sort(strings, new Alpha. Comparison()); //now in alphabetical order