omerhakanbilici
omerhakanbilici

Reputation: 953

Fill list with another object list

Is there any convenient way to move/copy id's from objList to idList?

With java 8 streams maybe?

public class SomeObject() {
  private Long id;
  private String value;

  public Long getId() { return id; }
  public void setId(Long id) { this.id = id; }
  public String getValue() { return value; }
  public void setValue(String value) { this.value = value; }
}

Somewhere in code:

public void doSomething() {

  List<SomeObject> objList = fillWithManyObjects(); //getting objects with values
  List<Long> idList = new ArrayList<Long>();

  objList.forEach(obj -> flightSlotIdSet.add(obj.getId));

}

Upvotes: 3

Views: 3841

Answers (1)

Ash
Ash

Reputation: 2602

Its a simple stream

 List<Long> idList = objList.stream()
     .map(SomeObject::getId)
     .collect(Collectors.toList())

Upvotes: 5

Related Questions