删除具有空值的 treeMap 条目
Deleting the treeMap entries with null values
我正在尝试删除所有空值,但如果最后一个键的 treeSet 为空值,则它会保留在那里。所以我在想如何删除最后一个条目,如果它是空的。由于这是一个 treeMap 我认为我可以通过使用 tm.lastKey() 访问它来获取最后一个元素,但该方法似乎不存在。所以这个问题是双重的。首先,有没有办法删除包括最后一个在内的所有空值,其次,.lastKey() 方法在哪里?
public class Timing {
private static Map<String, SortedSet> tm = new TreeMap<String, SortedSet>();
public static Map manipulate() {
SortedSet ss = new TreeSet();
ss.add("APPL");
ss.add("VOD");
ss.add("MSFT");
tm.put("2019-09-18",null);
tm.put("2019-09-21",ss);
tm.put("2019-09-22", null);
tm.put("2019-09-20",ss);
tm.put("2019-09-19", null);
tm.put("2019-09-23",null);
return tm;
}
public static void printMap() {
for (String s: tm.keySet()) {
System.out.println(s + ": " + tm.get(s));
}
}
// Will delete all but the last one
public static void deleteNull() {
Set set = tm.entrySet();
Iterator i = set.iterator();
Map.Entry me = (Map.Entry) i.next();
// there is no tm.lastKey()??
while(i.hasNext()) {
if (me.getValue() == null) {
i.remove();
}
me = (Map.Entry) i.next();
}
}
}
A Java TreeMap
确实指定了 lastKey()
方法。您可以在 TreeMap
的 Java-Doc 中看到它。
问题是,您无法访问该方法,因为您将映射的真实类型隐藏到您的方法中。你可以在这里看到它:
private static Map<String, SortedSet> tm = new TreeMap<String, SortedSet>();
据此,您的方法只知道 tm
是一个 Map
对象,而没有 lastKey()
方法。将 Map
更改为 TreeMap
或在您的方法中进行转换,然后它将起作用。
备选方案 1:
private static TreeMap<String, SortedSet> tm = new TreeMap<String, SortedSet>();
备选方案 2:
public String lastKey() {
if (tm instanceof TreeMap<?, ?>) {
return ((TreeMap<String, SortedSet>) tm).lastKey();
} else {
// Error!
}
}
要从地图中删除值为 null
的所有条目,您可以将 deleteNull
方法替换为
tm.values().removeIf(Objects::isNull);
绝对最简单的方法是 运行 在 while 循环结束后再次检查迭代器,如下所示:
while(i.hasNext()) {
if (me.getValue() == null) {
i.remove();
}
me = (Map.Entry) i.next();
}
if (me.getValue() == null) {
i.remove();
}
me = (Map.Entry) i.next();
这样你会得到最后一个值。
但是,您可以使用与打印地图类似的键集。
Set<String> keySet = tm.keySet();
for(int ndx = 0; ndx < keySet.size(); ndx++){
String key = keySet.get(ndx);
if(tm.get(key) == null){
tm.remove(key);
}
}
我正在尝试删除所有空值,但如果最后一个键的 treeSet 为空值,则它会保留在那里。所以我在想如何删除最后一个条目,如果它是空的。由于这是一个 treeMap 我认为我可以通过使用 tm.lastKey() 访问它来获取最后一个元素,但该方法似乎不存在。所以这个问题是双重的。首先,有没有办法删除包括最后一个在内的所有空值,其次,.lastKey() 方法在哪里?
public class Timing {
private static Map<String, SortedSet> tm = new TreeMap<String, SortedSet>();
public static Map manipulate() {
SortedSet ss = new TreeSet();
ss.add("APPL");
ss.add("VOD");
ss.add("MSFT");
tm.put("2019-09-18",null);
tm.put("2019-09-21",ss);
tm.put("2019-09-22", null);
tm.put("2019-09-20",ss);
tm.put("2019-09-19", null);
tm.put("2019-09-23",null);
return tm;
}
public static void printMap() {
for (String s: tm.keySet()) {
System.out.println(s + ": " + tm.get(s));
}
}
// Will delete all but the last one
public static void deleteNull() {
Set set = tm.entrySet();
Iterator i = set.iterator();
Map.Entry me = (Map.Entry) i.next();
// there is no tm.lastKey()??
while(i.hasNext()) {
if (me.getValue() == null) {
i.remove();
}
me = (Map.Entry) i.next();
}
}
}
A Java TreeMap
确实指定了 lastKey()
方法。您可以在 TreeMap
的 Java-Doc 中看到它。
问题是,您无法访问该方法,因为您将映射的真实类型隐藏到您的方法中。你可以在这里看到它:
private static Map<String, SortedSet> tm = new TreeMap<String, SortedSet>();
据此,您的方法只知道 tm
是一个 Map
对象,而没有 lastKey()
方法。将 Map
更改为 TreeMap
或在您的方法中进行转换,然后它将起作用。
备选方案 1:
private static TreeMap<String, SortedSet> tm = new TreeMap<String, SortedSet>();
备选方案 2:
public String lastKey() {
if (tm instanceof TreeMap<?, ?>) {
return ((TreeMap<String, SortedSet>) tm).lastKey();
} else {
// Error!
}
}
要从地图中删除值为 null
的所有条目,您可以将 deleteNull
方法替换为
tm.values().removeIf(Objects::isNull);
绝对最简单的方法是 运行 在 while 循环结束后再次检查迭代器,如下所示:
while(i.hasNext()) {
if (me.getValue() == null) {
i.remove();
}
me = (Map.Entry) i.next();
}
if (me.getValue() == null) {
i.remove();
}
me = (Map.Entry) i.next();
这样你会得到最后一个值。
但是,您可以使用与打印地图类似的键集。
Set<String> keySet = tm.keySet();
for(int ndx = 0; ndx < keySet.size(); ndx++){
String key = keySet.get(ndx);
if(tm.get(key) == null){
tm.remove(key);
}
}