views:

577

answers:

5

I am iterating though a TreeSet and printing it out:

while (it.hasNext()) {
  System.out.println(it.next());
}

output:

after
explorers
giant
hoping
internet
into
.
.
.
virtual 
world

However, I would like to only print out those strings who's first character is within the range m-z. I have been playing around with java.util.regex, with no success:

String pattern = "[^m-z]";

Do I have the right idea here? Or is there a simpler way of doing this? All I want to do is make sure I only print out those Strings in the TreeSet who's first character is within the range m-z.

+1  A: 

seems like an homework but anyhow, the "[^m-z]" means NOT m-z

try putting the "^" outside the "[]"

Have a look at the Pattern class and BTW, try String.matches()

Julien Grenier
BTW, saua answer is the best since it is way more efficient (memory and performance wise)
Julien Grenier
+4  A: 

First of all, your regular expression is wrong. You want

"^[m-z]"

Second of all, you don't show the code you're using to do the matching.

Third: If you're willing to do something besides regular expressions and iteration, you should look into SortedSet.tailSet. That's probably what your teacher wants.

Paul Tomblin
It's not irrelevant. SortedSets are designed to support this sort of range quer with the sub-, head-, and tailSet methods.
erickson
@erickson, he explicitly said he's iterating through it. He didn't ask for help with sub-, head- and tailSet. Maybe that's what his teacher wanted, but I answered the question, not the implied question.
Paul Tomblin
+3  A: 

I don't know about regular expressions, but you can easily find those Elements that begin with a letter from m-z:

wordSet.tailSet("m", true);

Usually you would use subSet() with the lower bound and the next character from the upper bound, but since z is the last character that can't easily be done.

For getting the set from 'b' to 'y' you'd do

wordSet.subSet("b", true, "z", false);
Joachim Sauer
Definitely in the spirit of TreeSet.
erickson
If you say "Elements that being with letter from m-z", shouldn't you have said '.tailSet("m"'?
Paul Tomblin
Paul > there is a .headSet
Julien Grenier
Right, Paul, that was a typo, thanks
Joachim Sauer
+2  A: 

I agree this sounds suspiciously like homework, but here we go...

while (it.hasNext()) {
    String element = it.next();
    if (element.toLowerCase().matches("^[m-z].*")) {
        System.out.println(element);
    }
}
James Van Huis
+1  A: 

I know the original poster discussed using a regular expression, but maybe he had a problem to solve, decided to use regular expressions, and now has two problems to solve.

while (it.hasNext()) {
    String element = (String) it.next();
    char c = element.charAt(0);
    if (c >= 'm' && c <= 'z') {
        System.out.println(element);
    }
}

This seems to meet his requirement set out in bold in the original question, although it is possible his teacher dictated the use of regular expressions.

EDIT: I only bothered to read the full question just now. However, I still think it is valuable to understand that regular expressions aren't the only way to solve this problem.

Grant Wagner