2

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?

6
  • What kind of object is dupMap? Commented Aug 29, 2012 at 18:52
  • 1
    split() is a memeber of the String class. You can iterate through the Strings in your list and split() each one then put the results into your two separate lists. Commented Aug 29, 2012 at 18:53
  • 1
    @RoddyoftheFrozenPeas First, your name is kind of awesome. Second, I edited my post to have the information. It seems I forgot it. Commented Aug 29, 2012 at 18:55
  • any reason why parsed2 and some others are of type ArrayList and not List? Commented Aug 29, 2012 at 18:59
  • @Code-Guru I am sorry, do you mind explaining further? I am not sure I understand. Do you mean that I can say something like this in a loop iterating x: something = uniqueList.get(x); splits2 = something.split(delim2); ? Commented Aug 29, 2012 at 19:00

2 Answers 2

5

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.

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

12 Comments

This is more complicated than necessary. Using String.split() can vastly simplify this code.
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.
@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.
Of course, at the end of this discussion, we have basically spoon-fed the OP the answer ;-(
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!
|
0

From what I understand of your question, I would proceed as follows:

for each String in uniqueList
    parse the string into a character and two integers (probably using a single call to [String.split()](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String, int))
    insert the first integer into an List
    insert the second integer into another List

This is in pseudocode. Translating into Java is left as an exercise to the reader.

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.