HashSet, HashMap usage
in Algorithms
HashSet
Initialization
Set<Integer> s = new HashSet<>();
Adding elements
s.add(1); s.add(3);
Remove elements
s.remove(1);
Check if the element exists
s.contains(1);
Iteration
for(int i : s){ System.out.println(i); }
Empty
s.isEmpty();
Clear
s.clear();
HashMap
Initialization
Map<Integer,Integer> m = new HashMap<>();
Adding elements
m.put(1,1); m.put(3,2);
Delete Key
m.remove(1);
Check if the Key exists
m.containsKey(1);
Iteration
for (Map.Entry<Integer, Integer> entry : m.entrySet()) { System.out.print("(" + entry.getKey() + "," + entry.getValue() + ") ");}
clear the hash map
m.clear();
Check if the hash map is empty
if (m.isEmpty()) { System.out.println("hash map is empty now!"); }