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?

1
  • 4
    Also, the original code is not terrible and still works perfectly fine with JDK8+ ... Commented Jan 25, 2019 at 6:39

5 Answers 5

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));
Sign up to request clarification or add additional context in comments.

Comments

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

customers
    .stream()
    .map(Customer::getState)
    .forEach(states::add);

4 Comments

"List<State> 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.
@ABHISHEKHONEY this is not bad. That was just another way.
@ABHISHEKHONEY they said that " that was preferable" not bad :) because it doesn't require mutating a collection and it is readable.
0

Using Lambda and forEach

customers.forEach(p -> {
        states.add(p.getState())  
      }
    );

Comments

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());

1 Comment

There's a syntax error in your function declaration: Replace the lambda operator after getCustomerStates with assignment operator.
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());

5 Comments

If state is already a List, then states.add wouldn't have worked. The OP should have used addAll
@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?
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
@user7 did you read my answer or not? I specific said Worth to mentions that if... and I actually did define a List<State> states in the first code part? Is it not enough?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.