简体   繁体   English

在地图界面上应用过滤器和地图后,如何同时显示键和值?

[英]How to display both key and value after applying filter and map on Map Interface?

//i have a list of student type 
List<Student> list2 = new ArrayList<>();
        list2.add(new Student(101, "piyush"));
        list2.add(new Student(102, "Raman"));
        list2.add(new Student(109, "Raman"));

//i converted this list to Map




    Map<Integer, String> map3=list2.stream()
                    .collect(Collectors.
                    toMap(Student::getStudentId, Student::getStudName ));

//now i converted it to stream and applied some fiter and map

       map3.entrySet()
    .stream().filter(i -> i.getKey()==131 || i.getKey()==101).map(i-> i.getValue().toUpperCase())
    .forEach(System.out::println);


//above code displays only name in UpperCase

// but i want to display both id and name(Upper case) what should i do. //但是我想同时显示id和name(大写)该怎么办。

     map3.entrySet()
    .stream().filter(i -> i.getKey()==131 || i.getKey()==101)
    .forEach(System.out::println);

/ this code displays both id and name so y the above forEach loop is not displaying it. / 此代码同时显示了ID和名称,因此上述forEach循环未显示它。 i even tried to store result in Map using collector but that is not working. 我什至尝试使用收集器将结果存储在Map中,但这不起作用。 / /

// not working //不起作用

    Map<Integer,String> map4= map3.entrySet()
    .stream().filter(i -> i.getKey()==131 || i.getKey()==101).map(i-> i.getValue().toUpperCase()).
    collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue));

If the output is not what you desire, this means the Map.Entry implementation returned by your stream probably doesn't override Object 's toString , so you have to specify how to print the entry: 如果输出不是您想要的,则意味着流返回的Map.Entry实现可能不会覆盖ObjecttoString ,因此您必须指定如何打印条目:

map3.entrySet()
    .stream().filter(e -> e.getKey() == 131 || e.getKey() == 101)
    .forEach(e -> System.out.println(e.getKey() + " " + e.getValue().toUpperCase()));

However, looking at your full code, I'm not sure you need to create that map in the first place. 但是,查看您的完整代码,我不确定您是否首先需要创建该地图。 You can filter the original list and get the same output: 您可以过滤原始列表并获得相同的输出:

list2.stream()
     .filter(s -> s.getStudentId() == 131 || s.getStudentId() == 101)
     .forEach(s -> System.out.println(s.getStudentId() + " " + s.getStudName ().toUpperCase()));

BTW, if your original list would contain multiple Student s having the same ID, your Collectors.toMap would fail. 顺便说一句,如果您的原始列表包含多个具有相同ID的Student ,则Collectors.toMap将失败。

this code displays both id and name so y the above forEach loop is not displaying it 该代码同时显示了id和name,因此上面的forEach循环未显示它

  • Following code snippet not displaying key cause intermediate operation .map(i-> i.getValue().toUpperCase()) process elements from stream and return stream of student value element ( i-> i.getValue() ) not key. 以下代码片段未显示键会导致中间操作.map(i-> i.getValue().toUpperCase())处理流中的元素,并返回学生值元素的流( i-> i.getValue() )不是键。

     map3.entrySet() stream().filter(i -> i.getKey()==131 || i.getKey()==101).map(i->i.getValue().toUpperCase()) .forEach(System.out::println); 
  • Following code showing key and value because you just filter() stream element and it returns streams of student element ie student(131), student(101) which you iterate over through. 下面的代码显示了键和值,因为您只filter() stream filter()流元素,它返回了学生元素的流,即您迭代遍历的student(131),student(101)。

     map3.entrySet() .stream().filter(i -> i.getKey()==131 || i.getKey()==101) .forEach(System.out::println); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM