0

我有一个以这种方式创建的对象集合。我需要做的是按名称排列集合。

对象是

public class Object {
    private Integer id;
    private String name;
    private int value;

    public Object(Integer id, String name, int value) {
        this.id = id;
        this.name = name;
        this.value = value;
    }
        //getters,setters
}
private static List<Object> populateObjList1(){
    List<Object> objList = new ArrayList<Object> ();
    Object obj1 = new Object(1, "aa", 4);
    Object obj2 = new Object(2, "bb", 3);
    Object obj3 = new Object(3, "cc", 7);
    Object obj4 = new Object(4, "dd", 6);
    Object obj5 = new Object(1, "aa", 2);
    Object obj6 = new Object(2, "cc", 1);
    Object obj7 = new Object(3, "ee", 5);
    Object obj8 = new Object(4, "ff", 7);
    Object obj9 = new Object(1, "bb", 3);
    Object obj10 = new Object(2, "cc", 4);
    Object obj11 = new Object(3, "dd", 7);
    Object obj12 = new Object(4, "ff", 1);
    objList.add(obj1);
    objList.add(obj2);
    objList.add(obj3);
    objList.add(obj4);
    objList.add(obj5);
    objList.add(obj6);
    objList.add(obj7);
    objList.add(obj8);
    objList.add(obj9);
    objList.add(obj10);
    objList.add(obj11);
    objList.add(obj12);
    return objList;
} 

我的代码是-

public static void main (String args[]){
    List<Object> day1 = populateObjList1();
    List<String> abj = new ArrayList<String>();
    System.out.println(""+abj.size());
    for (Object object : day1) {
        if(!abj.contains(object.getName())){
            abj.add(object.getName());
        }else{
            System.out.println("available");
        }
    }
    for (String string : abj) {
        System.out.println("__ "+string);
    }   
}

    *** The expected dataset should be *****
    **********************
    |Name | ** | ** | ** |
    **********************
    aa      4     2    - 
    **********************
    bb      3     -    3
    **********************
    cc      7     1    4
    **********************
    dd      6     -    7
    **********************
    ee      -     5    -
    **********************
    ff      -     7    1
    **********************
4

3 回答 3

1

您应该使用PriorityQueue

PriorityQueue<String> queue = new PriorityQueue<String>(10,comparator);

实现说明:此实现为插入方法(offer、poll、remove() 和 add)方法提供 O(log(n)) 时间;remove(Object) 和 contains(Object) 方法的线性时间;检索方法(peek、元素和大小)的恒定时间。

于 2012-09-11T04:14:56.743 回答
1

有你的Object工具Comparable<Object>,所以...

public int compareTo(final Object other) {
  return name.compareTo(other.name);
}

接下来,尝试List<Object>使用Collections.sort.

Collections.sort(day1);

现在,day1应该按名称的字典升序排序。如果您希望能够修改day1而不必重新排序,请尝试使用 a TreeSet,即

final TreeSet<Object> set = new TreeSet<>();
/* add here */

如果您想支持多个总订单(即按 id 或按值),请考虑将比较从中解耦Object并创建一个 distinct Comparator<Object>。只需指定ComparatortosortTreeSet构造函数,具体取决于您使用的。

于 2012-09-11T04:27:56.753 回答
1

你必须改变你的设计。我想建议使用Outer-Innerclass 和TreeSet.

class Foo {
 private String name;
 private TreeSet<Item> items;

 public Foo(String name) {
    this.name = name;
    items = new TreeSet<Item>();
 }
 public String getName() { return name; }
 public void setName(String name) {this.name = name;}
 public TreeSet<Item> getItems() {return items; }
 public void addItem(Integer id, Integer value) {
    items.add(new Item(id, value));
 }

 public class Item implements Comparable {
    @Override
    public int compareTo(Object arg0) {
        Item i = (Item) arg0;
        if (id == i.id)
            return 0;
        else if (id > i.id)
            return 1;
        else
            return -1;
    }

    private Integer id;
    private Integer value;

    public Item(Integer id, Integer value) {
        this.id = id;
        this.value = value;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getValue() {
        return value;
    }

    public void setValue(Integer value) {
        this.value = value;
    }
}

}

并创建Foo和的对象Item

List<Foo> list = new ArrayList<Foo>();
list.add(new Foo("aa"));
list.add(new Foo("bb"));
list.add(new Foo("cc"));
list.add(new Foo("dd"));

list.get(0).addItem(1, 4);
list.get(0).addItem(2, 2);
list.get(0).addItem(3, 3);
list.get(1).addItem(3, 1);
list.get(1).addItem(1, 1);
list.get(1).addItem(2, 8);
list.get(2).addItem(3, 3);
list.get(3).addItem(2, 10);
list.get(0).addItem(5, 10);

for (Foo foo : list) {
 System.out.print(foo.getName());
 Foo.Item[] ar = foo.getItems().toArray(new Foo.Item[0]);
 for (int i = 1, j = 0; i <= 8; i++) {
   if (j >= ar.length)
    System.out.print(" - ");
   else if (ar[j].getId() == i) {
    System.out.print(" " + ar[j].getValue());
    j++;
   } else
        System.out.print(" - ");
  }
  System.out.println();
}
于 2012-09-12T03:56:40.580 回答