欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

JAVA JDK8 List获取属性列表

时间:2021-03-15 09:53:18|栏目:JAVA代码|点击:

概述

JDK 1.8里,可以使用如下代码获取List元素对象中某个属性的列表。

package test;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class ListAttrTest {
  public static void main(String[] args) {
    List<Coupon> couponList = new ArrayList<>();
    Coupon coupon1 = new Coupon(1,100,"优惠券1");
    Coupon coupon2 = new Coupon(2,200,"优惠券2");
    Coupon coupon3 = new Coupon(3,300,"优惠券3");
    couponList.add(coupon1);
    couponList.add(coupon2);
    couponList.add(coupon3);
    List<Integer> resultList = couponList.stream().map(Coupon::getCouponId).collect(Collectors.toList());
    System.out.println(resultList);
  }
}
public class Coupon {
  private Integer couponId;
  private Integer price;
  private String name;
  public Coupon(Integer couponId, Integer price, String name) {
    this.couponId = couponId;
    this.price = price;
    this.name = name;
  }
  public Integer getCouponId() {
    return couponId;
  }
  public void setCouponId(Integer couponId) {
    this.couponId = couponId;
  }
  public Integer getPrice() {
    return price;
  }
  public void setPrice(Integer price) {
    this.price = price;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
}

打印结果如下:

[1, 2, 3]

总结

上一篇:MyBatis在insert插入操作时返回主键ID的配置(推荐)

栏    目:JAVA代码

下一篇:springboot用thymeleaf模板的paginate分页完整代码

本文标题:JAVA JDK8 List获取属性列表

本文地址:http://www.codeinn.net/misctech/81344.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有