2

I have a custom Class like this:

public class Client {
    public int ID;
    public String Name;
    public double buys;
    public double getBuys() {
        return this.buys;
    }
}

Then, I have defined a list of Client, like:

List<Client> clientList;

Let's say that that List has a lot of client objects, already initialized. How can I map the field "buys" of each client object using Java 8?

I've tried something like:

List<Client> clientList2 = clientList.stream().map(c.getBuys() -> c.getBuys() + 1).collect(Collections.toList());

It, of course, isn't working, but i can't figure out (or find) any way to do it. What I want to do is just to modify every "buys" value of each object of the list. In the example case, I'm adding one, but it could be any operation.

6
  • 3
    Do you want to add 1 to the buys of each Client? Commented Oct 11, 2015 at 13:29
  • 2
    It's not clear what you are trying to do with the buys field. What should the output look like? Commented Oct 11, 2015 at 13:30
  • 1
    What do you mean exactly by "map" here? It seems that you want to get a list of client with a existing list. But what do you want the new list to be? Commented Oct 11, 2015 at 13:31
  • Sorry for been uncleared guys. Thank you for your comments anyway. What i want to do is just modify every buys value of each object of the list. In the example case, adding one, but it could be any operation. Commented Oct 11, 2015 at 13:32
  • 2
    It would be very easy to make a new list where the clients are new objects with incremented buys fields. Alternatively, If you want to mutate the existing client objects you would use forEach not map. Commented Oct 11, 2015 at 13:33

3 Answers 3

1

After reading your comment, what you want is to modify the buys of each client by applying a custom function. Consider the following method:

private static List<Client> mapClients(List<Client> clients, DoubleUnaryOperator op) {
    return clients.stream()
                  .peek(c -> c.setBuys(op.applyAsDouble(c.getBuys())))
                  .collect(Collectors.toList());
}

or the following simple for loop:

private static List<Client> mapClients(List<Client> clients, DoubleUnaryOperator op) {
    for (Client client : clients) {
        client.setBuys(op.applyAsDouble(client.getBuys()))
    }
    return clients;
}

This method will mutate every Client and set the buys with the result of the operator (a DoubleUnaryOperator is an operator that takes a double as argument and returns a value of type double). The operator is getting the buys as input to make the calculation. Then, you could use it like this:

mapClients(clients, d -> d + 1); // every buys are added 1
mapClients(clients, d -> 2*d); // every buys are multiplied by 2

Note that this solution mutates an existing Object, which is not a nice practice. It would be better to have a constructor of Client taking the buys as input and mapping the result of the operation to a new Client, like this:

private static List<Client> mapClients(List<Client> clients, DoubleUnaryOperator op) {
    return clients.stream()
                  .map(c -> new Client(op.applyAsDouble(c.getBuys())))
                  .collect(Collectors.toList());
}
Sign up to request clarification or add additional context in comments.

Comments

1

I would just use a for loop.

for (Client client : clientList)
    client.setBuys(client.getBuys() + 1);

2 Comments

In case of having a large amounts of clients in the list, would it be efficient enough?
You could use parallelism like this: clientList.parallelStream().forEach(c -> { c.setBuys(c.getBuys() + 1); });.
0

Have you tried using Iterator provided by Java?

for( Client c: clientList){ 
System.out.println(c.getBuys());
}

Or add the result to a map or whatever.

2 Comments

Same questions, in case of having a large amounts of clients in the list, would it be efficient enough?
I think this feature is optimized at low level, but i don't know if it could be better on specific collection (LinkedList, Maps, ecc). But I think it's always good to use foreach statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.