tags:

views:

8566

answers:

13

Conditions: do not modifiy the original lists; JDK only, no external libraries. Bonus points for a one-liner or a JDK 1.3 version.

Is there a simpler way than:

List<String> newList = new ArrayList<String>();
newList.addAll(listOne);
newList.addAll(listTwo);
A: 

Slightly simpler:

List newList = new ArrayList(listOne); newList.addAll(listTwo);

Tim
+14  A: 

Off the top of my head, I can shorten it by one line:

List<String> newList = new ArrayList<String>(listOne);
newList.addAll(listTwo);
AdamC
+4  A: 

A little shorter would be:

List<String> newList = new ArrayList<String>(listOne);
newList.addAll(listTwo);
Jorn
A: 

Perhaps I'm just too used to other languages... but, any chance this would work?

List newList = (new ArrayList(listOne)).addAll(listTwo);


Then, how about this? ;)

(List newList = new ArrayList(listOne)).addAll(listTwo);

Jonathan Lonowski
no, this won't work. addAll returns a boolean, not the new list.
AdamC
hehe - nope. nice try though;)
AdamC
+9  A: 

Probably not simpler, but intriguing and ugly:

    List<String> newList = new ArrayList<String>() { { addAll(listOne); addAll(listTwo); } };

Don't use it in production code... ;)

volley
Nice use of a anonymous class
Dave Cheney
*shudder* - fun though
Draemon
Irresponsible use of annonymous class. As the OP said - don't use it in production code.
ddimitrov
Ugly and evil, just as almost any use of double brace initialization.It ís shorter, though ;)
Jorn
+1  A: 

You can do a oneliner if the target list is predeclared.

(newList = new ArrayList<String>(list1)).addAll(list2);
deterb
so, in other words, a 2 liner...
AdamC
A: 

You could do it with a static import and a helper class

nb the generification of this class could probably be improved

public class Lists {

   private Lists() { } // can't be instantiated

   public static List join(List... lists) {
      List result = new ArrayList();
      for(List list : lists) {
         result.addAll(list);
      }
      return results;
   }

}

Then you can do things like


  import static Lists.join;
  List result = join(list1, list2, list3, list4);

Dave Cheney
For generification, change first line of method into this (the rest of the code should follow ;)): public static <T> List<T> join(List<? extends T>... lists)
volley
volley, that wont compile. Say no to arrays (of references)!
Tom Hawtin - tackline
A: 

I can't improve on the two-liner in the general case without introducing your own utility method, but if you do have lists of Strings and you're willing to assume those Strings don't contain commas, you can pull this long one-liner:

List<String> newList = new ArrayList<String>(Arrays.asList((listOne.toString().subString(1, listOne.length() - 1) + ", " + listTwo.toString().subString(1, listTwo.length() - 1)).split(", ")));

If you drop the generics, this should be JDK 1.4 compliant (though I haven't tested that). Also not recommended for production code ;-)

Dov Wasserman
+3  A: 

I'm not claiming that it's simple, but you mentioned bonus for one-liners ;-)

Collection mergedList = Collections.list(new sun.misc.CompoundEnumeration(new Enuleration[] {
    new Vector(list1).elements(),
    new Vector(list2).elements(),
    ...
}))
ddimitrov
A: 

Use a Helper class.

I suggest:

    public static <E> Collection<E> addAll(Collection<E> dest, Collection<? extends E>... src) {
     for(Collection<? extends E> c : src) {
      dest.addAll(c);
     }

     return dest;
    }

    public static void main(String[] args) {
     System.out.println(addAll(new ArrayList<Object>(), Arrays.asList(1,2,3), Arrays.asList("a", "b", "c")));

     // does not compile
     // System.out.println(addAll(new ArrayList<Integer>(), Arrays.asList(1,2,3), Arrays.asList("a", "b", "c")));

     System.out.println(addAll(new ArrayList<Integer>(), Arrays.asList(1,2,3), Arrays.asList(4, 5, 6)));
    }
alex
+2  A: 
List<?> newList = ListUtils.combine(list1, list2);

Oh, you have to implement the combine method :-)

oxbow_lakes
A: 

it is ok for you to try to make it one liner, but it will not be ok for production and other purposes or people to understand your code after it's off your hands. the clearer the better

Carlos Sanchez
A: 

hmmm it exists ListUtils.union(list1,list2); works cool!

Guillermo