如何在Java8中过滤内部映射 static final List ALL_TYPES=AuthInt.getScopeAssociations().stream() .map(a->a.getLeft().getSimpleName()) .collect(Collectors.toList());

如何在Java8中过滤内部映射 static final List ALL_TYPES=AuthInt.getScopeAssociations().stream() .map(a->a.getLeft().getSimpleName()) .collect(Collectors.toList());,java,java-stream,Java,Java Stream,列表中有一个名字“MarieAntra”,我只想添加为Marie。。如何使用Streams实现这一点?在map中,只要返回正确的类型,就可以编写整个函数,因此没有理由不能编写这样的内容 List<String> ALL_TYPES = AuthInt.getScopeAssociations().stream() .map(a -> { if ("MarieAntra".equals(a.getLeft().ge

列表中有一个名字“MarieAntra”,我只想添加为Marie。。如何使用Streams实现这一点?

map
中,只要返回正确的类型,就可以编写整个函数,因此没有理由不能编写这样的内容

List<String> ALL_TYPES = AuthInt.getScopeAssociations().stream()

    .map(a -> {
                  if ("MarieAntra".equals(a.getLeft().getSimpleName())) return "Marie";
                  return a.getLeft().getSimpleName()
              }
   ).collect(Collectors.toList());
static final List ALL_TYPES=AuthInt.getScopeAssociations().stream()
.map(a->a.getLeft().getSimpleName())
.map(名称->“MarieAntra”。等于(名称)?“Marie”:名称)
.collect(Collectors.toList());

映射中
只要返回正确的类型,就可以编写整个函数,因此没有理由不能编写完整的函数

List<String> ALL_TYPES = AuthInt.getScopeAssociations().stream()

    .map(a -> {
                  if ("MarieAntra".equals(a.getLeft().getSimpleName())) return "Marie";
                  return a.getLeft().getSimpleName()
              }
   ).collect(Collectors.toList());
列出所有类型=AuthInt.getScopeAssociations().stream()
.地图(a->{
if(“mariantra”.equals(a.getLeft().getSimpleName())返回“Marie”;
返回a.getLeft().getSimpleName()
}
).collect(Collectors.toList());

一种方法是简单地提取具有该逻辑的方法

  public static String filterString(String s){
        return s.equals("MarieAntra") ? "Marie" : s;
    }
并在流中使用,如下所示:

AuthInt.getScopeAssociations().stream()
                              .map(a -> filterString(a))
                              .collect(Collectors.toList());
或者直接在流上:

AuthInt.getScopeAssociations()
       .stream()
       .map(a -> a.getLeft().getSimpleName().equals("MarieAntra") ? 
                                            "Marie" : 
                                            a.getLeft().getSimpleName())
       .collect(Collectors.toList());