key value
-------------------------------
aaa 11
aaa 22
aaa 33
aaa 34
aaa 35
bbb 1
bbb 2
bbb 3
bbb 4
ccc 5
ccc 6
ccc 7
----------------------------------1.key 为 String
2.value 为 int
3.按key值已排好序
4.要求按key值相同的,按顺序两两合并value(value值相加)
5.要求按key值相同的,按顺序三三合并value(value值相加)
6.要求按key值相同的,按顺序四四合并value(value值相加)例如:按key值相同的,按顺序两两合并value(value值相加)结果为
key value
----------------------------------
aaa 33
aaa 67
aaa 35
bbb 3
bbb 7
ccc 11
ccc 7----------------------------------例如:按key值相同的,按顺序三三合并value(value值相加)结果为
key value
----------------------------------
aaa 66
aaa 69
bbb 6
bbb 4
ccc 18
----------------------------------例如:四四合并....略..
PS:这些数据存在到List <BeanClass> list = new ArrayList<BeanClass>(); 这个集合里面.
BeanClass为一个JAVABEAN.里面就2个属性
private String key;
private int value;求高手赐教..用最简单的JAVA算法来完成这个操作..
/* 参数
* list ---- 待合并的数组
* n ---- n n 合并
* 返回
* 合并后的数组
*/
public List<BeanClass> combine(List<BeanClass> list, int n) {
List<BeanClass> newlist = new ArrayList<BeanClass>();
int sum;
for (int i=0; i<list.size(); i++) {
if (list.size()-i >= n) {
if (list.get(i).getKey().equals(list.get(i+n-1).getKey())) {
sum = 0;
for (int j=0; j<n; j++) {
sum += list.get(i+j).getValue();
}
BeanClass bc = new BeanClass();
bc.setKey(list.get(i).getKey());
bc.setValue(sum);
newlist.add(bc);
i += n-1;
} else
newlist.add(list.get(i));
} else
newlist.add(list.get(i));
}
return newlist;
}
/* 参数
* list ---- 待合并的数组
* n ---- n n 合并
* 返回
* 合并后的数组
*/ public List<BeanClass> combine(List<BeanClass> list, int n) {
List<BeanClass> newlist = new ArrayList<BeanClass>();
int i,j,sum;
for (i=0; i<list.size(); i++) {
sum = 0;
for (j=i; j<Math.min(i+n, list.size()); j++) {
if (list.get(i).getKey().equals(list.get(j).getKey()))
sum += list.get(j).getValue();
else
break;
}
BeanClass bc = new BeanClass();
bc.setKey(list.get(i).getKey());
bc.setValue(sum);
newlist.add(bc);
i = j-1;
}
return newlist;
}
import java.util.ArrayList;public class MyItem {
public String key;
public int value; public MyItem(String key, int value) {
this.key = key;
this.value = value;
} public static ArrayList<MyItem> sort(ArrayList<MyItem> myItems, int count) {
ArrayList<MyItem> item = new ArrayList<MyItem>();//保存中间结果的ArrayList
ArrayList<MyItem> sortedItems = new ArrayList<MyItem>();//返回的ArrayList
String currentKey = myItems.get(0).key;
int index = 0;
int sum = 0;
for(int i = 0;i < myItems.size();i++){
if(myItems.get(i).key == currentKey){
sum += myItems.get(i).value;
index++;
item.add(new MyItem(currentKey, myItems.get(i).value));
}
else {
currentKey = myItems.get(i).key;
if(index != count){
sortedItems.addAll(item);
}
item.clear();
index = 1;
sum = 0;
sum += myItems.get(i).value;
item.add(new MyItem(currentKey, myItems.get(i).value));
}
if(index == count){
item.clear();
sortedItems.add(new MyItem(currentKey, sum));
index = 0;
sum = 0;
}
}
//最后要加上剩余的项
if(item != null){
sortedItems.addAll(item);
}
return sortedItems;
}
}
import java.util.ArrayList;
public class Test {
public static ArrayList<MyItem> myItemList = new ArrayList<MyItem>();
public static void main(String[] args) {
MyItem[] myItem = new MyItem[12];
myItem[0] = new MyItem("aaa", 11);
myItem[1] = new MyItem("aaa", 22);
myItem[2] = new MyItem("aaa", 33);
myItem[3] = new MyItem("aaa", 34);
myItem[4] = new MyItem("aaa", 35);
myItem[5] = new MyItem("bbb", 1);
myItem[6] = new MyItem("bbb", 2);
myItem[7] = new MyItem("bbb", 3);
myItem[8] = new MyItem("bbb", 4);
myItem[9] = new MyItem("ccc", 5);
myItem[10] = new MyItem("ccc", 6);
myItem[11] = new MyItem("ccc", 7);
for(int i = 0;i < myItem.length;i++){
myItemList.add(myItem[i]);
}
System.out.println("合并前:");
print();
myItemList = MyItem.sort(myItemList, 3);
System.out.println("合并后 n = 3:");
print();
} public static void print(){
for(int i = 0;i < myItemList.size();i++){
System.out.println(myItemList.get(i).key + " " + myItemList.get(i).value);
}
}
}结果:合并前:
aaa 11
aaa 22
aaa 33
aaa 34
aaa 35
bbb 1
bbb 2
bbb 3
bbb 4
ccc 5
ccc 6
ccc 7
合并后 n = 3:
aaa 66
aaa 34
aaa 35
bbb 6
bbb 4
ccc 18