1

I'm wondering if its possible to sort an EntrySet< K, V > based on some property of K using lambdaj ?

I dont think Lambdaj will let me I just wanted to confirm.

For example if I have a Map< A, B > where A has the following structure:

public class A{
private double published;

     public double getPublished()
     {
           return double; 
     }
}

Am I able to sort an entryset, using lambdaj, on A.getPublished ?

MikePatel
  • 2,593
  • 2
  • 24
  • 38
  • What does it mean to sort a `Set`? `Set`s don't have order, right? – missingfaktor Jun 29 '12 at 15:54
  • I mean sort a returned collection. Thats what lambdaj does right ? It doesnt mutate the input collection. – MikePatel Jun 29 '12 at 15:58
  • I think your assumption is correct, but I am not a lambdaj expert. Aside: If this is not just an academic question, you may want to use a LinkedHashMap, where the order is determined by the insertion order. – HellishHeat Jun 29 '12 at 21:47

1 Answers1

3

Unfortunately what you're asking is not possible because of type erasure. Indeed supposing you have map of type Map what you would like to do is something as:

sort(map.entrySet(), on(Map.Entry.class).getKey().getPublished());

That doesn't work because lambdaj doesn't know that Map.Entry is actually a Map.Entry (and of course a syntax like on(Map.Entry.class).getKey()... is not allowed). It means that the on(Map.Entry.class).getKey() invocation just returns an object of Object class and then you're not allowed to invoke the getPublished() method on it.

Mario Fusco
  • 13,548
  • 3
  • 28
  • 37