1

I have a Java list, I need to split the list based on the index values passed from a property file.

Example :

ArrayList<String> sampleList = new ArrayList<String>();
sampleList.add("1");
sampleList.add("2");
sampleList.add("3");
sampleList.add("4");
sampleList.add("5");
sampleList.add("6");
sampleList.add("7");
sampleList.add("8");
sampleList.add("9");
sampleList.add("10");

Let's say for the first time, the index passed from property file are 3,5. So my output should be

For example :

System.out.println(sampleList.subList(0,3)+" "+sampleList.get(3)+" "+sampleList.subList(4,5)+" "+sampleList.get(5)+" "+sampleList.subList(6,sampleList.size()));

[1,2,3] 4 [5] 6 [7,8,9,10]

Next time if the indexes passed are 3,5,8 and the output should be

[1,2,3] 4 [5] 6 [7,8] 9 [10]

I have tried to sublist the list and pass on the indexes manually. It does gives output but it's not efficient.

Please advice how this can be done dynamically.TIA

7
  • 1
    It's unclear to me how the given indices correspond to the expected output. Also what exactly does "no efficient" mean? Sublist should be rather fast as it doesn't copy anything. Commented Sep 18, 2017 at 7:15
  • Please show the actual code you have tried and explain how it is not efficient Commented Sep 18, 2017 at 7:15
  • Can you please explain how a split 3,5 and 3,5,8 result in the respective outputs? Commented Sep 18, 2017 at 7:15
  • Please find the sysout command I have added . This is how I m trying to split the list manually to get the result Commented Sep 18, 2017 at 7:25
  • Your example does not give you the output that you said. It outputs this: [1, 2, 3] 4 [5] 6 [7, 8, 9, 10] . Can you describe in words what the parameters 3 and 5 should do? Commented Sep 18, 2017 at 7:30

1 Answer 1

3

Made a simple solution, indexes are stored in the array, and list is not truly 'split' - I've used a subList, which is only a view. But it shows an idea:

    int[] indexes = {3, 5, 8};

    List<String> sampleList = new ArrayList<>();
    sampleList.add("1");
    sampleList.add("2");
    sampleList.add("3");
    sampleList.add("4");
    sampleList.add("5");
    sampleList.add("6");
    sampleList.add("7");
    sampleList.add("8");
    sampleList.add("9");
    sampleList.add("10");        

    List<List<String>> result = new ArrayList<>();

    int lastIdx = 0;
    for (int idx: indexes) {
        if (lastIdx < idx) {
            //then this subList is not empty
            result.add(sampleList.subList(lastIdx, idx));            
        }
        //adding 'idx' element anyway
        result.add(sampleList.subList(idx, idx + 1));
        lastIdx = idx + 1;
    }
    result.add(sampleList.subList(lastIdx, sampleList.size()));

    System.out.println(result);

Output:

[[1, 2, 3], [4], [5], [6], [7, 8], [9], [10]]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.