I have a context map, which stores a list sorted in default order.
Map<String,Object> context = new HashMap<String,Object>();
context.put("list_all_content_default",getAllContentSortedDefault());
where getAllContentSortedDefault() returns a List by sorted in default order.
Now, I want to store the same list, in two different orders, lets say by creation date as well.
It is not possible for my clients to retrieve the list and then sort it.
So I have an option to populate the same context map twice with different key
context.put("list_all_content_createdate",getAllContentSortedCreateDate());
But this would mean, storing the same elements in context twice with different keys.
Is it possible in Java 8 to store references of methods in a Map, which can be invoked on call. e.g. something like
context.put("list_all_content_createdate",{
List<Contents> all_contents = (List<Contents>)context.get("list_all_content_default");
return Collections.sort(all_contents, new ContentComparatorByCreateDate();
});
This would help me save memory , and would create desired sort behaviour.
BiFunction,Predicateetc. In case you have multiple presentations of them why not just define your custom super interface.Collections.sort(context.get("list_all_content_createdate"), new ContextComparatorByCreateDate());. No Stream required.