Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HashMap如何根据Key的自然排序输出?Value呢? #2

Open
lycying opened this issue Dec 7, 2020 · 6 comments
Open

HashMap如何根据Key的自然排序输出?Value呢? #2

lycying opened this issue Dec 7, 2020 · 6 comments

Comments

@lycying
Copy link
Member

lycying commented Dec 7, 2020

问题:HashMap如何根据Key的自然排序输出?Value呢?
意图:是否能够灵活应用Java的API。如果这道题都答不出来,那面试可想而知。

在平常的面试中,我发现,确实有很大一部分同学答不上来。这是缺乏实践经验和联想能力,只能说sorry了

@lycying
Copy link
Member Author

lycying commented Dec 7, 2020

第一种方式,使用stream流。其实很乱的代码。

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class HashX {
    public static void main(String[] args) {
        Map<String,String> map = new HashMap(){{
            put("2","b");
            put("3","c");
            put("1","a");
            put("4","d");
        }};

        Map<String,String> linkedHashMap = new LinkedHashMap<>(map);

        map.entrySet()
                .stream()
                .sorted(Map.Entry.comparingByKey())
                //.sorted() //java.util.HashMap$Node cannot be cast to java.lang.Comparable
                .forEachOrdered(e->linkedHashMap.put(e.getKey(),e.getValue()));

        for(Map.Entry<String, String> e : linkedHashMap.entrySet()){
            System.out.println(e.getKey() + ", " + e.getValue());
        }
    }
}

@lycying
Copy link
Member Author

lycying commented Dec 7, 2020

更简洁的思路,使用TreeMap转换一下即可。

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class HashX {
    public static void main(String[] args) {
        Map<String,String> map = new HashMap(){{
            put("2","b");
            put("3","c");
            put("1","a");
            put("4","d");
        }};

        Map<String,String> treeMap = new TreeMap(map);
        for(Map.Entry<String, String> e : treeMap.entrySet()){
            System.out.println(e.getKey() + ", " + e.getValue());
        }
    }
}

@lycying
Copy link
Member Author

lycying commented Dec 7, 2020

使用list等结构缓存。

import java.util.HashMap;
import java.util.Map;

public class HashX {
    public static void main(String[] args) {
        Map<String, String> map = new HashMap() {{
            put("2", "b");
            put("3", "c");
            put("1", "a");
            put("4", "d");
        }};

        map.keySet().stream().sorted().forEach(
                k -> System.out.println(k + ", " + map.get(k))
        );
    }
}

@lycying
Copy link
Member Author

lycying commented Dec 7, 2020

根据value排序更有意思一些,可以直接使用lambda的sorted方法

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class HashX {
    public static void main(String[] args) {
        Map<String, String> map = new HashMap() {{
            put("2", "b");
            put("3", "c");
            put("1", "a");
            put("4", "d");
            put("5", null);
        }};

        map.entrySet().stream().sorted((o1, o2) -> {
            if (Objects.isNull(o1.getValue())) {
                return -1;
            }
            return o1.getValue().compareTo(o2.getValue());
        }).forEach(e -> System.out.println(e.getKey() + ", " + e.getValue()));
    }
}

@lycying
Copy link
Member Author

lycying commented Dec 7, 2020

茴香豆的n种写法,但假如你一种都不会,那问题才算大。

@lycying
Copy link
Member Author

lycying commented Dec 10, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant