[英]Why After implementing comparator in treeMap returning Values as null?
I have Treemap which implements Comparator if i print this treemap object it is printing all keys And values but when i am using for loop it is giving values as null eg 我有树图,如果我打印此树图对象,它正在打印所有键和值,则实现比较器,但是当我使用for循环时,它给出的值是null
system.out.println(tm)
for (Integer j : tm.keySet()) {
System.out.println(j+" "+tm.get(j));
}
output is {1=2, 1=3, 1=4} 输出为 {1 = 2,1 = 3,1 = 4}
1 null
1 null
1 null
full code is 完整的代码是
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeMap;
import java.util.TreeSet;
class com implements Comparator<Integer> {
int day;
@Override
public int compare(Integer o1, Integer o2) {
if (o1.intValue() >= o2.intValue()) {
return 1;
}
return -1;
}
}
class ChefStamph {
static int adj[][];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
TreeMap<Integer, TreeMap<Integer, Integer>> days;
int t = sc.nextInt();
while (t-- > 0) {
int n, m;
n = sc.nextInt();
m = sc.nextInt();
adj = new int[m + 1][m + 1];
days = new TreeMap<Integer, TreeMap<Integer, Integer>>();
int arr[] = new int[n];
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt();
}
for (int i = 0; i < m; i++) {
int day = sc.nextInt();
com ob= new com();
int a = sc.nextInt();
int b = sc.nextInt();
if (days.containsKey(day)) {
TreeMap<Integer, Integer> tm1 = days.get(day);
tm1.put(a, b);
} else {
TreeMap<Integer, Integer> tm = new TreeMap<Integer, Integer>
(new com());
tm.put(a, b);
days.put(day, tm);
}
}
for (int i : days.keySet()) {
System.out.println("for day :" + i);
TreeMap<Integer, Integer> tm = days.get(i);
/**//this print fine all keys and values**
System.out.println(tm);
for (Integer j : tm.values()) {
///**here i am geting valus as null**
System.out.println(j+" "+tm.get(j));
}
}
System.out.println(days);
}
}
}
You probably meant: 您可能的意思是:
for (Integer j : tm.keySet()) {
....
This returns keys which you can pass to get(K key) method. 这将返回可以传递给get(K key)方法的键。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.