-2

I am asked to implement a java library module containing 1 method that is able to filter collections of arbitrary objects using a user-defined filter method and returns the object's identity hash code (unique hash code for the object generated by the JVM), preferably using Java 8 constructs. If multiple objects are found, the largest one should be returned. If the comparator does not sort them uniquely, the largest hash code should be returned. The method should follow the signature you can find below.

public <T> Optional<Integer> getIt(Collection<T> collection, Predicate<T> filter, Comparator<T> comparator)

So, I thought to filter first, using:

collection.removeIf(filter.negate());

But, what I couldn't get is, how you could get the largest arbitrary object?

Any help is appreciated, thanks

gyre
  • 16,369
  • 3
  • 37
  • 47

2 Answers2

1

The Streams API does make the solution relatively simple:

public <T> Optional<Integer> getIt(Collection<T> collection, Predicate<T> filter, Comparator<T> comparator) {
    return collection.stream()
        .filter(filter)
        .max(comparator.thenComparingInt(System::identityHashCode))
        .map(System::identityHashCode);
}

Edit: A commenter, Mike Nakis, pointed out to me that this question is likely to fall under the "homework help" category, an observation which I myself missed on the first pass. If you are indeed seeking help with classwork, please use the solutions on this thread honestly. As always, Stack Overflow answers require attribution, and whatever code you turn in should at least provide a link to this question explaining you were stuck and asked for some assistance.

gyre
  • 16,369
  • 3
  • 37
  • 47
  • 1. It is ethically questionable to answer homework questions with actual code. 2. Your streams solution is impressive, but if the OP turns this in, his professor will immediately know that someone else wrote it for him. – Mike Nakis Apr 22 '17 at 21:17
  • Then again, the question says "preferably with java 8 constructs", so the streams solution might be what's expected. But still, answering homework questions with code is bad. – Mike Nakis Apr 22 '17 at 21:21
  • @MikeNakis Looking at the question again, this does seem a bit too much like homework help. On the initial pass it looked like an employer requirement but I guess I scanned a bit too quickly. – gyre Apr 22 '17 at 21:25
  • Thanks all, this is not a classwork, but a work work :). my question is more related to know how to sort by the largest/biggest object. so, what i am seeking is the size of object in memory. –  Apr 22 '17 at 21:50
  • @teqany Then why did you provide us with a function signature and such that is completely unrelated to memory usage? I am finding it difficulty to believe your story. – gyre Apr 22 '17 at 21:53
  • @teqany If your question is really about the memory size of objects, please see https://stackoverflow.com/questions/52353/in-java-what-is-the-best-way-to-determine-the-size-of-an-object. I'm not sure why you keep changing the question though... bear in mind all edits are recorded and publicly viewable. – gyre Apr 22 '17 at 22:04
  • @gyre Thanks for reminding me :) Still i tried Instrumentation, MemoryMeter and MemoryMeasurer. But my problem is to get it runtime. –  Apr 22 '17 at 22:07
  • @teqany I'm not sure I can help you with that... I answered originally because I felt comfortable tackling a streams question, but this is a different area of expertise entirely. I hope other answerers can help you find what you're looking for, though. – gyre Apr 22 '17 at 22:13
1

You need to put your filtered collection in an ArrayList and then invoke on it the sort() method which accepts a comparator.

However, you cannot use the supplied comparator, because according to the definition of the problem, it may not sort your objects uniquely. So, you need to supply your own comparator, which does the following:

First it invokes the supplied comparator; if it returns something other than zero, then you return that result. If the supplied comparator returns zero, then you compute the identity hashcodes of the two objects, you compare the two hashcodes, and you return the result of that comparison.

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142