0

I have a sorted ArrayList A based on substring(1,4). I want to add all the Strings with the same substring(1,4) to a new ArrayList.

ArrayList A: x111abc, x111acb, x111bca, x222abc, x222cba, x333abc, x333bca, x444abc

So I want x111abc, x111acb, x111bca all in one ArrayList, x222abc, x222cba in a different ArrayList, x333abc, x333bca in a different ArrayList, and x444abc in a different ArrayList.

I've tried this and a while loop (instead of the if statement) similar to this, but I know that it's going to create an error once I get to the last item in A. I feel like there has to be an easier way, but I can't figure it out.

for (int i = 0; i < A.size(); i++) {
    if (A.get(i).substring(1, 4).equals(A.get(i).substring(1, 4))) {
        //add it to an ArrayList
    }
}
0

3 Answers 3

1

You can use a stream with groupingBy():

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import static java.util.stream.Collectors.groupingBy;

public class Test {
    public static void main(String[] args) {
        List<String> list = Arrays
                .asList("x111abc", "x111acb", "x111bca", "x222abc",
                        "x222cba", "x333abc", "x333bca", "x444abc");

        Collection<List<String>> grouped = list.stream()
                .collect(groupingBy(s -> s.substring(1, 4)))
                .values();

        System.out.println(grouped);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

How would I be able to loop through each group to perform basic operations without knowing specifically what substring(1,4) is? I have looked up examples, but everything I have found the developer knows that 111 is in the list for example. I have a large dataset and everything is already sorted by substring(1,4), but now I just need to loop through each group to perform operations. Thanks in advance!
0

If you want to use your logic then it will be like

String prevStr = origList.get(0).substring(1,4);
for (int i = 0; i < A.size(); i++) {
  if(prevStr.equals(origList.get(i).substring(1,4)) {
    //add it to an ArrayList
  }else{
    //create new list
  }
  prevStr = origList.get(i).substring(1,4);
}

Comments

0

You can collect a map of lists from this list:

List<String> listA = List.of("x111abc", "x111acb", "x111bca",
        "x222abc", "x222cba", "x333abc", "x333bca", "x444abc");
Map<String, List<String>> map = listA.stream().collect(Collectors.toMap(
        // key - substring with a number
        e -> e.substring(1, 4),
        // value - list of strings
        e -> new ArrayList<>(List.of(e)),
        // merge values
        (l1, l2) -> {
            l1.addAll(l2);
            return l1;
        },
        // sort by numbers
        () -> new TreeMap<String, List<String>>(Comparator.naturalOrder())));
// output
map.forEach((k, v) -> System.out.println(k + "=" + v));
//111=[x111abc, x111acb, x111bca]
//222=[x222abc, x222cba]
//333=[x333abc, x333bca]
//444=[x444abc]

See also: Split an array based on regex

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.