Java中的Hashmap方法与示例|设置 2 (keySet(), values(), containsKey()..)
Java中的HashMap 类方法与示例|设置 1(put()、get()、isEmpty() 和 size())
在这篇文章中,将讨论更多方法。
- keySet(): Java.util.HashMap.keySet()它返回此映射中包含的键的 Set 视图。集合由地图支持,因此对地图的更改会反映在集合中,反之亦然。
Syntax: public Set keySet() Return: a set view of the keys contained in this map
- values(): Java.util.HashMap.values()它返回此映射中包含的值的集合视图。集合由地图支持,因此对地图的更改会反映在集合中,反之亦然。
Syntax: public Collection values() Return: a collection view of the values contained in this map
- containsKey(): Java.util.HashMap.containsKey()如果此映射将一个或多个键映射到指定值,则返回true。
Syntax: public boolean containsValue(Object value) Parameters: value - value whose presence in this map is to be tested Return: true if this map maps one or more keys to the specified value
执行:
// Java program illustrating usage of HashMap class methods // keySet(), values(), containsKey() import java.util.*; public class NewClass { public static void main(String args[]) { // 1 Creation of HashMap HashMap
Geeks = new HashMap<>(); // 2 Adding values to HashMap as ("keys", "values") Geeks.put("Language", "Java"); Geeks.put("Platform", "Geeks For geeks"); Geeks.put("Code", "HashMap"); Geeks.put("Learn", "More"); // 3 containsKey() method is to check the presence // of a particluar key // Since 'Code' key present here, the condition is true if (Geeks.containsKey("Code")) System.out.println("Testing .containsKey : " + Geeks.get("Code")); // 4 keySet() method returns all the keys in HashMap Set Geekskeys = Geeks.keySet(); System.out.println("Initial keys : " + Geekskeys); // 5 values() method return all the values in HashMap Collection Geeksvalues = Geeks.values(); System.out.println("Initial values : " + Geeksvalues); // Adding new set of key-value Geeks.put("Search", "JavaArticle"); // Again using .keySet() and .values() methods System.out.println("New Keys : " + Geekskeys); System.out.println("New Values: " + Geeksvalues); } } 输出:
Testing .containsKey : HashMap Initial keys : [Language, Platform, Learn, Code] Initial values : [Java, Geeks For geeks, More, HashMap] New Keys : [Language, Platform, Search, Learn, Code] New Values: [Java, Geeks For geeks, JavaArticle, More, HashMap]
- .entrySet() : Java.util.HashMap.entrySet()方法返回 HashMap 中存在的一组完整的键和值。
Syntax: public Set
entrySet() Return: complete set of keys and values - .getOrDefault : Java.util.HashMap.getOrDefault()方法返回一个默认值,如果使用我们在 HashMap 中作为参数传递的键没有找到值。如果键的值已经存在于 HashMap 中,则它不会对其执行任何操作。
这是将值分配给尚未映射的键的非常好的方法,而不会干扰已经存在的键和值集。Syntax: default V getOrDefault(Object key,V defaultValue) Parameters: key - the key whose mapped value we need to return defaultValue - the default for the keys present in HashMap Return: mapping the unmapped keys with the default value.
- .replace() : Java.util.HashMap.replace(key, value)或Java.util.HashMap.replace(key, oldvalue, newvalue)方法是Java.util.HashMap 类方法。
第一种方法接受一组键和值,它将用参数中传递的新值替换键的现有值。如果不存在这样的集合,replace() 方法将什么也不做。
同时,如果在 HashMap 中找到 key 和 old_Value,则第二种方法只会替换已经存在的 key-old_value 集。Syntax: replace(k key, v value) or replace(k key, v oldvalue, newvalue) Parameters: key - key in set with the old value. value - new value we want to be with the specified key oldvalue - old value in set with the specified key newvalue - new value we want to be with the specified key Return: True - if the value is replaced Null - if there is no such set present
- .putIfAbsent Java.util.HashMap.putIfAbsent(key, value)方法用于将新的键值集插入到 HashMap(如果存在相应的集)。如果 HashMap 中已经存在这样的键值集,则返回空值。
Syntax: public V putIfAbsent(key, value) Parameters: key - key with which the specified value is associates. value - value to associates with the specified key.
// Java Program illustrating HashMap class methods(). // entrySet(), getOrDefault(), replace(), putIfAbsent import java.util.*; public class NewClass { public static void main(String args[]) { // Creation of HashMap HashMap
Geeks = new HashMap<>(); // Adding values to HashMap as ("keys", "values") Geeks.put("Language", "Java"); Geeks.put("Code", "HashMap"); Geeks.put("Learn", "More"); // .entrySet() returns all the keys with their values present in Hashmap Set > mappingGeeks = Geeks.entrySet(); System.out.println("Set of Keys and Values using entrySet() : "+mappingGeeks); System.out.println(); // Using .getOrDefault to access value // Here it is Showing Default value as key - "Code" was already present System.out.println("Using .getorDefault : " + Geeks.getOrDefault("Code","javaArticle")); // Here it is Showing set value as key - "Search" was not present System.out.println("Using .getorDefault : " + Geeks.getOrDefault("Search","javaArticle")); System.out.println(); // .replace() method replacing value of key "Learn" Geeks.replace("Learn", "Methods"); System.out.println("working of .replace() : "+mappingGeeks); System.out.println(); /* .putIfAbsent() method is placing a new key-value as they were not present initially*/ Geeks.putIfAbsent("cool", "HashMap methods"); System.out.println("working of .putIfAbsent() : "+mappingGeeks); /* .putIfAbsent() method is not doing anything as key-value were already present */ Geeks.putIfAbsent("Code", "With_JAVA"); System.out.println("working of .putIfAbsent() : "+mappingGeeks); } } 输出:
Set of Keys and Values using entrySet() : [Language=Java, Learn=More, Code=HashMap] Using .getorDefault : HashMap Using .getorDefault : javaArticle working of .replace() : [Language=Java, Learn=Methods, Code=HashMap] working of .putIfAbsent() : [Language=Java, cool=HashMap methods, Learn=Methods, Code=HashMap] working of .putIfAbsent() : [Language=Java, cool=HashMap methods, Learn=Methods, Code=HashMap]
- remove(Object key):如果存在,则从此映射中删除此键的映射。
// Java Program illustrating remove() method using Iterator.
import java.util.*;
public class NewClass
{
public static void main(String args[])
{
// Creation of HashMap
HashMap Geeks = new HashMap<>();
// Adding values to HashMap as ("keys", "values")
Geeks.put("Language", "Java");
Geeks.put("Platform", "Geeks For geeks");
Geeks.put("Code", "HashMap");
// .entrySet() returns all the keys with their values present in Hashmap
Set> mappingGeeks = Geeks.entrySet();
System.out.println("Set of Keys and Values : "+mappingGeeks);
System.out.println();
// Creating an iterator
System.out.println("Use of Iterator to remove the sets.");
Iterator> geeks_iterator = Geeks.entrySet().iterator();
while(geeks_iterator.hasNext())
{
Map.Entry entry = geeks_iterator.next();
// Removing a set one by one using iterator
geeks_iterator.remove(); // right way to remove entries from Map,
// avoids ConcurrentModificationException
System.out.println("Set of Keys and Values : "+mappingGeeks);
}
}
}
输出:
Set of Keys and Values : [Language=Java, Platform=Geeks For geeks, Code=HashMap]
Use of Iterator to remove the sets.
Set of Keys and Values : [Platform=Geeks For geeks, Code=HashMap]
Set of Keys and Values : [Code=HashMap]
Set of Keys and Values : []
优势:
如果我们使用 for 循环,它会在内部转换为 Iterator,但如果不显式使用 Iterator,我们将无法在 Iteration 期间删除任何条目。这样做时,Iterator 可能会抛出 ConcurrentModificationException。所以,我们使用显式迭代器和while循环来遍历。
参考:
https://docs.oracle.com/javase/7/docs/api/java Java