Java8 Streams map 使用

12 篇文章 0 订阅

引言

在 Java 8 中,stream (). Map ()允许您将一个对象转换为其他对象。查看下面例子:

1. 将 List 中的字符串转为大写

public static void main(String[] args) {
    List<String> alpha = Arrays.asList("a", "b", "c", "d");

    //Before Java8
    List<String> alphaUpper = new ArrayList<>();
    for (String s : alpha) {
      alphaUpper.add(s.toUpperCase());
    }

    System.out.println(alpha); //[a, b, c, d]
    System.out.println(alphaUpper); //[A, B, C, D]

    // Java 8
    List<String> collect = alpha.stream().map(String::toUpperCase).collect(Collectors.toList());
    System.out.println(collect); //[A, B, C, D]

    // Extra, streams apply to any data type.
    List<Integer> num = Arrays.asList(1,2,3,4,5);
    List<Integer> collect1 = num.stream().map(n -> n * 2).collect(Collectors.toList());
    System.out.println(collect1); //[2, 4, 6, 8, 10]
  }

2. 将 List 中的对象转为字符串

public class Developer {


  private String name;

  private BigDecimal salary;

  private Integer age;

  //...

}
public static void main(String[] args) {
    List<Developer> persons = Arrays.asList(
        new Developer("zhangsan", 20),
        new Developer("lisi",21),
        new Developer("wangwu",22));

    //Before Java 8
    List<String> result = new ArrayList<>();
    for (Developer developer : persons) {
      result.add(developer.getName());
    }

    System.out.println(result); // [zhangsan, lisi, wangwu]

    //Java 8
    List<String> collect = persons.stream().map(x -> x.getName()).collect(Collectors.toList());
    System.out.println(collect); // [zhangsan, lisi, wangwu]
  }

3. 将 List 中的对象转为另一个对象

public class Person {

  private String name;
  private int age;
  private String extra;

  //...

}
  1. Java 8 之前:
List<Developer> developers = Arrays.asList(
        new Developer("zhangsan", 20),
        new Developer("lisi",21),
        new Developer("wangwu",22));

List<Person> result = new ArrayList<>();

    for (Developer developer : developers) {
      Person person = new Person();
      person.setName(developer.getName());
      person.setAge(developer.getAge());

      if ("lisi".equals(developer.getName())) {
        person.setExtra("i am lisi");
      }

      result.add(person);
    }

    System.out.println(JSONUtil.toJsonStr(result));
  }
  1. java 8
List<Developer> developers = Arrays.asList(
        new Developer("zhangsan", 20),
        new Developer("lisi",21),
        new Developer("wangwu",22));

List<Person> result = developers.stream().map(temp -> {
      Person person = new Person();
      person.setName(temp.getName());
      person.setAge(temp.getAge());

      if ("lisi".equals(temp.getName())) {
        person.setExtra("i am lisi");
      }
      return person;
    }).collect(Collectors.toList());

    System.out.println(JSONUtil.toJsonStr(result));        

源码见:java-8-demo

系列文章详见:Java 8 教程

欢迎关注

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值