Lavish Khetan
Lavish Khetan

Reputation: 59

How to split a String in Java and store it in different list

I have stored these comma separated String in an ArrayList.

[<b>SELL 512<\/b> lots of <b>[email protected]<\/b>, <b>BUY 513<\/b> lots of <b>[email protected]<\/b>]

I want to remove all the HTML Tags and then further I want to store them in different array List.

Say for eg I want my output something like this.

 List3: [SELL, BUY]
 List4: [512, 513]
 List5: [xyz, abc]
 List6: [112, 113]

List<String> List1 = new ArrayList<>();
List<String> List2 = new ArrayList<>();
List<List<String>> Tuples = new ArrayList<List<String>>();
List<String> List3 = new ArrayList<>();
List<String> List4 = new ArrayList<>();
List<String> List5 = new ArrayList<>();
List<String> List6 = new ArrayList<>();

 for (int i = 0; i < List1.size(); i++) {
       String var = List1.get(i).trim();
       for (String x : var.split("\\<.*?\\>|\\@|\\,*$|\\.", 7)) {
           List2.add(x);
       }
       Tuples.add(List2);
   }
   System.out.println(Tuples);

   for (int i = 0; i < Tuples.size(); i++) {
       List3.add(Tuples.get(i).get(1).split("\\s")[0].replaceAll("SELL","0").replaceAll("BUY","0"));
       List4.add(Tuples.get(i).get(1).split("\\s")[1]);
       List5.add(Tuples.get(i).get(3));
       List6.add(Tuples.get(i).get(4));
   }
   System.out.println(List3);
   System.out.println(List4);
   System.out.println(List5);
   System.out.println(List6);

}

But this fetches me output something like this:

 List3: [SELL, SELL]
 List4: [512, 512]
 List5: [xyz, xyz]
 List6: [112, 112]

Upvotes: 0

Views: 368

Answers (2)

Allan
Allan

Reputation: 12438

I think you are doing it in a way too complicated way, just do:

    List<String> inputList = new ArrayList<String>();
    inputList.add("<b>SELL 512</b> lots of <b>[email protected]</b>");
    inputList.add("<b>BUY 513</b> lots of <b>[email protected]</b>");

    System.out.println("inputList: "+inputList);


    List<List<String>> Tuples = new ArrayList<List<String>>();
    List<String> list3 = new ArrayList<String>();
    List<String> list4 = new ArrayList<String>();
    List<String> list5 = new ArrayList<String>();
    List<String> list6 = new ArrayList<String>();

     for (int i = 0; i < inputList.size(); i++) {
           String var = inputList.get(i).trim();
           String[] splitArr=var.split("</b>|<b>|\\d*@"); //remove the \\d* if you expect to have xyz18 in output instead of x

           list3.add((splitArr[1].split("\\s"))[0]);
           list4.add((splitArr[1].split("\\s"))[1]);
           list5.add(splitArr[3]);
           list6.add(splitArr[4]);

           Tuples.add(Arrays.asList(var.replaceAll("</b>|<b>", "")));

       }
       System.out.println("Tuples: "+Tuples);
       System.out.println("list3: "+list3);
       System.out.println("list4: "+list4);
       System.out.println("list5: "+list5);
       System.out.println("list6: "+list6);

OUTPUT:

inputList: [<b>SELL 512</b> lots of <b>[email protected]</b>, <b>BUY 513</b> lots of <b>[email protected]</b>]
Tuples: [[SELL 512 lots of [email protected]], [BUY 513 lots of [email protected]]]
list3: [SELL, BUY]
list4: [512, 513]
list5: [xyz, abc]
list6: [112.00, 113.00]

Upvotes: 1

k_ssb
k_ssb

Reputation: 6252

If you inspect the output of System.out.println(Tuples); you'll notice a problem:

Tuples.add(List2) does not make a copy of List2. You're storing multiple references to the same List2 in Tuples, and appending more data to the same List2. So you end up with a very long list (i.e., List2) containing all tuples concatenated, and that list replicated many times inside Tuples.

Instead of

Tuples.add(List2);

try this instead:

Tuples.add(new ArrayList<>(List2));
List2.clear();

This will make elements of Tuples reference different lists.

Upvotes: 2

Related Questions