Parse from List<String> Java

18,992

If you know that all of your data is in the form [something]:[num]-[num], you can use a regular expression like this:

Pattern p = Pattern.compile("^([^:]*):([^-]*)-([^-]*)$");

// I assume this holds all the values:
List<String> uniqueList = new ArrayList<String>(dupMap.values()); 

for (String src : uniqueList) {
    Matcher m = p.matcher(src); 
    if( m.find() && m.groupCount() >= 3) {
        String firstValue = m.group(1); // value to left of :
        String secondValue = m.group(2); // value between : and -
        String thirdValue = m.group(3); // value after -

        // assign to arraylists here
    }
}

I didn't actually put the code in to add to the specific ArrayLists because I couldn't quite tell from your code which ArrayList was supposed to hold which value.

Edit

Per Code-Guru's comment, an implementation using String.split() would go something like this:

String pattern = "[:\\-]";

// I assume this holds all the values:
List<String> uniqueList = new ArrayList<String>(dupMap.values()); 

for (String src : uniqueList) {
    String[] parts = src.split(pattern);
    if (parts.length == 3) {
        String firstValue = parts[1]; // value to left of :
        String secondValue = parts[2]; // value between : and -
        String thirdValue = parts[3]; // value after -

        // assign to arraylists here
    }
}

Both approaches are pretty much the same in terms of efficiency.

Share:
18,992
Stephopolis
Author by

Stephopolis

Merge keep

Updated on June 04, 2022

Comments

  • Stephopolis
    Stephopolis almost 2 years

    I am attempting to parse the value of the elements in a List declared as thus:

     List<String> uniqueList = new ArrayList<String>(dupMap.values());
    

    The values are such as this:

    a:1-2
    b:3-5
    

    but I want one ArrayList with the first number (i.e. 1, 3) and another with the second (i.e. 2, 5). I have this worked out... Sorta:

    String delims= "\t"; String delim2= ":"; String delim3= "-";
    String splits2[]; String splits3[]; String splits4[];
    Map<String,String> dupMap = new TreeMap<String, String>();
    List<String> uniqueList = new ArrayList<String>(dupMap.values());
    ArrayList<String> parsed2 = new ArrayList<String>();
    ArrayList<String> parsed3 = new ArrayList<String>();
    ArrayList<String> parsed3two= new ArrayList<String>();
    double uniques = uniqueList.size();
    for(int a=0;a<uniques;a++){
        //this doesn't work like it would for an ArrayList
        splits2 = uniqueList.split(delim2) ;
        parsed2.add(splits2[1]);
        for(int q=0; q<splits2.length; q++){
            String change2 = splits2[q];
            if(change2.length()>2){
               splits3 = change2.split(delim3);
               parsed3.add(splits3[0]);
               String change3=splits3[q];
               if (change3.length()>2){
                   splits4 = change3.split(delims);
                   parsed3two.add(splits4[0]);
               }
            }
         }
      }
    

    uniqueList.split does not work however and I don't know if there is a similar function for List. Is there any suggestions?

  • Code-Apprentice
    Code-Apprentice over 11 years
    This is more complicated than necessary. Using String.split() can vastly simplify this code.
  • Roddy of the Frozen Peas
    Roddy of the Frozen Peas over 11 years
    split() would require at minimum two regex calls (one for each split().) If the number of strings is large enough, this is more efficient. Unless it was .split("[:\\-]*"), I suppose.
  • Code-Apprentice
    Code-Apprentice over 11 years
    @Sujay In this case, there is no need for a series of splits. One call with the correct regex for the delimiters would split everything all at once.
  • Roddy of the Frozen Peas
    Roddy of the Frozen Peas over 11 years
    @Code-Guru -- I've updated the answer with an example for how to use split() instead of straight up regexes.
  • Sujay
    Sujay over 11 years
    @Code-Guru: I agree. I didn't think it through :(
  • Code-Apprentice
    Code-Apprentice over 11 years
    Do you need the * in the regex? I think the formatting in this case only specifies a single-character delimiter.
  • Roddy of the Frozen Peas
    Roddy of the Frozen Peas over 11 years
    Fat finger typo. Fixed. Thanks.
  • Roddy of the Frozen Peas
    Roddy of the Frozen Peas over 11 years
    Unless you mean the one in the first code example, which I presume is necessary. But since I don't know anything about the input data, including whether ":-" is a valid input, so I can't be 100% certain.
  • Code-Apprentice
    Code-Apprentice over 11 years
    Of course, at the end of this discussion, we have basically spoon-fed the OP the answer ;-(
  • Code-Apprentice
    Code-Apprentice over 11 years
    Yes, I meant the delimiter pattern in the second version using split().
  • Roddy of the Frozen Peas
    Roddy of the Frozen Peas over 11 years
    Well, it wasn't marked Homework, so we can hope the OP took the time to read and understand the code instead of just copy-pasting it.
  • Stephopolis
    Stephopolis over 11 years
    Thanks much! I appreciate that you included both ways! I chose the first way however. I always forget about regex even though it is awesome. Also, I promise it isn't homework. I am legit just trying to learn!