Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better way to convert an List<MyDataType> to List<String>

Tags:

java

I am wondering if there isn't a better way to convert whole Lists or Collections as the way I show in the following code example:

public static List<String> getAllNames(List<Account> allAccounts) {
        List<String> names = new ArrayList<String>(allAccounts.size());
        for (Account account : allAccounts) {
            names.add(account.getName());
        }
        return names;
    }

Every time I produce a method like this, I start thinking, isn't there a better way? My first thought would be to create maybe a solution with some generics and reflections, but this seems maybe a bit over sized and maybe a bit to slow when it comes to performance?

like image 798
Robin Avatar asked Dec 04 '12 14:12

Robin


People also ask

How do I turn a list element into a string?

To convert a list to a string, use Python List Comprehension and the join() function. The list comprehension will traverse the elements one by one, and the join() method will concatenate the list's elements into a new string and return it as output.

Which method can be used to convert a string to a list?

To do this we use the split() method in string. The split method is used to split the strings and store them in the list.

How do I print a list like a string?

Convert a list to a string for display : If it is a list of strings we can simply join them using join() function, but if the list contains integers then convert it into string and then use join() function to join them to a string and print the string.

How do I convert a list to a string in one line?

To convert a list to a string in one line, use either of the three methods: Use the ''. join(list) method to glue together all list elements to a single string. Use the list comprehension method [str(x) for x in lst] to convert all list elements to type string.


3 Answers

Take a look at Google's Guava library

Something like this should do it

final List<String> names = Lists.transform(myObjs, new Function<MyObject, String>() {
    public String apply(final MyObject input) {
        return input.getName();
    }
});
like image 112
RNJ Avatar answered Oct 20 '22 07:10

RNJ


With Guava, there is a more functional approach:

return FluentIterable.from(allAccounts).transform(new Function<Account,String>(){
    public String apply(Account account){return account.getName();}
}).toImmutableList()

But that essentially does the same thing, of course.

BTW: the difference between this answer and RNJ's is that in my case the list will be created once, while in the other answer it's a live view. Both versions are valid, but for different scenarios.

like image 42
Sean Patrick Floyd Avatar answered Oct 20 '22 08:10

Sean Patrick Floyd


I actually have exactly this kind of method in my personal library.

public static <TSource,TTarget> List<TTarget> castList(List<TSource> sourceList)
{
    List<TTarget> targetList = new ArrayList<TTarget>(sourceList.size());
    for (TSource t : sourceList) {
        //This will throw a ClassCastException if the types are not compatible
        //Be carefull
        targetList.add((TTarget)t);
    }
    return targetList;
}

Usage is very simple because the compiler infers the type for TTarget.

List<Object> objects = new ArrayList<Object>();
objects.add("One");
objects.add("Two");

List<String> strings = castList(objects);

Regarding the performance: I think using generics is no problem here. But the need to copy the whole array is another story.

like image 2
Bluuu Avatar answered Oct 20 '22 08:10

Bluuu