如何排序类的ArrayList [英] How to sort ArrayList of classes

查看:184
本文介绍了如何排序类的ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想排序其中有一个抽象类型的ArrayList类

下面是我的界面

 公共接口iSomeInterface {
    公众诠释级();
}

有几个实现这个接口的这是由他们的水平实现不同的。

 公共类LEVELONE实现iSomeInterface {
    @覆盖
    公众诠释级(){返回1;}
}公共类LevelTwo实现iSomeInterface {
    @覆盖
    公众诠释级(){返回2;}
}
公共类LevelThree实现iSomeInterface {
    @覆盖
    公众诠释级(){返回3;}
}

如果我有几个这些类的实例,并将其传递给级别

 的ArrayList< iSomeInterface> someInterfaces =新的ArrayList< iSomeInterface>();
        someInterfaces.add(新LEVELONE());
        someInterfaces.add(新LevelTwo());
        someInterfaces.add(新LEVELONE());
        someInterfaces.add(新LevelThree());
        someInterfaces.add(新LEVELONE());
        级别水平=新的水平(someInterfaces);

我如何可以排序这些基于其电平值归类。

 公共类级别{
    私人的ArrayList< iSomeInterface> someInterfaces;    公共级别(ArrayList的< iSomeInterface> someInterfaces){        this.someInterfaces = someInterfaces;
    }    公众的ArrayList< iSomeInterface>排序(){        //一些code到分拣实例        返回someInterfaces;
    }
}

答:
感谢您的帮助,最终的答案:

 公共类someComparator实现比较< iSomeInterface> {
 @覆盖
  公众诠释比较(iSomeInterface第一,第二iSom​​eInterface){
    返回first.level()&所述; second.level() - 1:first.level()== second.level()?0:1;
  }
}公众的ArrayList< iSomeInterface>排序(){
  Collections.sort(someInterfaces,新someComparator());
  返回someInterfaces;
}


解决方案

最简单的办法,如果你使用的是Java 8。

 公开的ArrayList< iSomeInterface>排序(){    Collections.sort(someInterfaces,(iSomeInterface的i1,i2的iSomeInterface) - > i1.getLevel() -  i2.getLevel());    返回someInterfaces;
}

如果你在此之前,一些你可以实现自己的比较工作...有一个匿名内部类仍使1衬...

 公开的ArrayList< iSomeInterface>排序(){
    Collections.sort(接口,新的比较< iSomeInterface>(){
        @覆盖
        公众诠释比较(iSomeInterface I1,I2 iSomeInterface){
            返回i1.getLevel() - i2.getLevel();
        }
    });
    返回接口;
}

我假设你要修改的成员变量,但你总是可以先复制您的清单。

(注意Collections是java.util.Collections中)

I am trying to sort arrayList of classes which have a abstract type.

Here is my interface

public interface iSomeInterface {
    public int level();
}

There are couple of implementation of this interface which is different by their level implementations.

public class LevelOne implements iSomeInterface {
    @Override
    public int level() {return 1;}
}

public class LevelTwo implements iSomeInterface {
    @Override
    public int level() {return 2;}
}
public class LevelThree implements iSomeInterface {
    @Override
    public int level() {return 3;}
}

if I have couple of instances of these classes and passing them to the Levels class

ArrayList<iSomeInterface> someInterfaces = new ArrayList<iSomeInterface>();
        someInterfaces.add(new LevelOne());
        someInterfaces.add(new LevelTwo());
        someInterfaces.add(new LevelOne());
        someInterfaces.add(new LevelThree());
        someInterfaces.add(new LevelOne());
        Levels levels = new Levels(someInterfaces);

How can I sort these classed based on their level value.

public class Levels {
    private ArrayList<iSomeInterface> someInterfaces;

    public Levels(ArrayList<iSomeInterface> someInterfaces){

        this.someInterfaces = someInterfaces;
    }

    public ArrayList<iSomeInterface> sorting(){

        //some code to sorting instances

        return someInterfaces;
    }
}

Answer : Thanks for your helps, The final answer :

public class someComparator implements Comparator<iSomeInterface> {
 @Override
  public int compare(iSomeInterface first, iSomeInterface second) {
    return first.level()<second.level()?-1:first.level()==second.level()?0:1;
  }
}

public ArrayList<iSomeInterface> sorting(){
  Collections.sort(someInterfaces,new someComparator());
  return someInterfaces;
}

解决方案

The simplest solution if you're using java 8.

public ArrayList<iSomeInterface> sorting(){

    Collections.sort(someInterfaces, (iSomeInterface  i1, iSomeInterface  i2) -> i1.getLevel() - i2.getLevel());

    return someInterfaces;
}

If you're working in something prior to that you can implement your own comparator...with an anonymous inner class to still make a 1-liner...

public ArrayList<iSomeInterface> sorting(){
    Collections.sort(interfaces, new Comparator<iSomeInterface>() {
        @Override
        public int compare(iSomeInterface i1, iSomeInterface i2) {
            return i1.getLevel() - i2.getLevel();
        }
    });
    return interfaces;
}

I assume you want to modify your member variable, but you can always duplicate your list first.

(Note Collections is java.util.Collections)

这篇关于如何排序类的ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆