-2

How to convert a nested list into a single list in java?

List list = new ArrayList();
List newList = new ArrayList();
List<Integer> list1 = new ArrayList<Integer>();

list1.add(2);

list1.add(4);

list.add(5);
list.addAll(list1);
list.add(6);

How can i add the elements of list to newList so that when i print newList

it prints

[5,2,4,6]
rolfl
  • 17,539
  • 7
  • 42
  • 76
  • whats the use case here? – Tirath Oct 01 '14 at 16:45
  • 1
    Don't use raw-types! – Elliott Frisch Oct 01 '14 at 16:46
  • Are you trying to figure out how to [add all](http://docs.oracle.com/javase/7/docs/api/java/util/List.html#addAll(int,%20java.util.Collection)) of the elements into a specific point in the list? – Floegipoky Oct 01 '14 at 16:50
  • By the way, there is no nested list in the code you've included. A nested list would be a list of lists eg `List> nestedList;` – Floegipoky Oct 01 '14 at 16:53
  • I rolled back your edit.... you can't go and change the core concepts of your question after there are 3 answers to your original question. – rolfl Oct 01 '14 at 17:00
  • yeah i know but if you see the code you can think what i asked ?okay – Shashank Pathak Oct 01 '14 at 17:01
  • If you the guys help me i would be very thank full – Shashank Pathak Oct 01 '14 at 17:04
  • as I said before, that you should ask a different question, if you have a different question. (but first search for duplicates), like [this one](http://stackoverflow.com/q/20144826/1305253) and [this one](http://stackoverflow.com/q/7431006/1305253) – rolfl Oct 01 '14 at 17:05

5 Answers5

2

Unless there is something you're not telling us, it is simply a repeat of what you have already done:

newList.addAll(list);

As has been said in the comments though, you really should be using full generic typing for your list variables:

 List<Integer> list = new ArrayList<>();
 List<Integer> newList = new ArrayList<>();
 List<Integer> list1 = new ArrayList<>();
rolfl
  • 17,539
  • 7
  • 42
  • 76
0

You shouldn't be using raw types, and you can call newList.addAll(list); -

public static void main(String[] args) {
    List<Integer> list = new ArrayList<>(); // <-- diamond operator, Java 7 and up.
    List<Integer> newList = new ArrayList<>();
    List<Integer> list1 = new ArrayList<>();
    list1.add(2);
    list1.add(4);
    list.add(5);
    list.addAll(list1);
    list.add(6);
    newList.addAll(list); // <-- here
    System.out.println(newList);
}

Output is the requested

[5, 2, 4, 6]
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

You already used a possible way with

list.addAll(list1)

Another way would be walking through the list resp. using it's iterator to fetch and put all items into the newList.

Have a look at the API for more detailed info: http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#addAll(java.util.Collection)

0
  for(int i=0;i<list.size();i++){
      if(list.get(i) instanceof Integer){ 
           newlist.add(list.get(i));
      }else{
           newlist.addAll((ArrayList)list.get(i));
      }
  }
aphex
  • 3,372
  • 2
  • 28
  • 56
0

The list that has to hold another list should be defined properly, rest working will be same. Refer below:

import java.util.List;
import java.util.ArrayList;
public class demo2 {
    public static void main(String[] args) {
        List<List<Integer>> nestedList = new ArrayList<>();
        List<Integer> list = new ArrayList<>();
        list.add(1);
        nestedList.add(list);
        System.out.println(nestedList);
    }
}