Home > Back-end >  How to "add parsed string that used filter" to an String ArrayList
How to "add parsed string that used filter" to an String ArrayList

Time:05-17

Right now I have collection of substrings that match the pattern. I want to add these filtered substrings to an arraylist.

Right now have this:

Pattern reg = Pattern.compile("\\*");
   
String result = reg.splitAsStream("A*B*C*D*E*F*G*A*B*C*D*E*F*A*B*C*D*E*F*G*H")
        .filter(role -> role.matches("A"))
        .map(String::trim)
        .collect(Collectors.joining(","));

System.out.println(result);

I want to add the printed out result which is all the A into a arraylist I need help with adding it to arraylist.

CodePudding user response:

If you need to add every matched result specifically into an ArrayList, then you could use the terminal operation collect(Collectors.toCollection(ArrayList::new)) and provide the ArrayList constructor as the factory method for the toCollection. This will specifically instantiate an ArrayList containing all your matched results.

Pattern reg = Pattern.compile("\\*");
ArrayList<String> listRes = reg.splitAsStream("A*B*C*D*E*F*G*A*B*C*D*E*F*A*B*C*D*E*F*G*H")
        .filter(role -> role.matches("A"))
        .map(String::trim)
        .collect(Collectors.toCollection(ArrayList::new));

EDIT

In case you also want to include the results that start with A, then you could use the following regex within the filter aggregate operation: A.*. The code will look like this:

Pattern reg = Pattern.compile("\\*");
ArrayList<String> listRes = reg.splitAsStream("A123*B*C*D*E*F*G*A222*A*B*C*A")
        .filter(role -> role.matches("A.*"))
        .map(String::trim)
        .collect(Collectors.toCollection(ArrayList::new));

CodePudding user response:

Currently, your application prints out the following.

A,A,A

If you are saying that you want the result to be placed into an ArrayList, then all you have to do is use the ArrayList() constructor that accepts a Collection, then simply turn your Stream into a Collection. Like this.

import java.util.ArrayList;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class SOQ_20220516_1
{

   public static void main(String[] args)
   {
   
      Pattern reg = Pattern.compile("\\*");
   
      var result = new ArrayList<>(reg.splitAsStream("A*B*C*D*E*F*G*A*B*C*D*E*F*A*B*C*D*E*F*G*H")
         .filter(role -> role.matches("A"))
         .map(String::trim)
         .toList());
   
      System.out.println(result);
   
   }

}

Of course, if you are willing to use a List instead of an ArrayList, then this becomes much simpler. Like this.

import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class SOQ_20220516_1
{

   public static void main(String[] args)
   {
   
      Pattern reg = Pattern.compile("\\*");
   
      var result = reg.splitAsStream("A*B*C*D*E*F*G*A*B*C*D*E*F*A*B*C*D*E*F*G*H")
         .filter(role -> role.matches("A"))
         .map(String::trim)
         .toList();
   
      System.out.println(result);
   
   }

}
  • Related