4

I've this class:

private class Item {
  private String transactionId;
  private String user;
  private LocalDate expiration;
  private String confidential;
  private String locked;
}

By other hand, I've five collections:

List<String> transactions;
List<String> users;
List<LocalDate> expirations;
List<String> confidential;
List<String> lockeds;

So I need to map each n of each collection to a new Item object.

Any ideas?

1 Answer 1

10

Stream over the indices (assuming all 5 lists have the same number of elements):

List<Item> items = IntStream.range(0,transactions.size())
                            .mapToObj(i -> new Item(transactions.get(i),
                                                    users.get(i),
                                                    expirations.get(i),
                                                    confidential.get(i),
                                                    lockeds.get(i)))
                            .collect(Collectors.toList());
Sign up to request clarification or add additional context in comments.

5 Comments

It's telling me that IntStream.map has to return an integer... cannot convert from Item to int
@Jordi oops. you're right. I'll fix it. It should be mapToObj
Only one more question... Could I create anonimous type?
@Jordi what do you mean? What would that anonymous type implement/extend?
I mean, if it's possible to create a "dynamic" map object without declaring any class, or declaring it on the fly inside mapToObj

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.