모든 null을 제거하려고 시도하지만 마지막 키의 treeSet이 null이면 해당 키는 그대로 유지됩니다. 그래서 그것은 null 인 경우 마지막 항목을 삭제하는 방법을 생각하고있었습니다. 이것은 treeMap이므로 tm.lastKey()를 사용하여 마지막 요소에 액세스 할 수 있다고 생각했지만 그 메서드는 존재하지 않는 것 같습니다. 그래서이 질문은 두 가지입니다. 첫째, 마지막 하나를 포함하여 모든 null을 삭제하는 방법이 있습니까? 두 번째는 .lastKey() 메서드가 어디에 있습니까?null 값을 사용하여 treeMap 항목 삭제
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();
}
}
}
원시 형식을 사용하지 마십시오. – shmosel
'tm'은'Map <>'입니다. 당연히 'TreeMap'의 메소드는 그 위에 표시되지 않습니다. – Kayaman
오 예 ... 올바른 –