如何使用IntStream将String和Integer元素放入LinkedHashMap中?

时间:2018-07-24 13:19:23

标签: java lambda hashmap linkedhashmap

我是LinkedHashMap的新手,我想使用IntStream而不是filter.put(“ 1”,1)将元素<String, Integer>放入地图中,有什么办法吗? ?

private Map<String, Integer> filter = new LinkedHashMap<>();

@PostConstruct
    public void init() throws IdNotFoundException {
        filter.put("1", 1);
        filter.put("2", 2);
        filter.put("3", 3);
        filter.put("4", 4);
        filter.put("5", 5);
        filter.put("6", 6);
        filter.put("7", 7);
        filter.put("8", 8);
        filter.put("9", 9);
        filter.put("10", 10);
        filter.put("All", -1);
    }

1 个答案:

答案 0 :(得分:3)

为此,我建议使用IntStream

 Map<String, Integer> filter = new LinkedHashMap<>();
    IntStream.range(1, 11).forEach(integer -> filter.put(String.valueOf(integer), integer));
    filter.put("All", -1);