Common methods of HashMap

Map set

There is an implementation class Hashtable, a sub-interface SortedMap and an abstract class AbstractMap under the Map interface. There is a HashMap implementation class and a TreeMap implementation class under the AbstractMap interface. Next, the common methods of HashMap implementation class are introduced.

put(): Add the element key - value to HashMap, key is not allowed to repeat, otherwise override

package map;

import java.util.HashMap;

public class Map {

	public static void main(String[] args) {
		HashMap<String,Integer> map = new HashMap<String,Integer>();//Angle brackets are generic
		map.put("A", 149);
		map.put("B", 26);
		map.put("C", 39);
	}
}

Generics can not be basic data types, they can be custom classes or wrapper classes corresponding to basic data types.
Byte Short Integer Float Double Boolean Character

get(): Gets the value corresponding to the key

package map;

import java.util.HashMap;

public class Map {

	public static void main(String[] args) {
		HashMap<String,Integer> map = new HashMap<String,Integer>();
		map.put("A", 149);
		map.put("B", 26);
		map.put("C", 39);
		int v = map.get("A");
		System.out.println(v);
	}
}

size(): Gets how many key-value pairs are in the HashMap collection container

package map;

import java.util.HashMap;

public class Map {

	public static void main(String[] args) {
		HashMap<String,Integer> map = new HashMap<String,Integer>();
		map.put("A", 149);
		map.put("B", 26);
		map.put("C", 39);
		int v = map.get("A");
		System.out.println(map.size());
	}
}

clear(): empty container key pair, isEmpty(): empty returns true

package map;

import java.util.HashMap;

public class Map {

	public static void main(String[] args) {
		HashMap<String,Integer> map = new HashMap<String,Integer>();
		map.put("A", 149);
		map.put("B", 26);
		map.put("C", 39);
		map.clear();
		System.out.println(map.isEmpty());
	}
}

remove(): Delete the HashMap element and return value

package map;

import java.util.HashMap;

public class Map {

	public static void main(String[] args) {
		HashMap<String,Integer> map = new HashMap<String,Integer>();
		map.put("A", 149);
		map.put("B", 26);
		map.put("C", 39);
		int score = map.remove("Tom");
		System.out.println(score);
	}
}
package map;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;

public class Map {

	public static void main(String[] args) {
		HashMap<String,Integer> map = new HashMap<String,Integer>();
		map.put("A", 149);
		map.put("B", 26);
		map.put("C", 39);
		
//		Collection<Integer> vals = map.values();
//		for (Integer i : vals) {
//			System.out.println(i);//set of values
//		}

//		Set<String> set = map.keySet();
//		for (String key : set) {
//			System.out.println (key+"+map.get(key);//set of keys, get values by keys
//		}
//		
//		Iterator<String> it = set.iterator();
//		while(it.hasNext()) {
//			String key = it.next();
//			System.out.println (key+"+map.get(key);//Give the information of the set of keys to the iterator variable, and then traverse through the keys to get the value.
//		}
		
//		Set < entry < String, Integer > set = map. entrySet (); // Create a set of entries, which are encapsulated objects of key-value pairs and can call the method traversal of objects.
//		for (Entry<String, Integer> entry : set) {
//			System.out.println(entry.getKey()+" "+entry.getValue());
//		}
//		
//		Iterator < Entry < String, Integer > it = set. iterator (); // Give the information of these objects to the iterator variable, and iterator traverses it
//		while(it.hasNext()) {
//			Entry<String,Integer> entry = it.next();
//			System.out.println (entry.getKey()+"+entry.getValue();//entry object

	}
}

Tags: Java

Posted on Mon, 07 Oct 2019 20:38:26 -0700 by fenrir