lecture-32 - Lists and Maps The java.util library contains...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Lists and Maps The java.util library contains high-power classes for maintaining a collection of objects. These classes are collectively referred to as the Java Collection Framework (JCF). Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 50 Lists The List interface is one useful feature that can help us manage large numbers of objects. Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 51 Lists Two classes in the JCF implement the List interface: ArrayList LinkedList The ArrayList class uses an array to manage data. The LinkedList class uses a technique called linked-node representation. Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 52 Lists To use the List interface, we declare the variable as List and assign an instance of the class that implements the List interface to it: List myList; ... myList = new ArrayList( ); This approach permits a quick change of the implementation class if necessary. Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 53 Lists The default constructor will create an empty list with an initial capacity of 10. If we add more than 10 objects, a list object will increase its capacity automatically. It is possible to increase the capacity of a list. However, it is better to create a list with the actual capacity we need, if we know in advance what that capacity is. This is done by calling the second constructor of ArrayList. Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 54 Lists The add method allows us to add objects to the list. To find out the number of objects contained in a list, we use its size method. Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 55 Lists The remove method takes an element’s index as its parameter and allows us to remove an element from a list. The get method allows us to access objects stored in a list by giving their index position in the list. Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 56 Lists import java.util.* ... List friends; Person person; friends = new ArrayList(); person = new Person(“Jane”,10,‘F’); friends.add(person); person = new Person(“Jack”,6,‘M’); friends.add(person); person = new Person(“Jill”,8,‘F’); friends.add(person); Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 57 Lists System.out.println(friends.size()); Person p = (Person) friends.get(i); Notice that we need to type cast the element to a Person object because the elements inside a list can be an instance of any class. A compile-time error will occur if we forget to type cast the returned element. Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 58 Lists The following code removes the third Person object from friends: friends.remove(2) All elements after the removed element are moved one position to fill the hole created by the removal of the second element. Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 59 Lists Person p; int limit = friends.size(); for(int i = 0; i < limit; i++) { p = (Person) friends.get(i); System.out.println(p.getName()); } Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 60 Lists The iterator method as well allows us to scan the elements inside a list or other JCF classes. When we call a list’s iterator method, an Iterator object (an instance of a class that implements the Iterator interface) that supports two methods, hasNext and next, is returned. hasNext returns true if the iterator has more elements to access. Calling next if there are no more elements to access will result in an error. Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 61 Lists Iterator itr = friends.iterator(); while( itr.hasNext()) { p = (Person) itr.next(); System.out.println(p.getName()); } Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 62 Lists Using iterator method we can access the elements only in one direction. The iterator method is supported by many other java.util classes. Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 63 Lists A list cannot include primitive data values as its elements, but wrapper classes can adapt the values to acceptable list objects. List intList = new ArrayList(); intList.add(new Integer(15)); intList.add(new Integer(20)); Integer intObject; Iterator itr = intList.iterator(); while(itr.hasNext()) { intObject = (Integer) itr.next(); System.out.println(intObject.intValue()); } Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 64 Maps Two classes implement the Map interface: TreeMap HashMap TreeMap implements a subinterface of Map called SortedMap, where the entries in the map are sorted. Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 65 Maps A map consists of entries. Each entry is divided into two parts: key value Duplicate keys are not allowed in the map. Both the key and the value may be instances of any class. Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 66 Maps A map may be characterized as an expandable array with instances of any class as its indices. The main advantage of a map is its performance in locating an entry, given the key. Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 67 Maps We create an instance of a map: Map table; ... table = new TreeMap(); We add the key-value pairs to the map: table.put(“IT114”, “Great course.”); The first argument is the key. The second argument is the value. Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 68 Maps To remove an entry from a map, we use its remove method and pass the key as the argument. table.remove(“CS0233”); If there is no matching entry, then nothing happens to the map but the value null is returned. To retrieve the value associated to a key: String courseEval = table.get(“CS102”); To check whether the map contains an evaluation for course number IT114: boolean result = table.containsKey(“IT114”); Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 69 Maps To access all entries in a map, we use the entrySet method which returns an object of the interface Set to obtain a set of entries, then use the iterator method as in the ArrayList example. Set entry = table.entrySet(); Set is another interface in JCF. It supports the behaviour of mathematical sets, a collection of data with no duplicates. Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 70 Maps An entry of a map is an instance of the class that implements the Map.Entry interface. The dot notation indicates that the Entry interface is defined inside the declaration of the Map interface. Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 71 Maps The getKey and getValue methods are two particularly useful methods in the interface Map.Entry. These method retrieve the map entry’s key and value, respectively. Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 72 Maps Iterator itr = table.entrySet().iterator(); while(itr.hasNext()) { entry = (Map.Entry) itr.next(); System.out.println(entry.getKey() + “:”); System.out.println( entry.getValue() + “\n”); } Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 73 WordList class Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 75 Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 76 ...
View Full Document

Ask a homework question - tutors are online