1

Say I have this class

    public class MovieCharacter {

    private String name;

    public String getName() { return name ; }
    public String setName(String name) { this.name = name; }
    }

I'm running a test and have a Set and I want to use a Lambda to comb through each object to see if it contains a desired String for the Name. If the name is found, a boolean is tripped to "true". After it's found, I don't want the boolean changed again.

Set<MovieCharacter> mySet // assume this Set has previously been created and 
                     // contains 1,000's of MovieCharacter

boolean hasName = false;
mySet.forEach( i -> i.getName().equals("Darth Vader")) // add here?
assertTrue(hasName);

I know I'm close, but how would I finish the lambda line off so that if the set contains a MovieCharacter where .getName() returns "Darth Vader" the boolean would then get set to true? But if the the item of i under examination doesn't, it just keeps moving along?

Thanks!

3
  • 2
    It's a bad idea to name your class the same as a class from the JDK, especially from the automatically imported java.lang package. Commented Dec 30, 2016 at 22:38
  • That is a great point, I'm going to change it. It's something different in my actual program thankfully. Note for anyone reading this later - MovieCharacter used to just be Character. Commented Dec 30, 2016 at 23:03
  • See also stackoverflow.com/questions/23004921/… Commented Jan 1, 2017 at 19:22

2 Answers 2

5

The behavior you're describing is exactly what the anyMatch method is designed for:

boolean hasName = mySet.stream().anyMatch(c -> c.getName().equals("Darth Vader"));
Sign up to request clarification or add additional context in comments.

6 Comments

Or the (IMHO funkier) lambda-free mySet.stream().map(Character::getName).anyMatch("Darth Vader"::equals);
Would I still want to use this if I know my set would have one and only one match?
@Mureinik's answer worked perfectly, clear and concise, thank you! I may have to use this with a million items one day, would using paralellStream require any differences?
@NateH06 just drop parallelStream() in there instead of stream() and you're done. Note, however, that the underlying collection may return a sequential stream for it, so some testing is advisable.
@NateH06 if you want exactly one, go with .stream().filter(c -> c.getName.equals("Darth Vader")).count() == 1
|
1

What you need here is filtering operation with combination of findFirst() which returns Optional.

If you sure that there is such item in your set, then you may safely call get() after findFirst(), but in general I advice you to provide a default value using orElse or throw exception using orElseThrow on that Optional or use one of other useful methods. Choose wisely what fits your case :)

set.stream()
   .filter(character -> character.getName().equals("Darth Veider"))
   .findFirst()

3 Comments

From my understanding of the JavaDoc, this example would return Optional<Character>.
@NateH06 Yes, it will. Didnt I covered this in answer ? Would you need more claryfiyng ?
sorry, I did not intend for that comment to be posted. I had written something, figured it out before finishing the comment, and something with my browser wouldn't let me delete it, and I guess it posted. Safe to ignore.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.