Download presentation
Presentation is loading. Please wait.
Published byTimothée St-Arnaud Modified over 6 years ago
2
Back to Collections Lists: Sets Maps ArrayList LinkedList
No duplicates No order (TreeSets happen to have an order) Maps Mapping a key to a value E.g., our soccer players: map each player to a position Each player is unique, each position doesn’t have to be The player’s name should bring up their position
3
Map The Map interface maps unique keys to values. A key is an object that you use to retrieve a value at a later date. Given a key and a value, you can store the value in a Map object. After the value is stored, you can retrieve it by using its key. Examples: web page url (key) -> web page content (value) Word (key) -> count of word in document (value) Word(key)-> List of words that follow that word (value) Student name (key) -> class schedule (value) ClassID(key)->roster(list) of students (value)
4
Map Methods void clear( ) - Removes all key/value pairs from the map.
Maps keys to values No duplicate keys Each key maps to a value Methods: void clear( ) - Removes all key/value pairs from the map. boolean containsKey(Object k) - Returns true if the invoking map contains k as a key. Otherwise, returns false. boolean containsValue(Object v) - Returns true if the map contains v as a value. Otherwise, returns false. Set entrySet( ) - Returns a Set that contains the entries in the map. In essence, this is turning the Map into a set (with each key-value pair as an object) Object get(Object k) - Returns the value associated with the key k. boolean isEmpty( ) - Returns true if the invoking map is empty. Otherwise, returns false. Set keySet( ) - Returns a Set that contains the keys from the map Object put(Object k, Object v) - Puts an entry in the map, overwriting any previous value associated with the key. The key and value are k and v, respectively. Returns null if the key did not already exist. Otherwise, the previous value linked to the key is returned. void putAll(Map m) - Puts all the entries from m into this map. Object remove(Object k) - Removes the entry whose key equals k. int size( ) - Returns the number of key/value pairs in the map.
5
Map Implementations HashMap (uses hashing function on keys)
Fast TreeMap (orders keys) Sorted ordering Key-ordered iteration LinkedHashMap (guess) Insertion-order iteration
6
Declaring Maps Declare types for both keys and values Class HashMap<K,V> HashMap<String, ArrayList<String>> map = new HashMap(); What type is the key? What type is the value?
7
HashMap<Integer, String> map = new HashMap(); map
HashMap<Integer, String> map = new HashMap(); map.put(21, "Twenty One"); // which is the key, and which is the value? //map.put(21.0, "Twenty One"); //this will throw an error because 21.0 is not integer Integer key = 21; String value = map.get(key); // gets value associated with key System.out.println("Key: " + key +" value: "+ value); map.put(31, "Thirty One"); System.out.println("Size of Map: " + map.size()); System.out.println("Does HashMap contains 21 as key: " + map.containsKey(21)); System.out.println("Does HashMap contains 21 as value: " + map.containsValue(21)); System.out.println("Does HashMap contains Twenty One as value: " + map.containsValue("Twenty One")); map.put(41, "Thirty One"); System.out.println("Unsorted HashMap: " + map); TreeMap sortedHashMap = new TreeMap(map); // cool stuff System.out.println("Sorted HashMap: " + sortedHashMap); map.clear(); //clears hashmap , removes all element System.out.println("Is HashMap is empty: " + map.isEmpty());
8
Looping through map: How could this go wrong?
More than one way: for (Integer key : map.keySet()) { … //code goes here System.out.println("Key : " + key.toString() + " Value : "+ map.get(key)); } How could this go wrong? we should use an Iterator
9
Traversing Collections (1)
For-each loop: for (Object o : collection) System.out.println(o); Equivalent to: for (Iterator i = collection.iterator(); i.hasNext();) { Object o = i.next(); } Everything that inherits from Collections Interface has an iterator We should usually use the iterator: more efficient and better way to traverse the objects in a collection
10
How to use an iterator: Iterator enables you to cycle through a collection obtaining or removing elements ListIterator extends Iterator to allow bidirectional traversal of a list Can go backwards and forward in list. To use an iterator: Obtain an iterator (pointer) to the start of the collection call the collection's iterator( ) method. Set up a loop that makes a call to hasNext( ) Have the loop iterate as long as hasNext( ) returns true. Within the loop, obtain each element by calling next( ).
11
Traversing Collections: Iterators
Java Interface Object next() get the next element boolean hasNext() are there more elements? void remove() removes the previous element Only safe way to remove elements during iteration
12
public static void main(String args[]) {
ArrayList al = new ArrayList(); // Create an array list al.add("C"); // add elements to the array list al.add("A"); al.add("E"); al.add("B"); al.add("D"); al.add("F"); System.out.print("Original contents of al: "); Iterator itr = al.iterator(); // Use iterator to display contents of al while(itr.hasNext()) { Object element = itr.next(); System.out.print(element + " "); } System.out.println(); ListIterator litr = al.listIterator(); while(litr.hasNext()) { // Modify objects being iterated Object element = litr.next(); litr.set(element + "+"); // concatenates “+” to each String System.out.print("Modified contents of al: "); itr = al.iterator(); // sets itr to initial address of ArrayList (i.e., first element in list) System.out.print("Modified list backwards: "); while(litr.hasPrevious()) { // Now, display the list backwards Object element = litr.previous();
13
ListIterator methods:
void add(Object obj) Inserts obj into the list in front of the element that will be returned by the next call to next( ). boolean hasNext( ) Returns true if there is a next element. Otherwise, returns false. boolean hasPrevious( ) Returns true if there is a previous element. Otherwise, returns false. Object next( ) Returns the next element. A NoSuchElementException is thrown if there is not a next element. int nextIndex( ) Returns the index of the next element. If there is not a next element, returns the size of the list. Object previous( ) Returns the previous element. A NoSuchElementException is thrown if there is not a previous element. int previousIndex( ) Returns the index of the previous element. If there is not a previous element, returns -1. void remove( ) Removes the current element from the list. An IllegalStateException is thrown if remove( ) is called before next( ) or previous( ) is invoked. void set(Object obj) Assigns obj to the current element. This is the element last returned by a call to either next( ) or previous( ).
14
Filter Algorithm void filter(Collection c) {
Iterator i = c.iterator(); while( i.hasNext() ) { // if the next element does not // adhere to the condition, remove it if (!cond(i.next())) { i.remove(); }
15
TreeSets: How do TreeSets work?
What makes TreeSets unique compared to HashSets? What if we put student objects into the tree? How about car objects?
16
Comparable Remember toString() method? Write it for every class
You decide what should be the string representation of the object you are creating Java assumes a toString method in printing Every object has a built-in toString method Remember System.out.println(myObject); // gives you: You overrode the built in toString method by writing your own toString method for the classes you were creating. Then everywhere you need a String representation of the object, the string returned from the toString method will be used
17
Comparables You now get to do something similar for comparing objects:
You decide what will make one object greater than, less than, or equal to another object of the same class. So you decide if you want to compare students by last name, by lab score, by total score, or whatever you want.
18
public class Student2 implements Comparable<Student2> { String f,l; int g1,g2,g3; int total; public Student2(String f1, String l1, int g11, int g21, int g31) { f = f1; l = l1; g1 = g11; g2 = g21; g3 = g31; total = getTotal(); } public int compareTo(Student2 s2) { System.out.println(total + " " + s2.getTotal()); if (total > s2.getTotal()) { return 1; else if (total < s2.getTotal()) { return -1; else { return 0; } } private int getTotal() { return(g1 + g2 + g3); } public String toString() { String s = ""; s += l + " "+f+": "+total; return(s);
19
A couple of comments… The comparable interface only has one method – compareTo() All primitive wrapper classes implement the comparable interface Wrapper classes = Integer, Double, Character Our way of making objects out of primitive types CompareTo should return an int: 1 when comparing x to y, x is greater -1 when comparing x to y, y is greater 0 when x and y are considered equal You must write compareTo for every class with which you want to use Arrays.sort or any of the Collections sort method.
20
public static void main(String[] args) { Student2 s = new Student2("Harry","Jones",20,20,20); Student2 s2 = new Student2("Anne","Rivers",50,50,50); if (s.compareTo(s2)==1) { System.out.println("s is greater "); } else if (s.compareTo(s2)==-1) { System.out.println("s2 is greater"); else { System.out.println("equal"); } 60 150 s2 is greater
21
public static void main(String[] args) { Student2 s = new Student2("Harry","Jones",20,20,20); Student2 s2 = new Student2("Anne","Rivers",50,50,50); Student2 s3 = new Student2("Bill","Young",30,30,30); Student2 s4 = new Student2("Lisa","Smith",40,40,40); Student2 s5 = new Student2("Joe","Banks",10,10,10); TreeSet<Student2> tree = new TreeSet(); tree.add(s); tree.add(s2); tree.add(s3); tree.add(s4); tree.add(s5); System.out.println(tree); [Banks Joe: 30, Jones Harry: 60, Young Bill: 90, Smith Lisa: 120, Rivers Anne: 150]
22
public class Student2 implements Comparable<Student2> { String f,l; int g1,g2,g3; int total; public Student2(String f1, String l1, int g11, int g21, int g31) { f = f1; l = l1; g1 = g11; g2 = g21; g3 = g31; total = getTotal(); } public int compareTo(Student2 s2) { return(l.compareTo(s2.l)); private int getTotal() { return(g1 + g2 + g3); } public String toString() { String s = ""; s += l + " "+f+": "+total; return(s); } }
23
public static void main(String[] args) {
Student2 s = new Student2("Harry","Jones",20,20,20); Student2 s2 = new Student2("Anne","Rivers",50,50,50); Student2 s3 = new Student2("Bill","Young",30,30,30); Student2 s4 = new Student2("Lisa","Smith",40,40,40); Student2 s5 = new Student2("Joe","Banks",10,10,10); TreeSet<Student2> tree = new TreeSet(); tree.add(s); tree.add(s2); tree.add(s3); tree.add(s4); tree.add(s5); System.out.println(tree); [Banks Joe: 30, Jones Harry: 60, Rivers Anne: 150, Smith Lisa: 120, Young Bill: 90]
24
Equals Why can’t we do: When will this be equal?
Student2 s = new Student2("Harry","Jones",20,20,20); Student2 sa = new Student2("Harry","Jones",20,20,20); if (s == sa) { System.out.println("They're equal"); } else { System.out.println("Not equal"); When will this be equal?
25
Equals You can use the compareTo method to determine equality
compareTo should return 0 when what you’ve determined to be equality is encountered We often write equals as well. Like toString There’s a built-in equals, but it always compares the actual object In other words, if they’re not literally the same object, they won’t be considered equal. We can override the built-in equals.
26
public class Student2 implements Comparable<Student2> { String f,l; int g1, g2, g3, total; public Student2(String f1, String l1, int g11, int g21, int g31) { f = f1; l = l1; g1 = g11; g2 = g21; g3 = g31; total = getTotal(); } public boolean equals(Student2 s2) { return(f.equals(s2.f)&&l.equals(s2.l) &&g1==s2.g1 && g2==s2.g2 && g3==s2.g3); public int compareTo(Student2 s2) { System.out.println(total + " " + s2.getTotal()); return(l.compareTo(s2.l)); private int getTotal() { int t = g1 + g2 + g3; return(t); public String toString() { String s = ""; s += l + " "+f+": "+total; return(s); } }
27
public static void main(String[] args) {
Student2 s = new Student2("Harry","Jones",20,20,20); Student2 sa = new Student2("Harry","Jones",20,20,20); if (s == sa) { System.out.println("They're equal"); } else { System.out.println("Not equal"); if (s.equals(sa)) { System.out.println("they're equal"); System.out.println("nope"); Not equal they're equal
28
As a general rule In general, it is considered good coding practice to make equals and compareTo compare equally. It just makes sense… People expect it… So the compareTo function should’ve been:
29
compareTo and equals public boolean equals(Student2 s2) {
return (f.equals(s2.f)&&l.equals(s2.l) && g1==s2.g1 && g2==s2.g2 && g3==s2.g3); } public int compareTo(Student2 s2) { if (total > s2.getTotal()) { return 1; } else if (total < s2.getTotal()) { return -1; else { if (l.compareTo(s2.l) != 0) { return l.compareTo(s2.l); else if (f.compareTo(s2.f) != 0){ return f.compareTo(s2.f); else if (g1>s2.g1) { return 1; } else if (g1 < s2.g1) {return -1; } else if (g2>s2.g2) { return 1; } else if (g2 < s2.g2) {return -1; } else if (g3>s2.g3) { return 1; } else if (g3 < s2.g3) {return -1; } else { return 0; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.