compare and sort different type of objects using java Collections

前端 未结 3 1570
一生所求
一生所求 2020-12-20 22:55

How to compare and sort different type of objects using java Collections .Below is the use case: For example DOG,MAN,TREE, COMPUTER,MACHINE - all these different objects has

3条回答
  •  情话喂你
    2020-12-20 23:49

    If all your classes implement a Living interface defined as it follows (always a good idea in Java):

    public interface Living {
        int getLifeTime();
    }
    

    you can sort your collections of Living objects by using the lambdaj library as it follows:

    List sortedLivings = sort(livings, on(Alive.class).getLifeTime());
    

提交回复
热议问题