0
List<Customer> customers = findAllCustomer();   

public class Customer implements Serializable {

    private State state;

    //getter and setter

I have below approached using jdk 7

List<State> states = new ArrayList<>();

for (Customer customer : customers) {
    states.add(customer.getState());
}   

How can I achieve the same thing using jdk 8?

Shiladittya Chakraborty
  • 4,270
  • 8
  • 45
  • 94
  • 4
    Also, the original code is not terrible and still works perfectly fine with JDK8+ ... – Thilo Jan 25 '19 at 06:39

5 Answers5

7

Stream the contents, map to get the state and collect it as a List.

customers.stream()
    .map(Customer::getState)
    .collect(Collectors.toList());

If you need an ArrayList as the resultant list

customers.stream()
    .map(Customer::getState)
    .collect(Collectors.toCollection(ArrayList::new));
Thiyagu
  • 17,362
  • 5
  • 42
  • 79
0
List<State> states = new ArrayList<>();

customers
    .stream()
    .map(Customer::getState)
    .forEach(states::add);
Khalid Shah
  • 3,132
  • 3
  • 20
  • 39
  • "List states = new ArrayList<>(); " why are you declaring the arraylist if you are using java 8? You should never do it like this. Always use collect and Collectors. This is bad. – Abhishek Honey Jan 25 '19 at 06:56
  • 1
    @ABHISHEKHONEY this is not bad. That was just another way. – Khalid Shah Jan 25 '19 at 06:57
  • Kindly refer this :) https://stackoverflow.com/questions/28319064/java-8-best-way-to-transform-a-list-map-or-foreach – Abhishek Honey Jan 25 '19 at 07:03
  • @ABHISHEKHONEY they said that " that was preferable" not bad :) because it doesn't require mutating a collection and it is readable. – Khalid Shah Jan 25 '19 at 07:04
0

Using Lambda and forEach

customers.forEach(p -> {
        states.add(p.getState())  
      }
    );
DirtyMind
  • 2,353
  • 2
  • 22
  • 43
0
List<State> states = customers.stream()
                              .map(Customer::getState)
                              .collect(Collectors.toList());

Additionally you can wrap this issue to the static method:

public static List<State> getCustomerStates(List<Customer> customers) {
   return customers.stream()
                   .map(Customer::getState)
                   .collect(Collectors.toList());
}

...or to the function:

private static final Function<List<Customer>, List<State>> getCustomerStates =
        customers -> customers.stream()
                              .map(Customer::getState)
                              .collect(Collectors.toList());
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35
  • There's a syntax error in your **function** declaration: Replace the lambda operator after `getCustomerStates` with assignment operator. – Andy Sug Jan 25 '19 at 07:47
0

Worth to mentions that if the state is actually a List<state> (states)

public class Customer implements Serializable {
private List<State> states;//Important note: I changed State to a List<State> here

//getter and setter

It will be a little bit tricky to get a List of states here

List<State> states = customers.stream()
    .map(Customer::getStates)
    .filter(Objects::nonNull)//null check
    .flatMap(Collection::stream)
    .collect(Collectors.toList());
Dang Nguyen
  • 1,209
  • 1
  • 17
  • 29
  • If state is already a List, then `states.add` wouldn't have worked. The OP should have used `addAll` – Thiyagu Jan 25 '19 at 08:00
  • @user7 Sorry but I don't get your comment? My answer mentions another case OP might need. And I didn't say anything about `add` and `addAll`? – Dang Nguyen Jan 25 '19 at 08:07
  • If it is another case, then it is fine. But in OP `state` cannot be a List. So, this doesn't answer the **current** question – Thiyagu Jan 25 '19 at 08:08
  • @user7 did you read my answer or not? I specific said `Worth to mentions that if...` and I actually did define a `List states` in the first code part? Is it not enough? – Dang Nguyen Jan 25 '19 at 08:10
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/187310/discussion-between-dang-nguyen-and-user7). – Dang Nguyen Jan 25 '19 at 09:30