chapter 37 slides
DESCRIPTION
Exposure Java-AB 2007. Chapter 37 Slides. Sets & Maps. PowerPoint Presentation created by: Mr. John L. M. Schram. From Materials Created by Mr. Leon Schram. AP Exam Alert. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/1.jpg)
![Page 2: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/2.jpg)
The HashSet, TreeSet, HashMap and TreeMap are four classes that are part of the Java AB subset, which may be tested on the AP Computer Science Examination.
AP Exam Alert
![Page 3: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/3.jpg)
Collections
A collection is a group of objects.
![Page 4: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/4.jpg)
Unordered Collections
An unordered collection stores elements without order.
![Page 5: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/5.jpg)
Bags
A bag is an unordered collection that can have duplicate elements.
![Page 6: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/6.jpg)
Sets
A set is an unordered collection without any duplicate elements.
![Page 7: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/7.jpg)
Java Collection Hierarchy
CollectionInterface
ListInterface
SetInterface
ArrayListclass
LinkedListclass
HashSetclass
TreeSetclass
![Page 8: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/8.jpg)
// Java3701.java// This program reviews the two <Set> implementations, which are the // <TreeSet> and <HashSet> classes, with the <add> method.import java.util.*;public class Java3701{
public static void main (String args[]){
System.out.println("\nJAVA3701.JAVA\n");int[ ] numbers = {10,90,20,80,30,70,40,60,50};Set<Integer> hSet = new HashSet<Integer>();Set<Integer> tSet = new TreeSet<Integer>();for (int number: numbers){ hSet.add(new Integer(number)); tSet.add(new Integer(number));}
System.out.println(hSet);System.out.println(tSet);System.out.println();
}}
![Page 9: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/9.jpg)
// Java3702.java// This program demonstrates that <Set> objects do not contain// duplicate elements like <List> objects. It also demonstrates that// <TreeSet> objects store elements in ascending order.
import java.util.*;
public class Java3702{
public static void main (String args[]){
System.out.println("\nJAVA3702.JAVA\n");int[] numbers = {23,43,49,61,23,50,49,18,75,18};
List<Integer> list = new ArrayList<Integer>();Set<Integer> hSet = new HashSet<Integer>();Set<Integer> tSet = new TreeSet<Integer>();for (int number: numbers){ list.add(new Integer(number)); hSet.add(new Integer(number)); tSet.add(new Integer(number));}System.out.println(list);
System.out.println(hSet);System.out.println(tSet);System.out.println();
}}
![Page 10: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/10.jpg)
Constructing a Set Object
HashSet<Integer> hSet = new HashSet<Integer>();TreeSet<Integer> tSet = new TreeSet<Integer>();
or you can use the preferred declaration below
Set<Integer> hSet = new HashSet<Integer>();Set<Integer> tSet = new TreeSet<Integer>();
![Page 11: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/11.jpg)
Set Method add
hSet.add(new Integer(1000));tSet.add(new Integer(2000));
Method add stores a new value in a Set object, provided the element is not already stored in the Set object.
![Page 12: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/12.jpg)
// Java3703.java// This program demonstrates how to use an <Iterator> object, with the// <next> method, to access every element in a <Set> object. // This is followed by using the <for..each> loop to access <Set> elements. // It also demonstrates the <size> method.import java.util.*;public class Java3703{
public static void main (String args[]){
System.out.println("\nJAVA3703.JAVA\n");Set<Integer> hSet = new HashSet<Integer>();Set<Integer> tSet = new TreeSet<Integer>();for (int k = 10; k < 100; k+= 10){ hSet.add(new Integer(k)); tSet.add(new Integer(k));}Iterator<Integer> hAccess = hSet.iterator();for (int k = 0; k < hSet.size(); k++) System.out.print(hAccess.next() + " ");System.out.println("\n\n");for (Integer element: tSet) System.out.print(element + " ");System.out.println("\n\n");}
}
![Page 13: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/13.jpg)
Constructing an Iterator Object
Iterator<Integer> hAccess = hSet.iterator();
The iterator method of a Collection class object (ArrayList, LinkedList, HashSet and TreeSet) instantiates an object of the Iterator class, in this case hAccess.
![Page 14: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/14.jpg)
Iterator Method next
System.out.print(hAccess.next()+" ");
Method next moves the iterator to the next
element, and then returns it.
![Page 15: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/15.jpg)
Set Method size
for (int k = 0; k < hSet.size(); k++)
Method size returns the number of elements in the Set object.
![Page 16: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/16.jpg)
// Java3704.java// This program demonstrates how to create a conditional loop with the // <hasNext> method of the <Iterator> class.import java.util.*;public class Java3704{
public static void main (String args[]){
System.out.println("\nJAVA3704.JAVA\n");Set<Integer> hSet = new HashSet<Integer>();Set<Integer> tSet = new TreeSet<Integer>();for (int k = 10; k < 100; k+= 10){ hSet.add(new Integer(k)); tSet.add(new Integer(k));}Iterator<Integer> hAccess = hSet.iterator();while (hAccess.hasNext()) System.out.print(hAccess.next() + " ");System.out.println("\n\n");Iterator<Integer> tAccess = tSet.iterator();while (tAccess.hasNext()) System.out.print(tAccess.next() + " ");
System.out.println("\n\n");}
}
![Page 17: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/17.jpg)
Iterator Method hasNext
while (iter.hasNext())
Method hasNext returns true if elements remain in the Collection object and returns false otherwise.
![Page 18: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/18.jpg)
// Java3705.java This program demonstrates the <remove> method of the <Iterator> class.import java.util.*;public class Java3705{
public static void main (String args[]){
System.out.println("\nJAVA3705.JAVA\n");Set hSet = new HashSet();Set tSet = new TreeSet();for (int k = 1; k <= 10; k++){
hSet.add(new Integer(k));tSet.add(new Integer(k));
}System.out.println("Set elements before using the <remove> method.");System.out.println(hSet);System.out.println(tSet);Iterator hAccess = hSet.iterator();Iterator tAccess = tSet.iterator();for (int k = 1; k <= 10; k++){
hAccess.next();tAccess.next();if (k % 2 == 0){
hAccess.remove();tAccess.remove();
}}System.out.println("\nSet elements after using the <remove> method.");System.out.println(hSet);System.out.println(tSet);System.out.println();
}}
![Page 19: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/19.jpg)
2 remove Methodsiter.remove();
Iterator method remove removes the current item referenced by the iterator.
hSet.remove(new Integer(k));
Set method remove removes the element specified in the parameter, if it exists in the Set object.
![Page 20: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/20.jpg)
// Java3706.java// This program demonstrates the <remove> method of the <Set> interface,// which is not the same as the <Iterator> <remove> method>.import java.util.*;public class Java3706{
public static void main (String args[]){
System.out.println("\nJAVA3706.JAVA\n");int[] numbers = {10,11,12,13,14,15,16,17,18,19,20};Set<Integer> hSet = new HashSet<Integer>();for (int number: numbers) hSet.add(new Integer(number));
System.out.println("Set elements before using the <remove> method.");for (Integer number: hSet) System.out.print(number + " ");for (int k = 10; k <= 20; k++){ if (k % 2 == 0)
hSet.remove(new Integer(k));}
System.out.println("\n\nSet elements after using the <remove> method."); for (Integer number: hSet)
System.out.print(number + " ");System.out.println();}
}
![Page 21: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/21.jpg)
// Java3707.java// This program demonstrates the <contains> method of the <Set> interface,
import java.util.*;
public class Java3707{
public static void main (String args[]){
System.out.println("\nJAVA3707.JAVA\n");Set<Integer> tSet = new TreeSet<Integer>();Random rndInt = new Random(12345);for (int k = 1; k <= 40; k++){ tSet.add(new Integer(rndInt.nextInt(90) + 10));}System.out.println("tSet Members");for (int k = 10; k <= 99; k++) if (tSet.contains(new Integer(k))) System.out.print(k + " ");
System.out.println();}
}
![Page 22: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/22.jpg)
Set Method contains
if (tSet.contains(new Integer(k)))
Method contains returns true if the parameter value exists in the Set object and false otherwise.
![Page 23: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/23.jpg)
![Page 24: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/24.jpg)
Venn Diagram #1The Boolean Algebra logical and ( * ) can be demonstrated with Venn Diagrams, using intersection.
A intersect B also A and B also A * B also AB
![Page 25: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/25.jpg)
Venn Diagram #2The Boolean Algebra logical or ( + ) can be demonstrated with Venn Diagrams, using union.
A union B also A or B also A + B
A B
![Page 26: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/26.jpg)
Why you did not learn Set Theory in your Math classes
After Sputnik, the United States adopted New Math in the classroom.
After test scores went down the focus switched back to the "3 Rs" (Reading, Riting & Rithmetic).Unfortunately, they stopped New Math completely just as Technology courses were being introduced.
![Page 27: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/27.jpg)
public class Java3708{
public static void main (String args[]){
System.out.println("\nJAVA3708.JAVA\n");
Random rnd = new Random(12345);
Set<Integer> s1 = new HashSet<Integer>();
for (int k = 1; k <= 5; k++)
{
Integer obj = new Integer(rnd.nextInt(10));
s1.add(obj);
}
System.out.println("s1 Elements: " + s1);
Set<Integer> s2 = new HashSet<Integer>();
for (int k = 1; k <= 5; k++)
{
Integer obj = new Integer(rnd.nextInt(10));
s2.add(obj);
}
System.out.println("s2 Elements: " + s2);
Set s3 = intersection(s1,s2);
System.out.println("\nIntersection of s1 and
s2: " + s3);
System.out.println();}
public static Set intersection (Set<Integer> s1, Set<Integer> s2)
{
Set<Integer> temp = new
HashSet<Integer>();
for (Integer number: s1)
if (s2.contains(number))
temp.add(number);
return temp;
}
8 1 2 0 5 4 9
![Page 28: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/28.jpg)
import java.util.*;public class Java3709{
public static void main (String args[]){
System.out.println("\nJAVA3709.JAVA\n");
Random rnd = new Random(12345);
Set<Integer> s1 = new HashSet<Integer>();
for (int k = 1; k <= 5; k++)
{
Integer obj = new Integer(rnd.nextInt(10));
s1.add(obj);
}
System.out.println("s1 Elements: " + s1);
Set<Integer> s2 = new HashSet<Integer>();
for (int k = 1; k <= 5; k++)
{
Integer obj = new Integer(rnd.nextInt(10));
s2.add(obj);
}
System.out.println("s2 Elements: " + s2);
Set s3 = union(s1,s2);
System.out.println("\nUnion of s1 and s2: " + s3);System.out.println();
}
public static Set union
(Set<Integer> s1, Set<Integer> s2)
{
Set<Integer> temp = new
HashSet<Integer>();
temp = s1;
for (Integer number: s2)
temp.add(number);
return temp;
}
8 1 2 0 5 4 9
![Page 29: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/29.jpg)
Set DifferenceA lesser-known set operation is set difference.
In this operation all the elements of one set are returned that are not found in the second set.
It is important to realize that difference can create two different results. The order is significant.
Consider the following example.
Set1 contains [10, 20, 30, 40, 50, 60]Set2 contains [40, 50, 60, 70, 80, 90]
The difference of Set1 and Set2 or Set1 - Set2 = [10, 20, 30]The difference of Set2 and Set1 or Set2 - Set1 = [70, 80, 90]
10 40 70 20 50 80 30 60 90
![Page 30: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/30.jpg)
Venn Diagram #3Boolean Algebra logical subtraction ( - ) can be demonstrated with Venn Diagrams, using difference.
A - B also A * ~B also A and not B also A not B
A B
![Page 31: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/31.jpg)
public class Java3710{
public static void main (String args[]){ System.out.println("\nJAVA3710.JAVA\n"); Random rnd = new Random(12345);
Set<Integer> s1 = new HashSet<Integer>();
for (int k = 1; k <= 5; k++)
{
Integer obj = new Integer(rnd.nextInt(10));
s1.add(obj);
}
System.out.println("s1 Elements: " + s1);
Set<Integer> s2 = new HashSet<Integer>();
for (int k = 1; k <= 5; k++)
{
Integer obj = new Integer(rnd.nextInt(10));
s2.add(obj);
}
System.out.println("s2 Elements: " + s2);
Set s3 = difference(s1,s2);
Set s4 = difference(s2,s1);
System.out.println("\nDifference of s1 and s2: " + s3);
System.out.println("Difference of s2 and s1: " + s4);
System.out.println();}
}
public static Set difference (Set<Integer> s1, Set<Integer> s2)
{
Set<Integer> temp = new
HashSet<Integer>();
for (Integer number: s1)
if (!s2.contains(number))
temp.add(number);
return temp;
}
8 1 2 0 5 4 9
![Page 32: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/32.jpg)
![Page 33: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/33.jpg)
Math Example
x-value y = x + 2 y - value x-value y = x2 - 3 y-value
1 y = 1 + 2 3 1 y = 1 - 3 -2
2 y = 2 + 2 4 2 y = 4 - 3 1
3 y = 3 + 2 5 3 y = 9 - 3 6
4 y = 4 + 2 6 4 y = 16 - 3 13
5 y = 5 + 2 7 5 y = 25 - 3 22
6 y = 6 + 2 8 6 y = 36 - 3 33
7 y = 7 + 2 9 7 y = 49 - 3 46
8 y = 8 + 2 10 8 y = 64 - 3 61
9 y = 9 + 2 11 9 y = 81 - 3 78
In the example below call the x-value the key and the y-value the value or the target. For the y = x + 2 function we can say that 1 maps to 3 & 2 maps to 4. For the y = x2 - 2 function we can say that 1 maps to -2 & 2 maps to 1.
![Page 34: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/34.jpg)
Geography ExampleOnce again there is an association between the key (country) and its value or target (capital). In this example Belgium maps to Brussels, France maps to Paris, Germany maps to Berlin, etc.
Country Capital Country Capital
Belgium Brussels France Paris
Germany Berlin Austria Vienna
Netherlands Amsterdam Luxembourg Luxembourg
Columbia Bogotá Spain Madrid
Italy Rome Poland Warsaw
![Page 35: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/35.jpg)
// Java3711.java// This program introduces the <HashMap> and <TreeMap> classes with the <put> method.import java.util.*;public class Java3711{
public static void main (String args[]){
System.out.println("\nJAVA3711.JAVA\n");Map hMap = new HashMap();Map tMap = new TreeMap();
hMap.put("D","Dog");hMap.put("B","Bear");hMap.put("A","Aardvark");hMap.put("C","Cat");
tMap.put("D","Dog");tMap.put("B","Bear");tMap.put("A","Aardvark");tMap.put("C","Cat");System.out.println(hMap);System.out.println(tMap);System.out.println();
}}
![Page 36: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/36.jpg)
Constructing a Map Object
HashMap hMap = new HashMap();TreeMap tMap = new TreeMap();
or you can use the preferred declaration below:
Map hMap = new HashMap();Map tMap = new TreeMap();
![Page 37: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/37.jpg)
Map Method put
hMap.put("C","Cat");tMap.put("C","Cat");
Method put stores the first parameter - "C" - as the key and its second parameter - "Cat" - as the value or target.
![Page 38: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/38.jpg)
// Java3712.java// This program investigates how data is sorted. // It appears that the key (1, 2, 3, 4) is used. // This program also demonstrates using generics with <Map> classes.// Notice how you need to use the double type, like <String,String>. import java.util.*;
public class Java3712{ public static void main (String args[]) { System.out.println("\nJAVA3712.JAVA\n"); Map<String,String> hMap = new HashMap<String,String>(); Map<String,String> tMap = new TreeMap<String,String>(); hMap.put("1","Dog"); hMap.put("2","Bear"); hMap.put("3","Aardvark"); hMap.put("4","Cat"); tMap.put("1","Dog"); tMap.put("2","Bear"); tMap.put("3","Aardvark"); tMap.put("4","Cat"); System.out.println(hMap); System.out.println(tMap); System.out.println(); }}
![Page 39: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/39.jpg)
// Java3713.java// This program demonstrates that keys in a <HashMap> object are not sorted.// Keys in a <TreeMap> object are sorted in ascending order.// The keys in each object map to the same target to focus on the key order.import java.util.*;public class Java3713{
public static void main (String args[]){
System.out.println("\nJAVA3713.JAVA\n");Map<Integer,String> hMap = new HashMap<Integer,String>();Map<Integer,String> tMap = new TreeMap<Integer,String>();Random rnd = new Random(12345);
System.out.println("Random Key Sequence");for (int k = 1; k <= 20; k++){ Integer intObj = new Integer(rnd.nextInt(90) + 10); System.out.print(intObj + " "); hMap.put(intObj,"HashMap"); tMap.put(intObj,"TreeMap" );}System.out.println("\n\n");
System.out.println(hMap);System.out.println("\n\n");System.out.println(tMap);System.out.println();
}}
![Page 40: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/40.jpg)
![Page 41: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/41.jpg)
// Java3714.java// This program demonstrates that the <put> method can be used to replace existing// data in a map with the same key.import java.util.*;public class Java3714{
public static void main (String args[]){
System.out.println("\nJAVA3714.JAVA\n");Map<String,String> hMap = new HashMap<String,String>();Map<String,String> tMap = new TreeMap<String,String>();
hMap.put("D","Dog");hMap.put("B","Bear");hMap.put("A","Aardvark");hMap.put("C","Cat");tMap.put("D","Dog");tMap.put("B","Bear");tMap.put("A","Aardvark");tMap.put("C","Cat");System.out.println(hMap);System.out.println(tMap);System.out.println();
hMap.put("A","Anaconda");tMap.put("A","Anaconda");System.out.println(hMap);System.out.println(tMap);System.out.println();
}}
![Page 42: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/42.jpg)
// Java3715.java// This program demonstrates that the <put> method is a return method, which// returns the current value, prior to replacing a new value.
import java.util.*;
public class Java3715{
public static void main (String args[]){
System.out.println("\nJAVA3715.JAVA\n");Map<String,String> map = new TreeMap<String,String>();map.put("A","Aardvark");map.put("B","Bear");map.put("C","Cat");map.put("D","Dog");
System.out.println(map.put("A","Andy"));System.out.println(map.put("B","Bonny"));System.out.println(map.put("C","Cliff"));System.out.println(map.put("D","Darlene"));System.out.println();System.out.println(map);
}}
![Page 43: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/43.jpg)
Map Method putThe Rest of the Story
System.out.println(map.put("A","Andy"));
put is a return method, which returns the currently mapped value before replacing the mapping with the new value in its parameter .
![Page 44: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/44.jpg)
// Java3716.java// This program demonstrates the <get> method, which returns// the object that is mapped to a specified key.import java.util.*;public class Java3716{
public static void main (String args[]){
System.out.println("\nJAVA3716.JAVA\n");Map<Integer,String> map = new TreeMap<Integer,String>();map.put(new Integer(15),"Dog");map.put(new Integer(18),"Bear");map.put(new Integer(21),"Aardvark");map.put(new Integer(35),"Cat");System.out.println(map);System.out.println();
for (int k = 1; k <= 50; k ++){
Integer key = new Integer(k);String target = map.get(key);if (target != null)
System.out.println(target);}System.out.println();
}}
![Page 45: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/45.jpg)
// Java3717.java// This program demonstrates the <containsKey> method.// This makes the previous program more practical.import java.util.*;public class Java3717{
public static void main (String args[]){
System.out.println("\nJAVA3717.JAVA\n");Map<Integer,String> map = new TreeMap<Integer,String>();map.put(new Integer(15),"Dog");map.put(new Integer(18),"Bear");map.put(new Integer(21),"Aardvark");map.put(new Integer(35),"Cat");System.out.println(map);System.out.println();
for (int k = 1; k <= 50; k ++){
Integer key = new Integer(k);if (map.containsKey(key))
System.out.println(map.get(key));}System.out.println();
}}
![Page 46: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/46.jpg)
// This program demonstrates how to use a map object as a dictionary.// It also demonstrates the use of the <keySet> method, which returns a // <Set> object of available keys in a <Map> object.public class Java3718{
public static void main (String args[]){System.out.println("\nJAVA3718.JAVA\n");String[] english = {"one","two","three","house","room","city","beach","bicycle"};String[] dutch = {"een","twee","drie","huis","kamer","stad","strand","fiets"};Translator englishDutch = new Translator(english,dutch);System.out.println(englishDutch);
}}class Translator{ private Map<String,String> map; public Translator(String[] keys, String[] values) { map = new HashMap<String,String>(); for (int k = 0; k < keys.length; k++) if (!map.containsKey(k)) map.put(keys[k],values[k]); } public String toString() { Set<String> keys = new HashSet<String>(); keys = map.keySet(); String temp = ""; for (String key: keys) temp += key + " = " + map.get(key) + "\n"; return temp; }}
![Page 47: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/47.jpg)
// This program presents a more practical dictionary. It is now possible - with a limited vocabulary - // to translate English words interactively into Dutch words. The program concludes with the entry of "end".public class Java3719{
public static void main (String args[]){
System.out.println("\nJava3719.java\n");String[] english = {"one","two","three","house","room","city","beach","bicycle"};String[] dutch = {"een","twee","drie","huis","kamer","stad","strand","fiets"};Translator dictionary = new Translator(english,dutch);Scanner input = new Scanner(System.in);String englishWord = "begin";while (!englishWord.equals("end")){
System.out.print("Enter an English word ===>> ");englishWord = input.nextLine(); System.out.println();if (englishWord.equals("end"))
System.out.println("Tot ziens");else{
String dutchWord = dictionary.getDutch(englishWord); System.out.println(englishWord + " in English equals " + dutchWord + " in Dutch"); }
System.out.println();}
}}class Translator{ private Map<String,String> map; public Translator(String[] keys, String[] values) { map = new HashMap<String,String>(); for (int k = 0; k < keys.length; k++) if (!map.containsKey(k)) map.put(keys[k],values[k]);
} public String getDutch(String word) {
if (map.containsKey(word)) return map.get(word);else return "not in dictionary";
}}
![Page 48: Chapter 37 Slides](https://reader035.vdocuments.us/reader035/viewer/2022081503/568148ad550346895db5c16e/html5/thumbnails/48.jpg)