Java 使用键从HashMap中删除一个条目,同时对其进行遍历

Java 使用键从HashMap中删除一个条目,同时对其进行遍历

在Java中给定一个HashMap和一个键,任务是使用该键从这个HashMap中删除一个条目,同时对其进行迭代。

例子
输入: HashMap: {1=Geeks, 2=ForGeeks, 3=GeeksForGeeks}, key = 2
输出: {1=Geeks, 3=GeeksForGeeks}

输入: HashMap: {1=G, 2=e, 3=e, 4=k, 5=s}, key = 3
输出: {1=G, 2=e, 4=k, 5=s}

建议:请先在{IDE}上尝试你的方法,然后再继续解决

  • 使用Java 7及以前的版本。
    1. 获取HashMap和密钥
    2. 使用HashMap.iterate()方法创建一个迭代器来迭代HashMap。
    3. 使用Iterator.hasNext()方法遍历HashMap。
    4. 迭代时,检查该迭代的键是否等于指定的键。Map的入口键可以在 entry.getKey() 方法的帮助下获得。
    5. 如果键值匹配,使用remove()方法将该迭代的条目从HashMap中删除。
    6. 所需的条目已被成功删除。

示例:

// Java program to remove an entry using key
// from a HashMap while iterating over it
  
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Create a HashMap
        HashMap<Integer, String>
            map = new HashMap<>();
  
        // Populate the HashMap
        map.put(1, "Geeks");
        map.put(2, "ForGeeks");
        map.put(3, "GeeksForGeeks");
  
        // Get the key to be removed
        int keyToBeRemoved = 2;
  
        // Print the initial HashMap
        System.out.println("Original HashMap: "
                           + map);
  
        // Get the iterator over the HashMap
        Iterator<Map.Entry<Integer, String> >
            iterator = map.entrySet().iterator();
  
        // Iterate over the HashMap
        while (iterator.hasNext()) {
  
            // Get the entry at this iteration
            Map.Entry<Integer, String>
                entry
                = iterator.next();
  
            // Check if this key is the required key
            if (keyToBeRemoved == entry.getKey()) {
  
                // Remove this entry from HashMap
                iterator.remove();
            }
        }
  
        // Print the modified HashMap
        System.out.println("Modified HashMap: "
                           + map);
    }
}
Java

输出:

Original HashMap: {1=Geeks, 2=ForGeeks, 3=GeeksForGeeks}
Modified HashMap: {1=Geeks, 3=GeeksForGeeks}
Java
  • 使用Java 8 lambda表达式。
    1. 获取HashMap和Key
    2. 使用HashMap.entrySet()方法获得该地图的条目集。
    3. 使用lambda表达式,如果key等于指定的key,则从地图中删除该条目。地图的入口键可以在 entry.getKey() 方法的帮助下获得。
    4. 所需的条目已被成功删除。

示例:

// Java program to remove an entry using key
// from a HashMap while iterating over it
  
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Create a HashMap
        HashMap<Integer, String>
            map = new HashMap<>();
  
        // Populate the HashMap
        map.put(1, "Geeks");
        map.put(2, "ForGeeks");
        map.put(3, "GeeksForGeeks");
  
        // Get the key to be removed
        int keyToBeRemoved = 2;
  
        // Print the initial HashMap
        System.out.println("Original HashMap: "
                           + map);
  
        // Remove the specified entry from the Map
        map.entrySet()
            .removeIf(
                entry -> (keyToBeRemoved == entry.getKey()));
  
        // Print the modified HashMap
        System.out.println("Modified HashMap: "
                           + map);
    }
}
Java

输出:

Original HashMap: {1=Geeks, 2=ForGeeks, 3=GeeksForGeeks}
Modified HashMap: {1=Geeks, 3=GeeksForGeeks}
Java

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册