Is it possible to change all list items in java?

I have a list Stringand I want to perform the same operation on everyone Stringin the list.

Is this possible without performing a loop?

+5
source share
13 answers

You can use apache commons util .

+2
source

Well, something in a loop, somewhere - if you want to distract this into your own method, you can do it, but I do not believe that anything is built into the framework.

Guava Iterables .., , , . , ( ), .

Java, , , - "-" , , .

+9

, , . - Python ( , ).

, - "".

+4

. .

 for(String s:yourlist){
       dooperation(s);
 }
+4

?

, , . .

, - , , . , , - :

map(list, new Mapper<String, String>() {
    public String map(String input) {
        return doSomethingToString(input);
    }
);

, , ,

for (int i = 0; i < list.size(); i += 1) {
    list[i] = doSomethingToString(list[i]);
}

.

map(list, new DoSomethingToStringMapper());
map(otherlist, new DoSomethingToStringMapper());

, , . - .

+3

, - , - .

+2

Java . , Groovy * . , . apply , , , - API . !

+2

. , / ( concurrency), , , ( , , , ?). , . , .

+2

, .

+1

, .

List, , (removeAll ..).

+1

Java API . Arraylist

, Arraylist java.util.ArrayList

ArrayList, .

import java.util.ArrayList;
//..
ArrayList ajay = new ArrayList(); 

  • ArrayList →

  • ajay →

, Arraylist:

ArrayList ajay<String> = new ArrayList<String>(10);

Arraylist .

add() ArrayList.And remove() .

:

import java.util.ArrayList;

public class MyClass {
    public static void main(String[ ] args) {
        ArrayList<String> ajay = new ArrayList<String>();
        ajay.add("Red");
        ajay.add("Blue");
        ajay.add("Green");
        ajay.add("Orange");
        ajay.remove("Green");

        System.out.println(colors);
    }
}

:

[Red,Blue,Orange]
+1

The accepted return link is broken and the proposed solution is out of date:

CollectionUtils::forAllDo

@Deprecated

public static <T,C extends Closure<? super T>> C forAllDo(Iterable<T> collection, C closure)

Outdated. starting with 4.1, use IterableUtils.forEach(Iterable, Closure)instead of

Performs the specified closure on each element of the collection. If the input collection or closure is NULL, no change occurs.


you can use IterableUtils::forEach(Closure c)

Applies a closure to each element of the provided iteration.

0
source

All Articles