java 集合存多种属性,Java Stream - 如何收集一个属性并保存到列表中

import java.util.ArrayList;

import java.util.List;

import java.util.stream.Collectors;

public class Main {

public static void main(String[] args) {

List trades = TradeUtil.createTrades();

List list = trades.stream()

.map(t->t.getQuantity())

.collect(Collectors.toList());

System.out.println("Collect List: "+list);

}

}

class TradeUtil {

public static List createTrades(){

List trades = new ArrayList();

Trade t = new Trade(1,"IBM",1000,"NEW");

trades.add(t);

t = new Trade(2,"APPL",2000,"NEW");

trades.add(t);

t = new Trade(3,"IBM",3000,"NEW");

trades.add(t);

t = new Trade(4,"IBM",4000,"PENDING");

trades.add(t);

return trades;

}

public static List createTrades(int size){

List trades = new ArrayList<>();

for (int i = 0; i < size; i++) {

Trade t = new Trade(i,"ISUER "+i,i+1000,"NEW");

trades.add(t);

}

return trades;

}

}

class Trade {

private int tradeId = 0;

private String issuer = null;

private int quantity = 0;

private String status = null;

public Trade(int id, String issuer, int quantity, String status) {

this.tradeId = id;

this.issuer = issuer;

this.quantity = quantity;

this.status = status;

}

public int getTradeId() {

return tradeId;

}

public void setTradeId(int tradeId) {

this.tradeId = tradeId;

}

public String getIssuer() {

return issuer;

}

public void setIssuer(String issuer) {

this.issuer = issuer;

}

public int getQuantity() {

return quantity;

}

public void setQuantity(int quantity) {

this.quantity = quantity;

}

public String getStatus() {

return status;

}

public void setStatus(String status) {

this.status = status;

}

@Override

public String toString() {

return "Trade{" + "tradeId=" + tradeId + ", issuer=" + issuer

+ ", quantity=" + quantity + ", status=" + status + '}';

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值