Join a list of object’s properties into a String

To retrieve a String consisting of all the ID’s separated by the delimiter “,” you first have to map the Person ID’s into a new stream which you can then apply Collectors.joining on. String result = personList.stream().map(Person::getId) .collect(Collectors.joining(“,”)); if your ID field is not a String but rather an int or some other primitive numeric … Read more

Java 8 lambda create list of Strings from list of custom class objects

You need to collect your Stream into a List: List<String> adresses = users.stream() .map(User::getAdress) .collect(Collectors.toList()); For more information on the different Collectors visit the documentation. User::getAdress is just another form of writing (User user) -> user.getAdress() which could as well be written as user -> user.getAdress() (because the type User will be inferred by the … Read more

Java Stream API – Best way to transform a list: map or forEach?

Don’t worry about any performance differences, they’re going to be minimal in this case normally. Method 2 is preferable because it doesn’t require mutating a collection that exists outside the lambda expression. it’s more readable because the different steps that are performed in the collection pipeline are written sequentially: first a filter operation, then a … Read more

Collect stream of EntrySet to LinkedHashMap

You can do this with Stream: Map<String, Object> result = resources.entrySet() .stream() .filter(e -> keys.contains(e.getKey())) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (x, y) -> y, LinkedHashMap::new)); The part (x, y) -> y is because of mergeFunction when find duplicate keys, it returns value of second key which found. the forth part is mapFactory which a supplier providing a new … Read more

Using multiple map functions vs. a block statement in a map in a java stream

Either is fine. Pick the one that seems more readable to you. If the calculation naturally decomposes, as this one does, then the multiple maps is probably more readable. Some calculations won’t naturally decompose, in which case you’re stuck at the former. In neither case should you be worrying that one is significantly more performant … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)