我有一个以这种方式创建的对象集合。我需要做的是按名称排列一个集合。
对象是
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;
}
我的代码impl是 -
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
**********************
答案 0 :(得分:1)
您应该使用PriorityQueue。
PriorityQueue<String> queue = new PriorityQueue<String>(10,comparator)
;
实现说明:此实现为插入方法(offer,poll,remove()和add)方法提供了O(log(n))时间; remove(Object)和contains(Object)方法的线性时间;和检索方法的持续时间(窥视,元素和大小)。
答案 1 :(得分:1)
让您Object
实施Comparable<Object>
,所以......
public int compareTo(final Object other) {
return name.compareTo(other.name);
}
接下来,尝试使用Collections.sort
按如下方式对List<Object>
进行排序。
Collections.sort(day1);
现在,day1
应按字典的升序排序。如果您希望能够修改day1
而无需重新排序,请尝试使用TreeSet
,即
final TreeSet<Object> set = new TreeSet<>();
/* add here */
如果您想支持多个总订单(即按ID或按值),请考虑将比较与Object
分离,并创建一个不同的Comparator<Object>
。只需将Comparator
指定为sort
或TreeSet
构造函数,具体取决于您使用的内容。
答案 2 :(得分:1)
你必须改变你的设计。我建议您使用Outer-Inner
课程和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();
}