0

I would like to get the highest score group by Id .If two highest score's are same then i would like get the highest score based on lowest Optional ID.I would like to get it in Java Stream.So far this code works.Is there any efficient way to rewrite this code in java stream

Example :

records=record.Person(batchNumber);
List<Person> highestRecords = new ArrayList<>();for(
Person s:records)
  {
if(!highestRecords.isEmpty()) {
    boolean contains = false;
    for(Person ns: new ArrayList<>(highestRecords)) {
        if(s.Id().compareTo(ns.Id()) == 0) {
            contains = true;
            if(s.getScore.compareTo(ns.getScore()) > 0     
    && s.optionalId().compareTo(ns.optionalId()) < 0) {

                highestRecords.remove(ns);
                highestRecords.add(s)       
            }
        }
    }
    if(contains == false) {
        highestRecords.add(s);
    }
}else {
    highestRecords.add(s);
}
  }
}
7
  • 2
    okay we see you have code using for each loop, why don't you explain with some input and output example Commented Jan 25, 2019 at 17:10
  • Variable names are really confusing, why List<Person> called getNewPendingMatches? Maybe the first step should be to clean-up existing solution before thinking about conversion to FP. Commented Jan 25, 2019 at 17:12
  • Just so you know, I do see the edits you're making to your question. If you're hoping that may entice someone to come along to write this out as a stream, that's...unlikely. Commented Jan 25, 2019 at 17:38
  • Thanks i thought my question was not clear thats why i made some changes Commented Jan 25, 2019 at 17:41
  • Off topic but I would strongly suggest you look over your naming practices of variable because this code is very hard to read. Don't name a variable getX because this name suggests a method that will return a class member so it gets confusing reading it. Furthermore separate the names better, having variables with long almost identical names also makes it harder to understand the code. Commented Jan 25, 2019 at 17:43

1 Answer 1

3

Don't convert this to a stream.

There is no one pure operation happening here. There are several.

Of note is the initial operation:

if(getNewPendingMatches.size() > 0) 

That's always going to be false on the first iteration and you're always going to add one element in.

On subsequent iterations, life gets weird because now you're trying to remove elements while iterating over them. A stream cannot delete values from itself while iterating over itself; it only ever processes in one direction.

As written this code should not be converted to a stream. You won't gain any benefits in doing so, and you're going to actively harm readability if you do.

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

6 Comments

.... "On subsequent iterations, life gets weird" - very much agreed. Not sure the code could get less readable than it is already!
@MarkKeen: You know what would make it more unreadable? Converting it into a stream. ;)
A stream would be very readable .... based on the example requirement, not supplied code....
Meh - I can't see much way around the forEaches required to glue it all together, honestly...
ha .. as I say based on the example output, not supplied code .. as per my comment on OP's question ..
|

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.