1 of 22

To do: November 17

  • Hackerrank problem

2 of 22

Go over homework

Hackerrank problems

  • Java Comparator: https://www.hackerrank.com/challenges/java-comparator/problem

3 of 22

The following are important

interfaces included in the

Java language ::

Collection

Set

Map

Java Interfaces

4 of 22

© A+ Computer Science - www.apluscompsci.com

The Map interface does not extend any

other interface.

Map

HashMap

SortedMap

TreeMap

Java Map Interface

5 of 22

© A+ Computer Science - www.apluscompsci.com

A Map stores pairs of keys and values.

Each key – value pair is unique.

A translation program could be written using a map.

Maps cannot store duplicates.

Java Map

6 of 22

© A+ Computer Science - www.apluscompsci.com

Key

Value

"restroom"

"bano"

"cat"

"gato"

"boy"

"muchacho"

"house"

"casa"

"toad"

"sapo"

"water"

"agua"

Java Map

7 of 22

© A+ Computer Science - www.apluscompsci.com

Because Map is an interface, you cannot instantiate it.

Map bad = new Map(); //illegal

Map hash = new HashMap(); //legal

Map tree = new TreeMap(); //legal

hash and tree store Object references.

Java Map

8 of 22

© A+ Computer Science - www.apluscompsci.com

You can specify which type of references you want to store in the TreeMap or HashMap.

Map<String, Integer> hash;

hash = new HashMap<String, Integer>();�

Map<String, Set> tree;

tree = new TreeMap<String, TreeSet<String>>();

Java Map

9 of 22

© A+ Computer Science - www.apluscompsci.com

HashMap – a map ordered by each

item’s hashCode that is extremely time efficient.

TreeMap – a naturally ordered map �that is very efficient, but not as �efficient as HashMap.

Java Map

10 of 22

© A+ Computer Science - www.apluscompsci.com

Map�Methods

11 of 22

© A+ Computer Science - www.apluscompsci.com

Map<Integer,String> map;\ = new TreeMap<Integer,String>();

map.put(1,"aplus");

map.put(2,"comp");

map.put(3,"sci");

map.put(4,"is");

map.put(5,"the");

map.put(6,"best");

System.out.println(map.get(1));

System.out.println(map.get(5));

System.out.println(map.get(7));

OUTPUT�

Java TreeMap

12 of 22

© A+ Computer Science - www.apluscompsci.com

Map

frequently used methods

Name

Use

V put(K key, V value)

adds the <K, V> key-value pair to the map. If K was already present, then V that was replaced is returned. Else, null is returned.

V get(Object k)

returns the value to which the key k is mapped or null if no mapping for the key

void clear()

removes all items from the map

int size()

returns the # of key-value mappings in the map

Set<K> keySet()

returns a set of all keys in the map

boolean containsKey(Object key)

checks if map contains a mapping for the specified key

V remove(Object key)

Removes the mapping for a key from this map if present. If not, returns null.

13 of 22

© A+ Computer Science - www.apluscompsci.com

Map<Integer,String> map;

map = new TreeMap<Integer,String>();

map.put(1,"aplus");

map.put(2,"comp");

map.put(3,"sci");

map.put(4,"is");

map.put(5,"the");

map.put(6,"best");

System.out.println(map.get(1));

System.out.println(map.get(5));

System.out.println(map.get(7));

OUTPUT�aplus�the�null

Java TreeMap

14 of 22

© A+ Computer Science - www.apluscompsci.com

Map<Integer,Double> map = new HashMap<Integer, Double>();

map.put(5,2.5);

map.put(8,6.7);

map.put(11,5.9);

map.put(6,4.2);

System.out.println(map.put(17,1.5));

System.out.println(map.put(8,9.5));

System.out.println(map.put(6,6.6));

System.out.println(map.get( 6 ));

System.out.println(map.get( 11));

System.out.println(map.get( 7 ));

OUTPUT�

Java HashMap

15 of 22

© A+ Computer Science - www.apluscompsci.com

Map<Integer,Double> map = new HashMap<Integer, Double>();

map.put(5,2.5);

map.put(8,6.7);

map.put(11,5.9);

map.put(6,4.2);

System.out.println(map.put(17,1.5));

System.out.println(map.put(8,9.5));

System.out.println(map.put(6,6.6));

System.out.println(map.get( 6 ));

System.out.println(map.get( 11));

System.out.println(map.get( 7 ));

OUTPUT�null

6.7

4.2

6.6

5.9

null

Java HashMap

16 of 22

Repl.it 04.06.introMaps

17 of 22

© A+ Computer Science - www.apluscompsci.com

Map<Character,Integer> map = new TreeMap<Character,Integer>();

String s = "bellairecardinalsarethebest";

for(int i=0; i<s.length(); i++)

{

char c = s.charAt(i);

if(map.get(c)==null)

map.put(c,0);

map.put(c,map.get(c)+1);

}

System.out.println(map.get('b'));

System.out.println(map.get('l'));

System.out.println(map.get('a'));

System.out.println(map.get('z'));

OUTPUT�

c is not in the map.

increment count

Map put() method

18 of 22

© A+ Computer Science - www.apluscompsci.com

Map<Character,Integer> map;

map = new TreeMap<Character,Integer>();

String s = "bellairecardinalsarethebest";

for(int i=0; i<s.length(); i++)

{

char c = s.charAt(i);

if(map.get(c)==null)

map.put(c,0);

map.put(c,map.get(c)+1);

}

System.out.println(map.get('b'));

System.out.println(map.get('l'));

System.out.println(map.get('a'));

System.out.println(map.get('z'));

OUTPUT�2

3

4

null

c is not in the map.

increment count

Map put() method

19 of 22

© A+ Computer Science - www.apluscompsci.com

Map<Character,Integer> map;

map = new TreeMap<Character,Integer>();

String s = "cabcdefghihabcdc";

for(char c : s.toCharArray())

{

if(map.containsKey(c))

{

map.put(c,map.get(c)+1);

}

else

{

map.put(c,1);

}

}

System.out.println(map.get('a'));

System.out.println(map.get('x'));

System.out.println(map.get('c'));

OUTPUT�

c is not in the map.

c is in the map.

Map put() method

20 of 22

© A+ Computer Science - www.apluscompsci.com

Map<Character,Integer> map;

map = new TreeMap<Character,Integer>();

String s = "cabcdefghihabcdc";

for(char c : s.toCharArray())

{

if(map.containsKey(c))

{

map.put(c,map.get(c)+1);

}

else

{

map.put(c,1);

}

}

System.out.println(map.get('a'));

System.out.println(map.get('x'));

System.out.println(map.get('c'));

OUTPUT�2�null�4

c is not in the map.

c is in the map.

Map put() method

21 of 22

Repl.it 04.07.putMap

22 of 22

Homework

  • Complete Repl.it 04-06.introMaps, 04-07.putMap
  • Write your implementation using a HashSet in your IN and submit to Hub
    • Java HashSet: https://www.hackerrank.com/challenges/java-hashset/problem