Java streams statement: find the error [duplicate] - java

This question already has answers here:
Why does this java 8 stream operation evaluate to Object instead of List<Object> or just List?
(2 answers)
Why do we have to cast the List returned by Collectors.toList() to List<Integer> even though the elements of the Stream are already mapped to Integer? [duplicate]
(1 answer)
What is a raw type and why shouldn't we use it?
(16 answers)
Closed 4 years ago.
This works as expected
Collection<String> items = combo.getItems();
items.stream().filter(item -> item.startsWith("New")).findFirst()...
But this fails to compile. Why?
Collection items = combo.getItems();
items.stream().map(Object::toString).filter(item -> item.startsWith("New")).findFirst()...
^^^^^^^^^^

Related

what is the meaning of list.toString(new int[0][]); [duplicate]

This question already has answers here:
What's the use of new String[0] in toArray(new String[0]);
(3 answers)
.toArray(new MyClass[0]) or .toArray(new MyClass[myList.size()])?
(8 answers)
What does "new Boolean[0]" or "new String[0]" in Java evaluate to [duplicate]
(3 answers)
Closed 7 months ago.
I am new to java, I have encountered a statement in "merge sublists" program,
list.toArray(new int[0][]); I found this statement in the end of program. Here list consist of list of integer array. It has some data, but why they are using a 2d array of 0 rows in above statement.
Please anyone give a detailed explanation

transform List using lambda expressions [duplicate]

This question already has answers here:
How can I turn a List of Lists into a List in Java 8?
(12 answers)
Closed 2 years ago.
I have created a function to transform the elements of a list:
private List<Hostel> build(List<Hotel> hotels) {
return hotels.stream().map(h -> convert(h)).collect(toList());
}
but I have a compilation error:
required type: List<Hostel>
Provided: List<List<Hostel>>
From your error it seems convert(h) return a List<Hostel>, for that when you use a map, and collect the result is List<List<Hostel>>, to get List<Hostel>, you have to use flatMap instead of map, like this:
.flatMap(h -> convert(h).stream())

Java - Trying to remove certain elements from List<String> [duplicate]

This question already has answers here:
Why do I get an UnsupportedOperationException when trying to remove an element from a List?
(17 answers)
Closed 4 years ago.
I'm trying to remove words starting with &, but the followning code gives me the following error:
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractList.remove(AbstractList.java:161)
at java.util.AbstractList$Itr.remove(AbstractList.java:374)
at java.util.Collection.removeIf(Collection.java:415)
at com.APT.SearchEngine.Main.main(Main.java:25)
String text="Hey &i love football";
List<String> purifiedList= Arrays.asList(text.split(" "));
purifiedList.removeIf((String word) -> word.startsWith("&"));
Arrays.asList returns an unmodifiable list.

JAVA8: Map list of objects to String[] [duplicate]

This question already has answers here:
How to convert a Java 8 Stream to an Array?
(9 answers)
Closed 5 years ago.
I have a List of 'Client' objects each one with a field "email".
I need something like:
List<String> listEmails = clients.stream().map(client->client.getEmail())
.collect(Collectors.toList());
...but returning directly a String[].
Is there a proper way to map a List<Client> to a String[] listEmails using Java 8 streams?
Sure :
String[] result = clients
.stream()
.map(client->client.getEmail())
.toArray(String[]::new)

How to assign a List<List<String>> variable to a empty list in java? [duplicate]

This question already has answers here:
Collections.emptyList() vs. new instance
(7 answers)
Closed 7 years ago.
I have a line of code
List<List<String>> get_valuesofValues = new ArrayList<List<String>>((Collection<? extends List<String>>)map.get(value_atIndex));
"get_valuesofValues" will be null in some places. How can I assign it to a empty list in order to avoid exception?
Should be something like this :
if(get_valuesofValues == null){
get_valuesofValues =((Collection.<List<String>>)emptyList();
}
Use Collections.emptyList() method. Please note, that such collections cannot be modified (add/replace/remove of elements produces an exception).

Categories