In: Computer Science
package SOLUTION;
import java.util.Collection;
import java.util.Set;
public interface MapInterface<K, V> {
/**
* If the given key is not already in the map, adds
the
* key-value pair to the map. Otherwise, updates the
old
* value of the existing key to the specified
value.
* @param key the key
* @param value the value to be stored in the map with
the key
* @return null if the key was not already in the map,
or
* the old value associated with the key if the key was
already in the map
*/
public V put(K key, V value);
/**
* Gets the value from the map that is associated with
the given key
* @param key the key
* @return the value associated with the key, or null
if the key is
* not in the map
*/
public V get(K key);
/**
* Removes from the key-value pair associated with the
specified key
* @param key the key
* @return the value associated with the key, or null
if the key is
* not in the map
*/
public V remove(K key);
/**
* Returns whether the map contains the key-value pair
associated with
* the specified key
* @param key the key
* @return true if the map contains a key-value pair
with the specified
* key, and false otherwise
*/
public boolean containsKey(K key);
/**
* Returns whether the map contains no elements
* @return true if the map contains no key-value pairs,
and false otherwise
*/
public boolean isEmpty();
/**
* Removes all elements from the map
*/
public void clear();
/**
* Gets the number of key-value pairs in the map
* @return the number of key-value pairs in the
map
*/
public int size();
/**
* Gets a set of all keys in the map
* @return
*/
public Set<K> keySet();
/**
* Gets a set of all values in the map
* @return
*/
public Collection<V> values();
}
package SOLUTION;
public class Tester {
public static void main(String[] args) {
ArrayListMap<String, Integer>
alm = new ArrayListMap<String, Integer>();
System.out.println(alm.put("Cindy",
1));
System.out.println(alm.put("Nina",
2));
System.out.println(alm.put("Morgan", 3));
System.out.println(alm.put("Michael", 4));
System.out.println(alm.size());
System.out.println(alm.containsKey("Morgan"));
System.out.println(alm.containsKey("Jack"));
System.out.println(alm.get("Michael"));
System.out.println(alm.values());
System.out.println(alm.keySet());
System.out.println(alm.put("Michael", 6));
System.out.println(alm.get("Michael"));
System.out.println(alm.remove("Michael"));
System.out.println(alm.remove("Morgan"));
System.out.println(alm.remove("Nina"));
System.out.println(alm.remove("Cindy"));
System.out.println(alm.size());
}
}
Summary:
Below ArrayListMap class is the implementation of the map interface. In this class we are implementing methods like put, get, remove, containKey and some other map related functions.
Its very program and I have added comments wherever required so that you can understand the logic.
-----------------------------------------------MapInterface.java-------------------------------------
import java.util.Collection;
import java.util.Set;
public interface MapInterface<K, V> {
/**
* If the given key is not already in the map, adds the
* key-value pair to the map. Otherwise, updates the old
* value of the existing key to the specified value.
* @param key the key
* @param value the value to be stored in the map with the key
* @return null if the key was not already in the map, or
* the old value associated with the key if the key was already in the map
*/
public V put(K key, V value);
/**
* Gets the value from the map that is associated with the given key
* @param key the key
* @return the value associated with the key, or null if the key is
* not in the map
*/
public V get(K key);
/**
* Removes from the key-value pair associated with the specified key
* @param key the key
* @return the value associated with the key, or null if the key is
* not in the map
*/
public V remove(K key);
/**
* Returns whether the map contains the key-value pair associated with
* the specified key
* @param key the key
* @return true if the map contains a key-value pair with the specified
* key, and false otherwise
*/
public boolean containsKey(K key);
/**
* Returns whether the map contains no elements
* @return true if the map contains no key-value pairs, and false otherwise
*/
public boolean isEmpty();
/**
* Removes all elements from the map
*/
public void clear();
/**
* Gets the number of key-value pairs in the map
* @return the number of key-value pairs in the map
*/
public int size();
/**
* Gets a set of all keys in the map
* @return
*/
public Set<K> keySet();
/**
* Gets a set of all values in the map
* @return
*/
public Collection<V> values();
}
--------------------------------------------------ArrayListMap.java-----------------------------
import java.util.*;
/**
* This class is the implementation of Map Interface.
* @param <String>
* @param <Integer>
*/
public class ArrayListMap<String,Integer> implements MapInterface<String,Integer>{
private List<ArrayListMap<String,Integer>> arrayListMaps = new ArrayList<>();
String key;
Integer value;
private static int size =0;
ArrayListMap(){
}
ArrayListMap(String key, Integer value){
this.key = key;
this.value = value;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public Integer getValue() {
return value;
}
public void setValue(Integer value) {
this.value = value;
}
@Override
public Integer put(String key, Integer value) {
// Loop through all the key value pair to check if key already exist
for (int k = 0; k<size ; k++) {
if (arrayListMaps.get(k).getKey().equals(key)) {
// if key exist update the value
Integer temp = (Integer) arrayListMaps.get(k).getValue();
arrayListMaps.get(k).setValue(value);
return temp;
}
}
// if new key then add the new key, value pair
arrayListMaps.add(new ArrayListMap(key, value));
size++;
return null;
}
@Override
public Integer get(String key) {
// Loop through all the key value pair to check if key already exist
for (int i = 0; i<size ; i++) {
if (arrayListMaps.get(i).getKey().equals(key)) {
// if key exist return the value
return (Integer) arrayListMaps.get(i).getValue();
}
}
return null;
}
@Override
public Integer remove(String key) {
// Loop through all the key value pair to check if key already exist
for (int i = 0; i<size ; i++) {
if (arrayListMaps.get(i).getKey().equals(key)) {
// if key exist remove the key,value pair from arrayListMaps list.
Integer temp = (Integer) arrayListMaps.get(i).getValue();
arrayListMaps.remove(arrayListMaps.get(i));
size--;
return temp;
}
}
return null;
}
@Override
public boolean containsKey(String key) {
for (int i = 0; i<size ; i++) {
if (arrayListMaps.get(i).getKey().equals(key)) {
return true;
}
}
return false;
}
@Override
public boolean isEmpty() {
return size==0 ? true : false;
}
@Override
public void clear() {
}
@Override
public int size() {
return this.size;
}
@Override
public Set<String> keySet() {
Set<String> stringSet = new LinkedHashSet<>();
for (int i = 0; i<size ; i++) {
stringSet.add(arrayListMaps.get(i).getKey());
}
return stringSet;
}
@Override
public Collection<Integer> values() {
Set<Integer> valueSet = new LinkedHashSet<>();
for (int i = 0; i<size ; i++) {
valueSet.add(arrayListMaps.get(i).getValue());
}
return valueSet;
}
}
------------------------------------------------------Tester.java--------------------------------------------------
public class Tester {
public static void main(String[] args) {
ArrayListMap alm = new ArrayListMap();
System.out.println(alm.put("Cindy", 1));
System.out.println(alm.put("Nina", 2));
System.out.println(alm.put("Morgan", 3));
System.out.println(alm.put("Michael", 4));
System.out.println(alm.size());
System.out.println(alm.containsKey("Morgan"));
System.out.println(alm.containsKey("Jack"));
System.out.println(alm.get("Michael"));
System.out.println(alm.values());
System.out.println(alm.keySet());
System.out.println(alm.put("Michael", 6));
System.out.println(alm.get("Michael"));
System.out.println(alm.remove("Michael"));
System.out.println(alm.remove("Morgan"));
System.out.println(alm.remove("Nina"));
System.out.println(alm.remove("Cindy"));
System.out.println(alm.size());
}
}
Output:
Connected to the target VM, address: '127.0.0.1:51272',
transport: 'socket'
null
null
null
null
4
true
false
4
[1, 2, 3, 4]
[Cindy, Nina, Morgan, Michael]
4
6
6
3
2
1
0
Disconnected from the target VM, address: '127.0.0.1:51272',
transport: 'socket'
Process finished with exit code 0
Hope you like this answer. Enjoy!!!