Prime Numbers
Permutations/Combinations
Bit Manipulation
Node deleteNode(Node head, int d) { Node n = head if (n.data == d) { return head.next; // moved head } while (n.next != null) { if (n.next.data == d) { n.next = n.next.next; return head; // head didn't change } n = n.next; } return head; }
public HashMap<Integer, Student> buildMap(Student[] students) { HashMap<Integer, Student> map = name HashMap<Integer, Student>(); for (Student s : students) map.put(s.getId(),s); return map; }
If you're only interested in the keys, you can iterate through the keySet() of the map:
Map<String, Object> map = ...; for (String key : map.keySet()) { // ... }
If you only need the values, use values():
for (Object value : map.values()) { // ... }
Finally, if you want both the key and value, use entrySet():
for (Map.Entry<String, Object> entry : map.entrySet()) { String key = entry.getKey(); Object value = entry.getValue(); // ... }
One caveat: if you want to remove items mid-iteration, you'll need to do so via an Iterator (see karim79's answer). However, changing item values is OK (see Map.Entry).
4 principles of OOP