qudongfang
0
Q:

java hashmap methods

// Import the HashMap class
import java.util.HashMap;

    // Create a HashMap object called capitalCities
    HashMap<String, String> capitalCities = new HashMap<String, String>();

    // Add keys and values (Country, City)
    capitalCities.put("England", "London");
    capitalCities.put("Germany", "Berlin");
    capitalCities.put("Norway", "Oslo");
    capitalCities.put("USA", "Washington DC");
    System.out.println(capitalCities);
17
import java.util.HashMap; // import the HashMap class

// instantiate a HashMap instance
HashMap<String, String> capitalCities = new HashMap<String, String>();

// SCROLL DOWN FOR LIST OF ALL METHODS --------------------------------------
// to use any methods below add the function name to the instance name
// General Form: HashMapInstanceName.{functionName}({parameter list});
// Specific Example Using the Put Method: 
capitalCities.put("California", "Sacramento");

// SCROLL DOWN FOR LIST OF ALL METHODS --------------------------------------

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.
3
// Java code to illustrate the put() method 
import java.util.*; 
  
public class Hash_Map_Demo { 
    public static void main(String[] args) 
    { 
  
        // Creating an empty HashMap 
        HashMap<Integer, String> hash_map = new HashMap<Integer, String>(); 
  
        // Mapping string values to int keys 
        hash_map.put(10, "Geeks"); 
        hash_map.put(15, "4"); 
        hash_map.put(20, "Geeks"); 
        hash_map.put(25, "Welcomes"); 
        hash_map.put(30, "You"); 
  
        // Displaying the HashMap 
        System.out.println("Initial Mappings are: " + hash_map); 
  
        // Inserting existing key along with new value 
        String returned_value = (String)hash_map.put(20, "All"); 
  
        // Verifying the returned value 
        System.out.println("Returned value is: " + returned_value); 
  
        // Displayin the new map 
        System.out.println("New map is: " + hash_map); 
    } 
} 
4
HashMap<Integer,String> map=new HashMap<Integer,String>();//key is integer, value is String
2
import java.util.HashMap; // import the HashMap class

HashMap<String, String> capitalCities = new HashMap<String, String>();
3
import java.util.Map; // import Map Interface
class MyClass {
    public static void main( String args[] ) {
    HashMap<Integer, String> shapes = new HashMap<Integer,String>(); // Create an ArrayList object with string data type
    }
}
  
-1

New to Communities?

Join the community