import java.util.HashMap;
public class HashMapDemo {
public static void main(String[] args) {
//create a HashMap
HashMap map = new HashMap();
map.put(1, "One");
map.put(2, "Two");
map.put(5, "Five");
map.put(3, "Three");
System.out.println(map);
//String as key
HashMap map2 = new HashMap();
map2.put("One", 1);
map2.put("Two", 2);
map2.put("Five", 5);
map2.put("Three", 3);
System.out.println(map2);
//get value by key
System.out.println("Value for key 2:"+map.get(2));
System.out.println("Value for key Two:"+map2.get("Two"));
System.out.println("Containes key 2:"+map.containsKey(2));
//add another map
HashMap map3 = new HashMap();
map3.put(4, "Four");
map3.put(6, "Six");
map3.put(7, "Seven");
map.putAll(map3);
System.out.println(map);
//remove an element
map.remove(2);
System.out.println(map);
//replace element
map.put(3, "@Three@");
System.out.println(map);
//get the length of the map
System.out.println("Length of Map:"+map.size());
//iterate using foreach loop
System.out.println("Iterating using foreach loop:");
map.forEach((k,v)->System.out.println("Key:"+k+" Value:"+v));
//using keySet
System.out.println("Iterating using keySet:");
for (Integer key : map.keySet()) {
System.out.println("Key:"+key+" Value:"+map.get(key));
}
//using entrySet
System.out.println("Iterating using entrySet:");
for (HashMap.Entry entry : map.entrySet()) {
System.out.println("Key:"+entry.getKey()+" Value:"+entry.getValue());
}
//using entrySet with forEach
System.out.println("Iterating using entrySet with forEach:");
map.entrySet().forEach((entry)->System.out.println("Key:"+entry.getKey()+" Value:"+entry.getValue()));
//using entrySet with forEach and lambda
System.out.println("Iterating using entrySet with forEach and lambda:");
map.entrySet().forEach((entry)->{
System.out.println("Key:"+entry.getKey()+" Value:"+entry.getValue());
});
//using values
System.out.println("Iterating using values:");
for (String value : map.values()) {
System.out.println("Value:"+value);
}
//
}
}
C Bitwise Operators
C Bitwise Operators Note: This is a more advanced topic in C. If you are new to programming, don't worry if it feels tricky at first - bitwise operators are mainly used in special cases like system programming, hardware control, or performance optimizations. In C, bitwise operators let you work directly with the bits (the 1s and 0s) that make up numbers in binary form. Every integer in a computer is stored in binary , which means it is represented using bits (binary digits) that are either 0 or 1. Bitwise operators allow you to compare, combine, shift, or flip these bits. Note: Bitwise operations only work on integer types (such as int , char , or long ). List of Bitwise Operators Operator Name Description & AND Sets each bit to 1 if both bits are 1 | OR Sets each bit to 1 if at least one of the bits is 1 ^ XOR Sets each bit to 1 if only one of the bits is 1 ~ NOT Inverts all the bits << Left Shift ...
0 Comments