概述
在jdk 1.8
里,可以使用如下代码获取list
元素对象中某个属性的列表。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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);
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
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]
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对服务器之家的支持。如果你想了解更多相关内容请查看下面相关链接
原文链接:https://blog.csdn.net/linsongbin1/article/details/83933184