1

If I know that my ArrayList<ArrayList<String>> will only contain one element (one list inside a list) how can I transform it into a 1D ArrayList. Such that from [["A","B"]] I get ["A","B"]?

Timmy333
  • 25
  • 5
  • 1
    `listOfLists.stream().flatMap(List::stream).collect(Collectors.toList());` - Works even if there are mutliple lists. [Ideone demo](https://ideone.com/MdFbNA) – Turing85 Nov 21 '21 at 14:32
  • 2
    Your 2D list contains 1D list which means you already have *some* 1D list (which is inside). Why would you want to convert your 2D list into 1D? Just `2dList.get(indexOf1DList)`. – Pshemo Nov 21 '21 at 14:32
  • @Pshemo because I don't want to create a new variable – Timmy333 Nov 21 '21 at 15:30
  • @Timmy333 why? What is the disadvantage you which you want to avoid? Anyway you can't change type of variable, so you can't change `ArrayList> variable` into `ArrayList variable` so you don't have many options (maybe raw types but that would be medicine which is worse than a disease - see: [What is a raw type and why shouldn't we use it?](https://stackoverflow.com/q/2770321)) – Pshemo Nov 21 '21 at 15:32
  • Also I never suggested creating new variable. You can always use `2dList.get(indexOf1DList)` in all places directly instead of storing it in variable. – Pshemo Nov 21 '21 at 15:38
  • Ohh. I get you know, one more complication is that I also have to remove the first string. Such that from `[["A","B","C"]]` I want `["B","C"]`, is it possible to do `2dList.get(0).remove(0)` ? – Timmy333 Nov 21 '21 at 15:51
  • Yes. `2dList.get(0)` should return inner list which holds `["A","B","C"]` so you can call on it `remove(0)` to remove `"A"`. – Pshemo Nov 21 '21 at 15:59

2 Answers2

1

If we run under Java 8+, we will be able to use the java.stream API to solve the problem:

listOfLists.stream() // transform the list into a stream, i.e. each element in 
                     // the list (in our case: List<String>) will appear in the 
                     // Stream.

    .flatMap(List::stream) // flatten the stream: for each element in the stream 
                           // (List<String> in our case), stream those entries 
                           // (Strings), construct a new stream over all entries 
                           // (Strings) of all List<String>s in the stream

    .collect(Collectors.toList()); // returns all `String`s in a new List<String>

Ideone demo

Turing85
  • 18,217
  • 7
  • 33
  • 58
  • I tried to test this using this code: `ArrayList a = new ArrayList<>(Arrays.asList("1", "2","3")); ArrayList b = new ArrayList<>(Arrays.asList(a)); System.out.println(b); b.stream().flatMap(List::stream).collect(Collectors.toList()); System.out.println(b);` but it doesn't seem to be working, I get `[["1","2","3"]]` both times – Timmy333 Nov 21 '21 at 15:28
  • 1
    Have you taken a look at the [Ideone demo](https://ideone.com/MdFbNA)? We have to assign the result of the streaming operations to a variable: `List result = listOfList.stream()...`. – Turing85 Nov 21 '21 at 15:34
0

I think you just need to get the first element with the get method. Then you put it in a new variable that you call oneD for example.

wil123
  • 17
  • 2