2

Is there any Utility function in Java to convert List<Object[]> to List<Object>

5
  • 3
    describe you question properly Commented Dec 22, 2014 at 14:23
  • The question is ambiguous: what should happen if an array contains two or more elements? Please provide an example. Commented Dec 22, 2014 at 14:24
  • What would you expect as a result from that function? Commented Dec 22, 2014 at 14:24
  • No, there is no such method available. Either iterate over the list and each array and add the entries into a new list or use one of the fancy Java 8 Streams. Commented Dec 22, 2014 at 14:25
  • @rams, feel free to review the answers and accept the one the best answers your question. Commented Jan 23, 2015 at 10:40

5 Answers 5

4

No, there's no method that does this directly for you. You can write a nested for loop or use the flatMap of the stream API as follows:

List<Object> flat = objArrs.stream()
                           .flatMap(Stream::of)
                           .collect(Collectors.toList());
Sign up to request clarification or add additional context in comments.

Comments

2

You can easily write one yourself:

public static<T> List<T> append (List<T[]> input) {
    List<T> res = new ArrayList<T>();
    for(T[] subarr : input) {
        if(subarr != null) {
            int n = subarr.length;
            for(int i = 0; i < n; i++) {
                res.add(subarr[i]);
            }
        }
    }
    return res;
}

The function appends the different arrays and null arrays are ignored, null elements are however not. Thus if the input is [null,[a,b],[null,null],[c,null,d],null]. The output is [a,b,null,null,c,null,d].

4 Comments

new List<T>()? Since when can you instantiate an interface? But besides that, I like that you're checking for null.
@Tom: True, modified it. In C#, List<T> is a class :(. Sometimes one starts mixing up std-libs... :s
Why allow null arrays? Null arrays seem likely to indicate a bug earlier in the program...?
@LouisWasserman: null arrays are ignored. Simply because some people see them as zero-length arrays. It is of course arbitrary. But in case null shouldn't be allowed, one can simply remove the if clause.
1

No.

Why don't you just write the function yourself? It would probably be faster than asking this question and waiting for an answer.

Comments

1

In Java 8 you can do it with Streams :

List<Object[]> list = ...
List<Object> l = list.stream()
                     .flatMap(arr -> Stream.of(arr))
                     .collect(Collectors.toList());

Comments

1

As others have already said, there is no utility and creating one yourself wouldn't be hard, for example using old school for loops:

public List<Object> flatten( List<Object[]> source )
{
    // if ( source == null ) return null; // which check you use it up to you
    List<Object> result = new ArrayList<Object>();
    if ( source == null ) return result; // Personally I like this check

    for ( Object[] array: source )
    {
        if ( array == null ) continue; // skip nulls
        for ( Object object: array )
        {
            result.add(object);
        }
    }
    return result;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.