java.util.stream.ReferencePipeline$3@396555a1
is the outcome of trying to print the Stream you have created until the map operation performed.
You are looking to collect the user names mapped until now while joining then with a \n delimiter.
String users = memberNames.stream()
.map(user -> String.format("The user name is %s and his lastname is %s", user.name, user.lastname))
.collect(Collectors.joining("\n"));
System.out.println(users); // to print them all at once
Answer from Naman on Stack Overflowjava streams java.util.stream.ReferencePipeline$3@7291c18f - Stack Overflow
dictionary - Most elegant way to join a Map to a String in Java 8 - Stack Overflow
Converting Stream to a List of user defined object
Why does my Clojure implementation of Conway's Game of Life run so much slower than my Java implementation?
You can grab the stream of the map's entry set, then map each entry to the string representation you want, joining them in a single string using Collectors.joining(CharSequence delimiter).
import static java.util.stream.Collectors.joining;
String s = attributes.entrySet()
.stream()
.map(e -> e.getKey()+"="+e.getValue())
.collect(joining("&"));
But since the entry's toString() already output its content in the format key=value, you can call its toString method directly:
String s = attributes.entrySet()
.stream()
.map(Object::toString)
.collect(joining("&"));
public static void main(String[] args) {
HashMap<String,Integer> newPhoneBook = new HashMap(){{
putIfAbsent("Arpan",80186787);
putIfAbsent("Sanjay",80186788);
putIfAbsent("Kiran",80186789);
putIfAbsent("Pranjay",80186790);
putIfAbsent("Jaiparkash",80186791);
putIfAbsent("Maya",80186792);
putIfAbsent("Rythem",80186793);
putIfAbsent("Preeti",80186794);
}};
/**Compining Key and Value pairs and then separate each pair by some delimiter and the add prefix and Suffix*/
String keyValueCombinedString = newPhoneBook.entrySet().stream().
// you can improve the performance by avoiding "+"
// "map(entrySet -> "%s:%s".formatted(entrySet.getKey(), entrySet.getValue())."
map(entrySet -> entrySet.getKey() + ":"+ entrySet.getValue()).
collect(Collectors.joining("," , "[","]"));
System.out.println(keyValueCombinedString);
/**
* OUTPUT : [Kiran:80186789,Arpan:80186787,Pranjay:80186790,Jaiparkash:80186791,Maya:80186792,Sanjay:80186788,Preeti:80186794,Rythem:80186793]
*
* */
String keyValueCombinedString1 = newPhoneBook.entrySet().stream().
map(Objects::toString).
collect(Collectors.joining("," , "[","]"));
System.out.println(keyValueCombinedString1);
/**
* Objects::toString method concate key and value pairs by =
* OUTPUT : [Kiran=80186789,Arpan=80186787,Pranjay=80186790,Jaiparkash=80186791,Maya=80186792,Sanjay=80186788,Preeti=80186794,Rythem=80186793]
* */
}
> Blockquote
Hello, I'm new to this, please bear with me. I have a List of user defined objects like this:
List<User> users;
Along the way I'll be populating users, after that I'll be filtering it as well. What I did first for filtering was this:
users = users.stream().filter(u -> u.name.equals(otherName))
But I got an error like this: Cannot cast object 'java.util.stream.ReferencePipeline$2@7c3e47d3' with class 'java.util.stream.ReferencePipeline$2' to class 'java.util.List', so I changed the code to this:
users = users.stream().filter(u -> u.name.equals(otherName))
.collect(Collectors.toList())But I got another error that goes something like this:
No such property: Collectors for class: com.users.graphql.$UserServiceDefinition$Intercepted
What can I do to successfully convert stream to a List<User>? I hope you can help, thanks!
EDIT :
I'm using groovy so I should've used the filter for groovy like this:
users = users.findAll{ it.name.equals(otherName) }