0

What I want to do is to split an array of strings, when the first 6 characters in the string are zeroes ("000000") or when all the digits in the string are zeroes. Limiting to 6 characters won't be very dynamic.

I got this code, and it does what I want to achieve.

import java.util.*;
public class Main
{
    public static void main(String[] args) {
            ArrayList<String> unsplitted = new ArrayList<String>();
  
            unsplitted.add("000000: this_should_go_into_first_array");
            unsplitted.add("000234: something1");
            unsplitted.add("0000ff: something2");
            unsplitted.add("000111: something3");
            unsplitted.add("000051: something4");
            unsplitted.add("007543: something5");
            unsplitted.add("000000: and_this_should_go_into_second_array");
            unsplitted.add("005612: something7");
            unsplitted.add("005712: something8");
  
            System.out.println("Unsplitted list: "+ unsplitted);

            List<String> arrlist1 = unsplitted.subList(0, 6);
            List<String> arrlist2 = unsplitted.subList(6, unsplitted.size());
  
            System.out.println("Sublist of arrlist1: "+ arrlist1);
            System.out.println("Sublist of arrlist2: "+ arrlist2);
    }
}

Which prints out the wanted results

Sublist of arrlist1: [000000: this_should_go_into_first_array, 000234: something1, 0000ff: something2, 000111: something3, 000051: something4, 007543: somethi
ng5]                                                                                                                                                          
Sublist of arrlist2: [000000: and_this_should_go_into_second_array, 005612: something7, 005712: something8]

However, I don't know the indexes for the zeroes beforehand, so how can I achieve the same result by finding the zeroes dynamically?

2 Answers 2

1

You can simply iterate in your array and create "bucket" each time you detect your 000000 string :

ArrayList<String> unsplitted = new ArrayList<String>();

    unsplitted.add("000000: this_should_go_into_first_array");
    unsplitted.add("000234: something1");
    unsplitted.add("0000ff: something2");
    unsplitted.add("000111: something3");
    unsplitted.add("000051: something4");
    unsplitted.add("007543: something5");
    unsplitted.add("000000: and_this_should_go_into_second_array");
    unsplitted.add("005612: something7");
    unsplitted.add("005712: something8");

    List<List<String>> results = new ArrayList<>();
    unsplitted.forEach(w -> {
        if(w.startsWith("000000") || results.isEmpty()) {
            // no bucket or detect 000000
            List<String> bucket = new ArrayList<>();
            bucket.add(w);
            results.add(bucket);
        }
        else {
            // not contains 00000 put the value in the last bucket
            results.get(results.size() - 1).add(w);
        }
    });

    results.forEach(w -> {
        System.out.println("Sublist " + w);
    });

Is it the result that you expected ?

The result :

Sublist [000000: this_should_go_into_first_array, 000234: something1, 0000ff: something2, 000111: something3, 000051: something4, 007543: something5]
Sublist [000000: and_this_should_go_into_second_array, 005612: something7, 005712: something8]
Sign up to request clarification or add additional context in comments.

2 Comments

Question: Why do I need the results.get(results.size() - 1).add(w);? I tried with results.add(w) but of course it didn't work. Just curious why I need to get the indexes first, and then add w
Result is a list of sublist. The-1 is to get the latest sublist that keep your string.
0

The question is quite interesting. There are different way to implement this, but I am going to show you a solution where it can be applied with any length of the first part, which we can consider as a key.
As you said in your introduction, it wouldn't be dynamic if the check was limited to only 6 characters. Based on this, as an example, you can take the position of the character ':' as reference and apply a partitioning among the elements of the array.
Here is the solution I propose:

import java.util.*;
import java.util.function.*;
import java.util.stream.*;

public class Main
{
    public static void main(String[] args) {
        ArrayList<String> unsplitted = new ArrayList<String>();



        unsplitted.add("000000: this_should_go_into_first_array");
        unsplitted.add("000234: something1");
        unsplitted.add("0000ff: something2");
        unsplitted.add("000111: something3");
        unsplitted.add("000051: something4");
        unsplitted.add("007543: something5");
        unsplitted.add("000000: and_this_should_go_into_second_array");
        unsplitted.add("005612: something7");
        unsplitted.add("005712: something8");

        System.out.println("Non-split list: "+ unsplitted);

        Predicate<String> filter = (String s) -> {
            int indexOfCol = s.indexOf(":");

            return s.substring(0, indexOfCol).equals("0".repeat(indexOfCol));
        };

        Map<Boolean, List<String>> splitMap = unsplitted.stream()
                                                        .collect(Collectors.partitioningBy(filter));

        List<String> arrayZeroStart = splitMap.get(true);
        List<String> arrayNonZeroStart = splitMap.get(false);

        System.out.println("Sublist of arrayZeroStart: "+ arrayZeroStart);
        System.out.println("Sublist of arrayWithout: "+ arrayNonZeroStart);
    }
}

And here is the output:

Non-split list: [000000: this_should_go_into_first_array, 000234: something1, 0000ff: 

something2, 000111: something3, 000051: something4, 007543: something5, 000000: and_this_should_go_into_second_array, 005612: something7, 005712: something8]
Sublist of arrayZeroStart: [000000: this_should_go_into_first_array, 000000: and_this_should_go_into_second_array]
Sublist of arrayWithout: [000234: something1, 0000ff: something2, 000111: something3, 000051: something4, 007543: something5, 005612: something7, 005712: something8]

1 Comment

Btw, thank you for the attention and consideration, that's very appreciated!

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.