62,630
社区成员
发帖
与我相关
我的任务
分享
public class Test {
public static List<A> getMaxList(){
A a1 = new A("CC000",0,"aaa");
A a2 = new A("CC000",1,"bbb");
A a3 = new A("CC001",0,"aaa");
A a4 = new A("CC001",1,"bbb");
A a5 = new A("CC001",2,"ccc");
A a6 = new A("CC002",0,"aaa");
A a7 = new A("CC002",1,"aaa");
A a8 = new A("CC003",0,"aaa");
A a9 = new A("CC003",1,"aaa");
List<A> maxList = new ArrayList<A>();
maxList.add(a1);
maxList.add(a2);
maxList.add(a3);
maxList.add(a4);
maxList.add(a5);
maxList.add(a6);
maxList.add(a7);
maxList.add(a8);
maxList.add(a9);
return maxList;
}
public static void insertSort(List<A> list , A obj){
//首先判断元素是否小于第一个或大于最后一个
A minObj = list.get(0);
A maxObj = list.get(list.size()-1);
if(minObj.getProptis() > obj.getProptis()){
list.add(0 , obj);
return;
}else if(maxObj.getProptis() < obj.getProptis()){
list.add(obj);
return;
}
//循环判断插入位置并插入元素
for(int i = 0 ; i < list.size() - 1 ; i ++){
//当前i对应位置的原始
A currentObj = list.get(i);
//当前i对应位置的下一个元素
A nextObj = list.get(i + 1);
if(obj.getProptis() > currentObj.getProptis() && obj.getProptis() < nextObj.getProptis()){
list.add(i + 1 , obj);
}
}
}
public static void main(String[] args){
List<A> maxList = Test.getMaxList();
Map<String , List<A>> resultMap = new HashMap<String , List<A>>();
for(A a : maxList){
if(resultMap.containsKey(a.getBusiType())){
Test.insertSort(resultMap.get(a.getBusiType()), a);
}else{
List<A> templist = new ArrayList<A>();
templist.add(a);
resultMap.put(a.getBusiType(), templist);
}
}
}
}
class A{
private String busiType;
private int proptis;//须序
private String value;
public A(String busiType, int proptis, String value) {
this.busiType = busiType;
this.proptis = proptis;
this.value = value;
}
public String getBusiType() {
return busiType;
}
public void setBusiType(String busiType) {
this.busiType = busiType;
}
public int getProptis() {
return proptis;
}
public void setProptis(int proptis) {
this.proptis = proptis;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
A[] a = {new A(...), .... new A(...)};
Map<String, List<A>> map = new HashMap<String, List<A>>;
for(int i=0;i<a.length;i++){
String key = a[i].busiType;
List<A> list = map.get(key);
if (list == null){
list = new Vector<A>();
map.put(key, list);
}
list.add(a[i]);
}