如何使用 LinkedHashMap 从 java HashMap 中删除最后一个输入?
How do I remove the last input from java HashMap using LinkedHashMap?
我需要删除 HashMap 中的最后一个输入,然后添加一个新输入 instead.I 听说您可以使用 LinkedHashMap 做到这一点,但具体怎么做呢?说明没有提到我应该使用 LinkedHashMap 但显然,没有它就不可能从 HashMap 中删除最后一项。
或者如果您有任何替代解决方案可以删除最后一项以便我可以添加其他输入,请告诉我应该添加到代码中的内容。
这是我正在尝试做的事情:
package studentlist;
import java.util.LinkedHashMap;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class StudentList {
public static void main(String[] args) {
Map<String, String> students = new HashMap<>();
Scanner s = new Scanner(System.in);
for(int i=1; i<= 3; i++){
System.out.print("Enter student number " + i + ": ");
String es = s.nextLine();
System.out.print("Enter student first name " + i + ": ");
String en = s.nextLine();
students.put(es, en);
}
for (Map.Entry mp : students.entrySet()) {
System.out.println(mp.getKey() + " " + mp.getValue());
}
//The 3rd input should be removed before this:
System.out.print("Enter your student number: ");
String sn = s.nextLine();
System.out.print("Enter your first name: ");
String fn = s.nextLine();
students.put(sn, fn);
for (Map.Entry mp : students.entrySet()) {
System.out.println(mp.getKey() + " " + mp.getValue());
}
}
}
一些想法:
- 保留一个变量,用于存储最后输入的学生
- 保留 2 个结构(一个列表和一个地图)
- 将 LinkedHashMap 迭代到末尾以找到最后一个条目。
Map
的 javadoc 说:
The Map interface provides three collection views, which allow a map's contents to be viewed as a set of keys, collection of values, or set of key-value mappings. The order of a map is defined as the order in which the iterators on the map's collection views return their elements. Some map implementations, like the TreeMap
class, make specific guarantees as to their order; others, like the HashMap
class, do not.
LinkedHashMap
的 javadoc 说:
[LinkedHashMap] maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order). Note that insertion order is not affected if a key is re-inserted into the map. (A key k
is reinserted into a map m
if m.put(k, v)
is invoked when m.containsKey(k)
would return true
immediately prior to the invocation.)
因此,要删除最后一个 inserted(不是 re-inserted)条目,请使用 3 个迭代器中的任何一个,跳到结束,并删除条目:
static <K, V> Entry<K, V> removeLast(LinkedHashMap<K, V> map) {
Iterator<Entry<K, V>> entryIter = map.entrySet().iterator();
if (! entryIter.hasNext())
return null;
Entry<K, V> entry;
do {
entry = entryIter.next();
} while (entryIter.hasNext());
entryIter.remove();
return entry;
}
测试
LinkedHashMap<String, String> map = new LinkedHashMap<>();
map.put("A", "1");
map.put("E", "5");
map.put("B", "2");
map.put("D", "4");
map.put("E", "6"); // Re-insert
map.put("C", "3");
for (int i = 0; i < 7; i++)
System.out.println(removeLast(map));
输出
C=3
D=4
B=2
E=6
A=1
null
null
没有人说它会很快,但这就是如何做到的,只需 Map
。如果您需要删除多个条目而不在中间插入新条目,则特别有用。
根据需要,最好记住旁边的最后一个键。有了它,您可以快速删除最后插入的密钥,但您不能,例如通过执行两次“删除最后一个”来删除倒数第二个键。
我需要删除 HashMap 中的最后一个输入,然后添加一个新输入 instead.I 听说您可以使用 LinkedHashMap 做到这一点,但具体怎么做呢?说明没有提到我应该使用 LinkedHashMap 但显然,没有它就不可能从 HashMap 中删除最后一项。
或者如果您有任何替代解决方案可以删除最后一项以便我可以添加其他输入,请告诉我应该添加到代码中的内容。
这是我正在尝试做的事情:
package studentlist;
import java.util.LinkedHashMap;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class StudentList {
public static void main(String[] args) {
Map<String, String> students = new HashMap<>();
Scanner s = new Scanner(System.in);
for(int i=1; i<= 3; i++){
System.out.print("Enter student number " + i + ": ");
String es = s.nextLine();
System.out.print("Enter student first name " + i + ": ");
String en = s.nextLine();
students.put(es, en);
}
for (Map.Entry mp : students.entrySet()) {
System.out.println(mp.getKey() + " " + mp.getValue());
}
//The 3rd input should be removed before this:
System.out.print("Enter your student number: ");
String sn = s.nextLine();
System.out.print("Enter your first name: ");
String fn = s.nextLine();
students.put(sn, fn);
for (Map.Entry mp : students.entrySet()) {
System.out.println(mp.getKey() + " " + mp.getValue());
}
}
}
一些想法:
- 保留一个变量,用于存储最后输入的学生
- 保留 2 个结构(一个列表和一个地图)
- 将 LinkedHashMap 迭代到末尾以找到最后一个条目。
Map
的 javadoc 说:
The Map interface provides three collection views, which allow a map's contents to be viewed as a set of keys, collection of values, or set of key-value mappings. The order of a map is defined as the order in which the iterators on the map's collection views return their elements. Some map implementations, like the
TreeMap
class, make specific guarantees as to their order; others, like theHashMap
class, do not.
LinkedHashMap
的 javadoc 说:
[LinkedHashMap] maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order). Note that insertion order is not affected if a key is re-inserted into the map. (A key
k
is reinserted into a mapm
ifm.put(k, v)
is invoked whenm.containsKey(k)
would returntrue
immediately prior to the invocation.)
因此,要删除最后一个 inserted(不是 re-inserted)条目,请使用 3 个迭代器中的任何一个,跳到结束,并删除条目:
static <K, V> Entry<K, V> removeLast(LinkedHashMap<K, V> map) {
Iterator<Entry<K, V>> entryIter = map.entrySet().iterator();
if (! entryIter.hasNext())
return null;
Entry<K, V> entry;
do {
entry = entryIter.next();
} while (entryIter.hasNext());
entryIter.remove();
return entry;
}
测试
LinkedHashMap<String, String> map = new LinkedHashMap<>();
map.put("A", "1");
map.put("E", "5");
map.put("B", "2");
map.put("D", "4");
map.put("E", "6"); // Re-insert
map.put("C", "3");
for (int i = 0; i < 7; i++)
System.out.println(removeLast(map));
输出
C=3
D=4
B=2
E=6
A=1
null
null
没有人说它会很快,但这就是如何做到的,只需 Map
。如果您需要删除多个条目而不在中间插入新条目,则特别有用。
根据需要,最好记住旁边的最后一个键。有了它,您可以快速删除最后插入的密钥,但您不能,例如通过执行两次“删除最后一个”来删除倒数第二个键。