import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* {@link <a href=
* "https://[Log in to view URL]"
* target= "_blank">Primitive HashMap<K,V></a>}
*
* @author itammb ( Italia Massimiliano Buscati )
* @version JDK 1.15
*
*/
class Main {
public static enum Game {
CRICKET, HOCKEY, TENNIS
}
public static class UniTestHashMap {
/**
* @see HashMap#containsKey(Object)
*
*/
public void applayContainsKey(Map<Game, Short> buffer, Game contains) {
System.out.println(buffer.containsKey(contains));
}
/**
* @see Map#copyOf(Map)
*
*/
public void applayCopyOf(Map<Game, Short> source) {
// vista immutabile
Map<Game, Short> view = Map.copyOf(source);
Map<Game, Short> copyBuf = new HashMap<Game, Short>(view);
copyBuf.remove(Game.CRICKET);
applayIterator(copyBuf);
applayIterator(source);
applayIterator(view);
}
/**
* @see Set#stream()
* @see Stream#collect(java.util.stream.Collector)
* @see Collectors#toMap(java.util.function.Function,
* java.util.function.Function)
*
*/
public void applayStream(Map<Game, Short> source) {
Stream<Entry<Game, Short>> stream = source.entrySet().stream();
Map<Game, Short> copyBuf = stream.collect(Collectors.toMap(Entry::getKey, Entry::getValue));
copyBuf.remove(Game.CRICKET);
applayIterator(copyBuf);
applayIterator(source);
}
/**
* @see Stream#sorted(java.util.Comparator)
* @see Stream#collect(java.util.stream.Collector)
* @see Collectors#toList()
*
*/
public void applaySort(Map<Game, Short> buffer) {
List<Entry<Game, Short>> sort = buffer.entrySet().stream().sorted(Map.Entry.comparingByKey())
.collect(Collectors.toList());
for (int i = 0; i < sort.size(); i++)
System.out.print("[i=" + i + ", e -> " + sort.get(i) + "] ");
System.out.println();
}
/**
* @see Collections#emptyMap()
*
*/
public void applayEmptyMap() {
System.out.println(Collections.emptyMap());
}
/**
* @see Collections#unmodifiableMap(Map)
*
*/
public void applayUnmodifiableMap(Map<Game, Short> buffer) {
// view a sola lettura
Map<Game, Short> view = Collections.unmodifiableMap(buffer);
try {
view.put(Game.TENNIS, Short.valueOf("0"));
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* @see Map#entrySet()
* @see Iterator
*
*/
public void applayIterator(Map<Game, Short> buffer) {
if (buffer.isEmpty())
System.out.print("[]");
else
for (Iterator<Entry<Game, Short>> itr = buffer.entrySet().iterator(); itr.hasNext();)
System.out.print(itr.next() + " ");
println();
}
private static void println() {
System.out.println();
}
/**
* @see Map#ofEntries(Entry...)
*
*/
public static HashMap<Game, Short> createTest() {
Map<Game, Short> source = Map.ofEntries(Map.entry(Game.CRICKET, Short.valueOf("1")),
Map.entry(Game.HOCKEY, Short.valueOf("2")), Map.entry(Game.TENNIS, Short.valueOf("3")));
HashMap<Game, Short> buffer = new HashMap<Game, Short>(source);
return buffer;
}
}
public static void main(String args[]) throws Exception {
// Unit test - verifica l'esistenza di una chiave nella struttura
new UniTestHashMap().applayContainsKey(UniTestHashMap.createTest(), Game.CRICKET);
// Unit test - copia di una struttura ( da una sua view immutabile )
new UniTestHashMap().applayCopyOf(UniTestHashMap.createTest());
// Unit test - copia di una struttura ( da uno stream )
new UniTestHashMap().applayStream(UniTestHashMap.createTest());
// Unit test - ordina una struttura e restituisce una lista
new UniTestHashMap().applaySort(UniTestHashMap.createTest());
// Unit test - restituisce una struttura vuota
new UniTestHashMap().applayEmptyMap();
// Unit test - view a sola lettura di una struttura
new UniTestHashMap().applayUnmodifiableMap(UniTestHashMap.createTest());
// Unit test - attraversa una struttura con l'ausilio di un'interfaccia
// Iteretor<E>
new UniTestHashMap().applayIterator(UniTestHashMap.createTest());
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: