2

I have been trying to convert string of ArrayList<ArrayList<String>> to ArrayList<ArrayList<Integer>>

Here is my codes that I tried to build.

public void convertString (ArrayList<ArrayList<String>> templist) {
    readList = new ArrayList<ArrayList<Integer>> ();
    for (ArrayList<String> t : templist) {
        readList.add(Integer.parseInt(t));
    }
    return readList;

Need some advice on how to convert it. Thanks a lot.

1
  • Also, keep in mind when converting a String to an Integer "you'll notice the "catch" is that this function can throw a NumberFormatException" stackoverflow.com/a/5585800/4977223 Commented Jan 2, 2020 at 16:50

4 Answers 4

5

You can achieve this using Stream API:

ArrayList<ArrayList<String>> list = ...

List<List<Integer>> result = list.stream()
    .map(l -> l.stream().map(Integer::parseInt).collect(Collectors.toList()))
    .collect(Collectors.toList());

Or if you really need ArrayList and not List:

ArrayList<ArrayList<String>> list = ...

ArrayList<ArrayList<Integer>> result = list.stream()
  .map(l -> l.stream().map(Integer::parseInt).collect(Collectors.toCollection(ArrayList::new)))
  .collect(Collectors.toCollection(ArrayList::new));
Sign up to request clarification or add additional context in comments.

3 Comments

Assuming you are using Java 8 or later.
Java 8 is long dead. If someone wants a historical answer, it's reasonable to mention the older version of Java in the question itself
My information may be out-of-date but I thought Android was stuck on Java 6,
3

You have nested lists so you will need a nested for-loop.

for (ArrayList<String> t: tempList) {
    ArrayList<Integer> a = new ArrayList<>();
    for (String s: t) {
        a.add(Integer.parseInt(s));
    }
    readList.add(a);
}

Comments

3

If you are using Java-8 you can use :

public ArrayList<ArrayList<Integer>> convertString(ArrayList<ArrayList<String>> templist) {
    return templist.stream()
            .map(l -> l.stream()
                    .map(Integer::valueOf)
                    .collect(Collectors.toCollection(ArrayList::new)))
            .collect(Collectors.toCollection(ArrayList::new));
}

I would suggest to use List instead of ArrayList :

public List<List<Integer>> convertString(List<List<String>> templist) {
    return templist.stream()
            .map(l -> l.stream()
                    .map(Integer::valueOf)
                    .collect(Collectors.toList()))
            .collect(Collectors.toList());
}

Comments

2

With java-8, you do it as below,

templist.stream()
        .map(l->l.stream().map(Integer::valueOf).collect(Collectors.toList()))
        .collect(Collectors.toList());

This would be List<List<Integer>. If you want ArrayList you can use,

Collectors.toCollection(ArrayList::new)

1 Comment

Not quite: this gives you a List<List<Integer>>.

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.