哈希映射升序排序

huangapple go评论60阅读模式
英文:

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());
        }
    }
}

请注意,代码中的 &quot; 已经被替换为普通的双引号 "&lt; 被替换为小于号 <&gt; 被替换为大于号 >。至于排序和映射部分的内容则保持不变。

英文:
public class Main {
    public static void main(String[] args)  {

        Dog dog1 = new Dog();
        dog1.setName(&quot;Lab&quot;);
        dog1.setAge(3);

        Dog dog2 = new Dog();
        dog2.setName(&quot;Beagle&quot;);
        dog2.setAge(1);

        Dog dog3 = new Dog();
        dog3.setName(&quot;Golden&quot;);
        dog3.setAge(6);

        Dog dog4 = new Dog();
        dog4.setName(&quot;German&quot;);
        dog4.setAge(4);

        Dog dog5 = new Dog();
        dog5.setName(&quot;Husky&quot;);
        dog5.setAge(2);

        List&lt;Dog&gt; dogList = new ArrayList&lt;&gt;();
        dogList.add(dog1);
        dogList.add(dog2);
        dogList.add(dog3);
        dogList.add(dog4);
        dogList.add(dog5);
        Collections.sort(dogList);
        System.out.println(dogList);

        List&lt;Cat&gt; catList = new ArrayList&lt;&gt;();
        catList.add(new Cat(&quot;cat1&quot;, 2));
        catList.add(new Cat(&quot;cat2&quot;, 6));
        catList.add(new Cat(&quot;cat3&quot;, 7));
        catList.add(new Cat(&quot;cat4&quot;, 4));
        catList.add(new Cat(&quot;cat5&quot;, 3));
        Collections.sort(catList);
        System.out.println(catList);
        
        Map&lt;Integer, Integer&gt; ageMap = new HashMap&lt;&gt;();
  }
}

After sorting, I would like to insert the following key-value pairs in a HashMap - &lt;Cat age, Dog age&gt;.

答案1

得分: 0

提供两个列表catListdogList都按年龄正确排序,可以通过简单的循环来填充映射。

使用循环的解决方案:

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 &lt; n; i++) {
    Cat cat = catList.get(i);
    Dog dog = dogList.get(i);
    ageMap.put(cat.getAge(), dog.getAge());
}

Solution with iterators:

Iterator&lt;Cat&gt; itCat = catList.iterator();
Iterator&lt;Dog&gt; itDog = dogList.iterator();

while (itCat.hasNext() &amp;&amp; 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&lt;Integer, Integer&gt; ageMap = new TreeMap&lt;&gt;();

答案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&lt;Dog&gt; dogList = 
list.sort(Comparator.comparing(Dog::getAge));
List&lt;Cat&gt; catList = 
list.sort(Comparator.comparing(Cat::getAge));
Map&lt;Integer, Integer&gt; ageMap = new HashMap&lt;&gt;();
int index= catList.size()-1;
if(dogList.size()==catList.size()){
dogList.forEach(dog -&gt; {
ageMap.put(dog.getAge(), catList.get(index).getAge());
index--;
});
}

huangapple
  • 本文由 发表于 2020年10月13日 02:12:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/64323203.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定