如果我想从 Set 中删除 ("one", 1),我应该写什么作为 Set remove() 方法的参数
what should I write as the argument to Set remove() method if I want to remove ("one", 1) from Set
我正在从Java tutorial oracle学习映射接口并且遇到了以下语句:
The Collection views support element removal in all its many forms —
remove, removeAll, retainAll, and clear operations, as well as the
Iterator.remove operation.
所以我想出了以下代码并尝试使用 Collection.remove()
方法从 Set sme1
中删除其中一个元素,即 ("one", 1)
。但是我不确定如果我想从 sme1
中删除 ("one", 1)
,我应该写什么作为 sme1.remove()
方法的参数。有人可以帮帮我吗?在此先感谢您的帮助!
Map<String, Integer> m1 = new LinkedHashMap<>();
m1.put("one", 1);
m1.put("two", 2);
m1.put("three", 3);
Set<Map.Entry<String, Integer>> sme1 = m1.entrySet();
System.out.println(sme1);
sme1.remove(?);//what should I write as the argument to sme1.remove() method if I want to remove ("one", 1) from sme1.
System.out.println(m1);
不要从 EntrySet 中删除,从地图本身中删除,如下所示:
Map<String, Integer> m1 = new LinkedHashMap<>();
m1.put("one", 1);
m1.put("two", 2);
m1.put("three", 3);
System.out.println(m1);
m1.remove("one");
System.out.println(m1);
您是在告诉地图删除具有键="one" 的元素。
与获取 Map
中所有 Map.Entry
的 Set
并调用 Map.Entry.remove()
相比,这是一个更简洁的解决方案
如 Java 教程所述,所有 Collection
都支持 remove()
操作。在这种情况下,如果您想使用 Map.Entry
的 Set
进行删除,那么您需要告诉 Set
要删除哪个 Map.Entry
。 remove()
操作对 Collection
中的每个条目调用 equals()
方法。因此,您需要将 Map.Entry
传递给它以与其他每个 Map.Entry
进行比较,以便找到要删除的正确元素。
这样做看起来像:
Map<String, Integer> m1 = new LinkedHashMap<>();
m1.put("one", 1);
m1.put("two", 2);
m1.put("three", 3);
Set<Map.Entry<String, Integer>> sme1 = m1.entrySet();
System.out.println(sme1);
sme1.remove(new AbstractMap.SimpleEntry<String, Integer>("one",1));
System.out.println(sme1);
远不如 'pretty'!
我正在从Java tutorial oracle学习映射接口并且遇到了以下语句:
The Collection views support element removal in all its many forms — remove, removeAll, retainAll, and clear operations, as well as the Iterator.remove operation.
所以我想出了以下代码并尝试使用 Collection.remove()
方法从 Set sme1
中删除其中一个元素,即 ("one", 1)
。但是我不确定如果我想从 sme1
中删除 ("one", 1)
,我应该写什么作为 sme1.remove()
方法的参数。有人可以帮帮我吗?在此先感谢您的帮助!
Map<String, Integer> m1 = new LinkedHashMap<>();
m1.put("one", 1);
m1.put("two", 2);
m1.put("three", 3);
Set<Map.Entry<String, Integer>> sme1 = m1.entrySet();
System.out.println(sme1);
sme1.remove(?);//what should I write as the argument to sme1.remove() method if I want to remove ("one", 1) from sme1.
System.out.println(m1);
不要从 EntrySet 中删除,从地图本身中删除,如下所示:
Map<String, Integer> m1 = new LinkedHashMap<>();
m1.put("one", 1);
m1.put("two", 2);
m1.put("three", 3);
System.out.println(m1);
m1.remove("one");
System.out.println(m1);
您是在告诉地图删除具有键="one" 的元素。
与获取 Map
中所有 Map.Entry
的 Set
并调用 Map.Entry.remove()
如 Java 教程所述,所有 Collection
都支持 remove()
操作。在这种情况下,如果您想使用 Map.Entry
的 Set
进行删除,那么您需要告诉 Set
要删除哪个 Map.Entry
。 remove()
操作对 Collection
中的每个条目调用 equals()
方法。因此,您需要将 Map.Entry
传递给它以与其他每个 Map.Entry
进行比较,以便找到要删除的正确元素。
这样做看起来像:
Map<String, Integer> m1 = new LinkedHashMap<>();
m1.put("one", 1);
m1.put("two", 2);
m1.put("three", 3);
Set<Map.Entry<String, Integer>> sme1 = m1.entrySet();
System.out.println(sme1);
sme1.remove(new AbstractMap.SimpleEntry<String, Integer>("one",1));
System.out.println(sme1);
远不如 'pretty'!