3

I have if condition where I am checking for String equality, if they match I store them in Set. Then I am looping through Set to check if ENUM3 value is present, if yes I replace that particular string with value String Java . I am using Iterator to loop and check for equality. I am looking for same functionality with use of streams where

1. I can loop through Set
2. Check for String equality
3. If ENUM3 found then replace with Java
4. Save all the matched String

Here is my code

{
    Set<String> only = new HashSet<String>();
    Iterator<Mark> itr = Marks.iterator();
    while (itr.hasNext()) {
        Mark find = itr.next();

        if (ENUM1.getData().equals(find.search())||ENUM3.getData().equals(find.search())) {
            only.add(find.search());
            only = only.stream()
                    .map(macro -> macro.equals(ENUM3.getData()) ? "Java" : macro).collect(Collectors.toSet());
        }
    }
}

Here is what I tried using Stream

only = only.stream()
            .map(macro -> macro.equals(ENUM3.getData()) ? "Java" : macro)
            .collect(Collectors.toSet());
2
  • Your logic is a little hard to understand. Why are you looping through only in each iteration? Why not do that at the end of the while loop? Commented Jan 17, 2020 at 1:23
  • Sorry I did not get your question. Basically all I am trying to do is check for string equality by iterating through Set and save the values if they match, also if value matches ENUM3 just replace it with String java. With the above Stream code String replace is possible by I am looking for only use streams to loop through Set and check for String equality, in short replacing Iterator logic with Streams. Let me know if this helps Commented Jan 17, 2020 at 1:26

1 Answer 1

2

This should perform your entire operation:

Set<String> only = Marks.stream()
    .map(Mark::search)
    .filter(mark -> ENUM1.getData().equals(mark) 
                    || ENUM2.getData().equals(mark) 
                    || ENUM3.getData().equals(mark))
    .map(macro -> macro.equals(ENUM3.getData()) ? "Java" : macro)
    .collect(Collectors.toSet());

You seem to be unnecessarily doing these 2 (both of which are avoided in the pipeline above)

  • iterating through only in each iteration to replace ENUM3.getData() with Java
  • repeatedly calling Mark.search()
Sign up to request clarification or add additional context in comments.

8 Comments

Further ENUM1.getData().equals(mark) || ENUM2.getData().equals(mark) || ENUM3.getData().equals(mark) could be replaced with a Set.contains where the Set would include Enum1,2,3.getData().
It is giving Cannot find symbol Mark error, on line 2
@user12707940 Mark is used in your original code, so you should have an import for it.
Yes it is, Mark is type. But the error is cannot find symbol variable Mark, not sure why. As search is a method do I need to add parenthesis like search() ?
What happens when you replace Mark::search with mark -> mark.search()?
|

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.