1

I'm trying to split each object in the ArrayList because a lot of lines contains comma(","). Each object contains a item and value(but not all objects contains value):

   Access control enable                             ,disabled
   Access policy prototyping                         ,enabled
   Access user group                                 
   Implicit roles access policy                      
   World access policy                               ,disabled

This is my piece of code:

            List<String> CEP = new ArrayList<String>();
            List<String> CEV = new ArrayList<String>();
            for (String str : CE) {
                for (String s : str.split(",")) {
                    CEP.add(s.trim());
                }
            }
  • "CE" is the main ArrayList.
  • "CEP" is the ArrayList that should contain only the "items"(what before comma).
  • "CEV" is the ArrayList that should contain all "values"

My piece of code only split it by comma to the same ArrayList and another problem is how to see an object doesn't have "value" and add blank to the values ArrayList.

Thanks guys.

4 Answers 4

2

Maybe using a CSV parser like super-csv is a good option. It will really pay off when your comma separated list starts to become more diverse.

Univocity provides a benchmark of CSV parsers. It says that univocity-parsers is fast, which is no surprise. You could give it a try.

Sign up to request clarification or add additional context in comments.

Comments

1

Using an array and checking its length allows you to handle the missing values:

for (String str : CE) {
    String[] a = str.split(",");
    CEP.add(a[0].trim());
    if(a.length > 1) {
        CEV.add(a[1].trim());
    } else {
        CEV.add(null); //just check that this is OK
    }
}

Just make sure that the value being added to CEV for missing values (null in the above code) is as required.

Comments

0

You could split and check the length of the array afterwards:

public static void main(String[] args) {
    List<String> ce = new ArrayList<String>();

    ce.add("Access control enable                             ,disabled");
    ce.add("Access policy prototyping                         ,enabled");
    ce.add("Access user group                                 ");
    ce.add("Implicit roles access policy                      ");
    ce.add("World access policy                               ,disabled");

    Map<String, String> cepCev = new HashMap<String, String>();

    ce.forEach((String line) -> {
        String[] splitLine = line.split(",");
        if (splitLine.length > 1) {
            cepCev.put(splitLine[0].trim(), splitLine[1].trim());
        } else {
            cepCev.put(splitLine[0].trim(), "not set");
        }
    });

    cepCev.forEach((String key, String value) -> {
        System.out.println(key + ": " + value);
    });
}

Comments

0

You can do it using java 8 and steam in simple way,

    List<String> CEP = new ArrayList<String>();
    CE.stream().forEach(ce->CEP.addAll(Arrays.asList(ce.split(","))));
    List<String>  CEV= CEP.stream().map(String::trim).filter(s -> s.length()>0).collect(Collectors.toList());

    System.out.println(CEP);
    System.out.println(CEV);

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.