list stream:将对象List中的某个字段放到新的List中

public static void main(String[] args) {
    List<TAdslot> userList = new ArrayList<>();
    TAdslot adslot1 = new TAdslot();
    adslot1.setName("One");
    TAdslot adslot2 = new TAdslot();
    adslot2.setName("Zero");
    TAdslot adslot3 = new TAdslot();
    adslot3.setName("Two");
    TAdslot adslot4 = new TAdslot();
    adslot4.setName("Four");
    userList.add(adslot1);
    userList.add(adslot2);
    userList.add(adslot3);
    userList.add(adslot4);

    List<String> stringList = userList.stream().map(TAdslot::getName).collect(Collectors.toList());
    
    System.out.println(stringList);
}

输出结果:
[One, Zero, Two, Four]

你可能感兴趣的:(jdk,java)