我有一个以这种方式创建的对象集合。我需要做的是按名称排列集合。
对象是
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
**********************