如何使用比较器在 Treemap 中获取计数
How to get count in Treemap using comparator
我用 String breed 和 int ageinmonths 创建了一只 Dog class。
String breed;
int ageInMonths;
public String getBreed() {
return breed;
}
public void setBreed(String breed) {
this.breed = breed;
}
public int getAgeInMonths() {
return ageInMonths;
}
public void setAgeInMonths(int ageInMonths) {
this.ageInMonths = ageInMonths;
}
public Dog(String breed, int ageInMonths) {
this.breed = breed;
this.ageInMonths = ageInMonths;
}
public String toString() {
return this.getBreed()+" - Age : "+
this.getAgeInMonths()+" - Count : ";
}
在主要方法中我使用了比较器。
public static void main(String[] args) {
Dog dog1 = new Dog("Companion", 10);
Dog dog2 = new Dog("Herding", 4);
Dog dog3 = new Dog("Terrier", 15);
Dog dog4 = new Dog("Companion", 7);
Dog dog5 = new Dog("Terrier", 15);
Dog dog6 = new Dog("Terrier", 9);
Dog dog7 = new Dog("Herding", 10);
Dog dog8 = new Dog("Herding", 10);
TreeMap<Integer, Dog> dogDetails = new TreeMap<Integer, Dog>();
dogDetails.put(1, dog1);
dogDetails.put(2, dog2);
dogDetails.put(3, dog3);
dogDetails.put(4, dog4);
dogDetails.put(5, dog5);
dogDetails.put(6, dog6);
dogDetails.put(7, dog7);
dogDetails.put(8, dog8);
System.out.println("The dog details are given below : "+dogDetails);
}
@Override
public int compare(Dog dog1, Dog dog2) {
return dog1.breed.compareTo(dog2.breed);
}
但是我怎样才能得到唯一对象的数量。我想要如下所述的输出:
同伴 - 年龄:10 - 人数:1
放牧 - 年龄:10 - 人数:2
将地图定义为:
Map<DogName , Map<DogAge , Count>>
实施:
Map<String , Map<Integer , Integer> > map = new HashMap<>();
// When inputting for object dog1 : You can write this as a function
Map<Integer , Integer> innerMap = map.get(dog1);
if(innerMap != null) {
innerMap.put(dog1.getAge , innerMap.getOrDefault(dog1.getAge , 1));
}
else if(innerMap == null) {
innerMap = new HashMap<>();
innerMap.put(dog1.getAge() , 1);
}
map.put(dog1.getBreed() , innerMap);
// For Printing the output :
for(Map.Entry<String , Map<Integer , Integer>> entry : map.entrySet()) {
for(Map.Entry<Integer , Integer> innerEntry : entry.getValue().entrySet()) {
System.out.println(entry.getKey() + " - Age : " + innerEntry.getKey() + " - count : " + innerEntry.getValue());
}}
希望这对您有所帮助,根据我的理解写了这篇文章。
您可以使用流按品种对狗进行分组,并最终在地图(年龄)的地图(品种)中按年龄对它们进行分组,如下所示:
Map<String, TreeMap<Integer, Long>> collect = Stream.of(dog1, dog2, dog3, dog4, dog5, dog6, dog7, dog8)
.collect(groupingBy(Dog::getBreed,
groupingBy(Dog::getAgeInMonths, TreeMap::new, counting())));
然后你会得到如下输出:
{Companion={7=1, 10=1}, Terrier={9=1, 15=2}, Herding={4=1, 10=2}}
我用 String breed 和 int ageinmonths 创建了一只 Dog class。
String breed;
int ageInMonths;
public String getBreed() {
return breed;
}
public void setBreed(String breed) {
this.breed = breed;
}
public int getAgeInMonths() {
return ageInMonths;
}
public void setAgeInMonths(int ageInMonths) {
this.ageInMonths = ageInMonths;
}
public Dog(String breed, int ageInMonths) {
this.breed = breed;
this.ageInMonths = ageInMonths;
}
public String toString() {
return this.getBreed()+" - Age : "+
this.getAgeInMonths()+" - Count : ";
}
在主要方法中我使用了比较器。
public static void main(String[] args) {
Dog dog1 = new Dog("Companion", 10);
Dog dog2 = new Dog("Herding", 4);
Dog dog3 = new Dog("Terrier", 15);
Dog dog4 = new Dog("Companion", 7);
Dog dog5 = new Dog("Terrier", 15);
Dog dog6 = new Dog("Terrier", 9);
Dog dog7 = new Dog("Herding", 10);
Dog dog8 = new Dog("Herding", 10);
TreeMap<Integer, Dog> dogDetails = new TreeMap<Integer, Dog>();
dogDetails.put(1, dog1);
dogDetails.put(2, dog2);
dogDetails.put(3, dog3);
dogDetails.put(4, dog4);
dogDetails.put(5, dog5);
dogDetails.put(6, dog6);
dogDetails.put(7, dog7);
dogDetails.put(8, dog8);
System.out.println("The dog details are given below : "+dogDetails);
}
@Override
public int compare(Dog dog1, Dog dog2) {
return dog1.breed.compareTo(dog2.breed);
}
但是我怎样才能得到唯一对象的数量。我想要如下所述的输出: 同伴 - 年龄:10 - 人数:1 放牧 - 年龄:10 - 人数:2
将地图定义为:
Map<DogName , Map<DogAge , Count>>
实施:
Map<String , Map<Integer , Integer> > map = new HashMap<>();
// When inputting for object dog1 : You can write this as a function
Map<Integer , Integer> innerMap = map.get(dog1);
if(innerMap != null) {
innerMap.put(dog1.getAge , innerMap.getOrDefault(dog1.getAge , 1));
}
else if(innerMap == null) {
innerMap = new HashMap<>();
innerMap.put(dog1.getAge() , 1);
}
map.put(dog1.getBreed() , innerMap);
// For Printing the output :
for(Map.Entry<String , Map<Integer , Integer>> entry : map.entrySet()) {
for(Map.Entry<Integer , Integer> innerEntry : entry.getValue().entrySet()) {
System.out.println(entry.getKey() + " - Age : " + innerEntry.getKey() + " - count : " + innerEntry.getValue());
}}
希望这对您有所帮助,根据我的理解写了这篇文章。
您可以使用流按品种对狗进行分组,并最终在地图(年龄)的地图(品种)中按年龄对它们进行分组,如下所示:
Map<String, TreeMap<Integer, Long>> collect = Stream.of(dog1, dog2, dog3, dog4, dog5, dog6, dog7, dog8)
.collect(groupingBy(Dog::getBreed,
groupingBy(Dog::getAgeInMonths, TreeMap::new, counting())));
然后你会得到如下输出:
{Companion={7=1, 10=1}, Terrier={9=1, 15=2}, Herding={4=1, 10=2}}