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.
Related
This question already has answers here:
Best way to concatenate List of String objects? [duplicate]
(19 answers)
Java: convert List<String> to a join()d String
(23 answers)
Closed 2 months ago.
I have list with some values:
List<String> list = Arrays.asList("1","2","3");
I want convert it to String "1; 2; 3"
I have only idea with loop. It works but it doesn't looks elegant. Maybe somebody can advice more easy way to do it?
Try to use String.join("; ", list)
It should work
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
This question already has answers here:
Java Arraylist remove multiple element by index
(8 answers)
Closed 2 years ago.
I have a List with some elements. My method get random element from List like
ListName.get(num)
How can I remove element from List with that num I've got?
Call List::remove and pass the same number.
myList.remove( num ) ;
listname.remove(elementnumber)
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())
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()...
^^^^^^^^^^