Example 1: java hashmap methods
import java.util.HashMap;
HashMap<String, String> capitalCities = new HashMap<String, String>();
capitalCities.put("California", "Sacramento");
1. void clear()
- Removes all of the mappings from this map.
2. Object clone()
- Returns a shallow copy of this HashMap instance: the keys and values themselves are not cloned.
3. boolean containsKey(Object key)
- Returns true if this map contains a mapping for the specified key.
4. boolean containsValue(Object value)
- Returns true if this map maps one or more keys to the specified value.
5. V get(Object key)
- Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
6. V getOrDefault(Object key, V defaultValue)
- Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.
7. boolean isEmpty()
- Returns true if this map contains no key-value mappings.
8. Set<K> keySet()
- Returns a Set view of the keys contained in this map.
9. V put(K key, V value)
- Associates the specified value with the specified key in this map.
10. V remove(Object key)
- Removes the mapping for the specified key from this map if present.
11. boolean remove(Object key, Object value)
- Removes the entry for the specified key only if it is currently mapped to the specified value.
12. V replace(K key, V value)
- Replaces the entry for the specified key only if it is currently mapped to some value.
13. boolean replace(K key, V oldValue, V newValue)
- Replaces the entry for the specified key only if currently mapped to the specified value.
14. int size()
- Returns the number of key-value mappings in this map.
15. Collection<V> values()
- Returns a Collection view of the values contained in this map.
Example 2: java hashmap syntax
import java.util.HashMap;
HashMap<String, String> capitalCities = new HashMap<String, String>();
capitalCities.put("England", "London");
capitalCities.put("Germany", "Berlin");
capitalCities.put("Norway", "Oslo");
capitalCities.put("USA", "Washington DC");
System.out.println(capitalCities);
Map<String, Integer> stGrade = new HashMap<String, Integer>();
stGrade.put("aaron", new Integer(90));
stGrade.put("isaac", new Integer(100));
stGrade.put("john", new Integer(35));
stGrade.put("mohammad", new Integer(100));
stGrade.get("mohammad");
stGrade.get("aaron");
stGrade.get("john");
stGrade.get("isaac");
Example 3: java hashmap example
Map< String,Integer> hm =
new HashMap< String,Integer>();
hm.put("a", new Integer(100));
hm.put("b", new Integer(200));
hm.put("c", new Integer(300));
hm.put("d", new Integer(400));
Example 4: how to create a hashmap in java
HashMap<Integer,String> map=new HashMap<Integer,String>();
Example 5: hashset in java
import java.util.HashMap;
public class HashMapExample
{
public static void main(String[] args)
{
HashMap<Integer, String> hm = new HashMap<Integer, String>();
hm.put(10,"Apple");
hm.put(20,"Banana");
hm.put(30,"Cherry");
hm.put(40,"Dragonfruit");
System.out.println("HashMap elements: " + hm);
}
}