To do: November 17
Go over homework
Hackerrank problems
The following are important
interfaces included in the
Java language ::
Collection
Set
Map
Java Interfaces
© A+ Computer Science - www.apluscompsci.com
The Map interface does not extend any
other interface.
Map
HashMap
SortedMap
TreeMap
Java Map Interface
© 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
© A+ Computer Science - www.apluscompsci.com
Key | Value |
"restroom" | "bano" |
"cat" | "gato" |
"boy" | "muchacho" |
"house" | "casa" |
"toad" | "sapo" |
"water" | "agua" |
Java Map
© 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
© 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
© 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
© A+ Computer Science - www.apluscompsci.com
Map�Methods
© 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
© 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. |
© 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
© 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
© 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
Repl.it 04.06.introMaps
© 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
© 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
© 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
© 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
Repl.it 04.07.putMap
Homework