英文:
Hashmap Sorting in ascending order
问题
public class Main {
public static void main(String[] args) {
Dog dog1 = new Dog();
dog1.setName("Lab");
dog1.setAge(3);
Dog dog2 = new Dog();
dog2.setName("Beagle");
dog2.setAge(1);
Dog dog3 = new Dog();
dog3.setName("Golden");
dog3.setAge(6);
Dog dog4 = new Dog();
dog4.setName("German");
dog4.setAge(4);
Dog dog5 = new Dog();
dog5.setName("Husky");
dog5.setAge(2);
List<Dog> dogList = new ArrayList<>();
dogList.add(dog1);
dogList.add(dog2);
dogList.add(dog3);
dogList.add(dog4);
dogList.add(dog5);
Collections.sort(dogList);
System.out.println(dogList);
List<Cat> catList = new ArrayList<>();
catList.add(new Cat("cat1", 2));
catList.add(new Cat("cat2", 6));
catList.add(new Cat("cat3", 7));
catList.add(new Cat("cat4", 4));
catList.add(new Cat("cat5", 3));
Collections.sort(catList);
System.out.println(catList);
Map<Integer, Integer> ageMap = new HashMap<>();
for (int i = 0; i < catList.size(); i++) {
ageMap.put(catList.get(i).getAge(), dogList.get(i).getAge());
}
}
}
请注意,代码中的 "
已经被替换为普通的双引号 "
,<
被替换为小于号 <
,>
被替换为大于号 >
。至于排序和映射部分的内容则保持不变。
英文:
public class Main {
public static void main(String[] args) {
Dog dog1 = new Dog();
dog1.setName("Lab");
dog1.setAge(3);
Dog dog2 = new Dog();
dog2.setName("Beagle");
dog2.setAge(1);
Dog dog3 = new Dog();
dog3.setName("Golden");
dog3.setAge(6);
Dog dog4 = new Dog();
dog4.setName("German");
dog4.setAge(4);
Dog dog5 = new Dog();
dog5.setName("Husky");
dog5.setAge(2);
List<Dog> dogList = new ArrayList<>();
dogList.add(dog1);
dogList.add(dog2);
dogList.add(dog3);
dogList.add(dog4);
dogList.add(dog5);
Collections.sort(dogList);
System.out.println(dogList);
List<Cat> catList = new ArrayList<>();
catList.add(new Cat("cat1", 2));
catList.add(new Cat("cat2", 6));
catList.add(new Cat("cat3", 7));
catList.add(new Cat("cat4", 4));
catList.add(new Cat("cat5", 3));
Collections.sort(catList);
System.out.println(catList);
Map<Integer, Integer> ageMap = new HashMap<>();
}
}
After sorting, I would like to insert the following key-value pairs in a HashMap
- <Cat age, Dog age>
.
答案1
得分: 0
提供两个列表catList
和dogList
都按年龄正确排序,可以通过简单的循环来填充映射。
使用循环的解决方案:
for (int i = 0, n = Math.min(catList.size(), dogList.size()); i < n; i++) {
Cat cat = catList.get(i);
Dog dog = dogList.get(i);
ageMap.put(cat.getAge(), dog.getAge());
}
使用迭代器的解决方案:
Iterator<Cat> itCat = catList.iterator();
Iterator<Dog> itDog = dogList.iterator();
while (itCat.hasNext() && itDog.hasNext()) {
ageMap.put(itCat.next().getAge(), itDog.next().getAge());
}
注意:为了保持ageMap
中条目的顺序,最好将HashMap
替换为按键排序的TreeMap
,或者使用保持插入顺序的LinkedHashMap
,但在我们的情况下,插入将是有序的。
Map<Integer, Integer> ageMap = new TreeMap<>();
英文:
Providing that both lists catList
and dogList
are properly sorted by age, the map can be populated with a simple loop.
for (int i = 0, n = Math.min(catList.size(), dogList.size()); i < n; i++) {
Cat cat = catList.get(i);
Dog dog = dogList.get(i);
ageMap.put(cat.getAge(), dog.getAge());
}
Solution with iterators:
Iterator<Cat> itCat = catList.iterator();
Iterator<Dog> itDog = dogList.iterator();
while (itCat.hasNext() && itDog.hasNext()) {
ageMap.put(itCat.next().getAge(), itDog.next().getAge());
}
Note: in order to maintain the order of entries in the ageMap
it is better to replace HashMap
with TreeMap
which is sorted by keys, or LinkedHashMap
which maintains order of insertion, but in our case the insertion will be ordered.
Map<Integer, Integer> ageMap = new TreeMap<>();
答案2
得分: 0
这是在Java 8中完成此操作的理想方法。如果语法有任何错误,请原谅。
List<Dog> dogList =
list.sort(Comparator.comparing(Dog::getAge));
List<Cat> catList =
list.sort(Comparator.comparing(Cat::getAge));
Map<Integer, Integer> ageMap = new HashMap<>();
int index= catList.size()-1;
if(dogList.size()==catList.size()){
dogList.forEach(dog -> {
ageMap.put(dog.getAge(), catList.get(index).getAge());
index--;
});
}
英文:
This is the ideal way to do this in java 8. Apologies for typo if any, in syntax.
List<Dog> dogList =
list.sort(Comparator.comparing(Dog::getAge));
List<Cat> catList =
list.sort(Comparator.comparing(Cat::getAge));
Map<Integer, Integer> ageMap = new HashMap<>();
int index= catList.size()-1;
if(dogList.size()==catList.size()){
dogList.forEach(dog -> {
ageMap.put(dog.getAge(), catList.get(index).getAge());
index--;
});
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论