如何对一个List中元素进行统计?

LoginOut 2010-01-05 09:40:23
有一List如下

1 10
1 20
2 10
3 57
1 2
3 24


统计结果如下:
1 10,20,2
2 10
3 57,24

该如何处理?

...全文
569 29 打赏 收藏 举报
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
LoginOut 2010-01-06
  • 打赏
  • 举报
回复
[Quote=引用 27 楼 phyerbarte 的回复:]
我把listb新建了一个List这是为了满足你算法中需要对2个list做比较,这应该是你思路的初衷。不能直接使用
List listb = list;而应该用clone()但是即使clone()了,也不能复制出list里的对象,还是需要循环,效率更低,这部分,自己查资料。
listb.remove(j);
list.remove(j);
这2句是为了能够使2个list同步,保持index的一致性。把符合条件的数据直接删除,防止外层循环再次检查已经保留数据。
[/Quote]

非常之感谢,我再想想看
在BJ工作0801 2010-01-06
  • 打赏
  • 举报
回复
都是牛人人
phyerbarte 2010-01-06
  • 打赏
  • 举报
回复
我把listb新建了一个List这是为了满足你算法中需要对2个list做比较,这应该是你思路的初衷。不能直接使用
List listb = list;而应该用clone()但是即使clone()了,也不能复制出list里的对象,还是需要循环,效率更低,这部分,自己查资料。
listb.remove(j);
list.remove(j);
这2句是为了能够使2个list同步,保持index的一致性。把符合条件的数据直接删除,防止外层循环再次检查已经保留数据。
phyerbarte 2010-01-06
  • 打赏
  • 举报
回复
算,你的算法真的很悲剧。这是我帮你改良的。

package str;

import java.util.ArrayList;
import java.util.List;

public class OtherOrder {

public static void main(String[] args) {
List list = new ArrayList();
String[] uinfo = new String[] { "1", "10" };
String[] uinfo1 = new String[] { "1", "20" };
String[] uinfo2 = new String[] { "2", "30" };
String[] uinfo3 = new String[] { "3", "340" };
String[] uinfo4 = new String[] { "1", "50" };
list.add(uinfo);
list.add(uinfo1);
list.add(uinfo2);
list.add(uinfo3);
list.add(uinfo4);

List listb = new ArrayList();
listb.add(uinfo);
listb.add(uinfo1);
listb.add(uinfo2);
listb.add(uinfo3);
listb.add(uinfo4);

String age = "";
int currentP = 0;
while (list.size() > 0) {
String[] user = (String[]) list.get(0);
for (int j = 0; j < listb.size(); j++) {
String[] info = (String[]) listb.get(j);
if (user[0].equals(info[0])) {
age = age + info[1] + ",";
listb.remove(j);
list.remove(j);
}
}
System.out.println(user[0] + "_" + age);
age = "";
}
}
}


问题出在几个地方
1、List listb = list;
这算什么?listb成为了list的一个引用。你在remove的时候2个是一起remove,这造成了for循环的结束条件一直在变。这种写法非常不可取。直接导致你的get(index)方法中index一直在变。
2、举个例子1_10,20,50, 你remove了里面的10,却没有办法remove其中的20和50,再下次循环时,依然要检查20和50的值,这影响效率。
3、痘号的拼接算法,永远后面会有个多余符号。。。
其他没了,最严重的问题就是下标移动。在我修改你的算法中有一句。
String[] user = (String[]) list.get(0);
请仔细理解为什么循环都永远取0而不用递增了。
LoginOut 2010-01-06
  • 打赏
  • 举报
回复
[Quote=引用 23 楼 phyerbarte 的回复:]
引用 22 楼 loginout 的回复:
引用 21 楼 phyerbarte 的回复:
list大小在变,但是你删除的list不时所有引用过的。

我再想想


同学..分...
[/Quote]
分肯定是你的,你都帮忙成这样了
苍蝇①号 2010-01-06
  • 打赏
  • 举报
回复
		List<int[]> strList = new ArrayList<int[]>();
strList.add(new int[]{1, 10});
strList.add(new int[]{1, 20});
strList.add(new int[]{2, 10});
strList.add(new int[]{3, 57});
strList.add(new int[]{1, 2});
strList.add(new int[]{3, 24});
Map<Integer,List<Integer>> map = new HashMap<Integer, List<Integer>>();
for(int[] e: strList){
if(! map.containsKey(e[0])){
map.put(e[0], new ArrayList<Integer>());
}
map.get(e[0]).add(e[1]);
}

for(Map.Entry<Integer, List<Integer>> entry : map.entrySet()){
System.out.println(entry.getKey()+" " + entry.getValue());
}
phyerbarte 2010-01-06
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 loginout 的回复:]
引用 21 楼 phyerbarte 的回复:
list大小在变,但是你删除的list不时所有引用过的。

我再想想
[/Quote]

同学..分...
LoginOut 2010-01-06
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 phyerbarte 的回复:]
list大小在变,但是你删除的list不时所有引用过的。
[/Quote]
我再想想
qiuqiupeng 2010-01-05
  • 打赏
  • 举报
回复
太懒了吧,不用动脑筋啊!就那么点数据怎么弄效率都可以接受的
phyerbarte 2010-01-05
  • 打赏
  • 举报
回复
你如果要把前面的1,2,3排序,直接换成TreeMap就可以了,如果要其他排法,自己实现个compaire接口。
bayougeng 2010-01-05
  • 打赏
  • 举报
回复
在数据库里做一下order by。
然后遍历一遍结果集,把第一列相同的记录做成一个数组就行了。
至于遍历的算法,应该就很简单了吧,把前一个的第一列和后一个的第一列比,不一样则新生成一个数组。一样则add。
phyerbarte 2010-01-05
  • 打赏
  • 举报
回复
假如2行值,输入结果还是
1: 10,20,2
2: 10
3: 57,24

哦,你理解错我意思
都已经进map了,还在乎输出吗?


package str;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Calculator {

/**
* @param args
*/
public static void main(String[] args) {
// 1 10
// 1 20
// 2 10
// 3 57
// 1 2
// 3 24
List strList = new ArrayList();

Map dataMap = new HashMap();


strList.add(new String[]{"1", "10"});
strList.add(new String[]{"1", "20"});
strList.add(new String[]{"2", "10"});
strList.add(new String[]{"3", "57"});
strList.add(new String[]{"1", "2"});
strList.add(new String[]{"3", "24"});
strList.add(new String[] { "33", "24" });
strList.add(new String[] { "352", "24" });

for(int i=0;i<strList.size();i++){
String[] temp = (String[]) strList.get(i);
String value = getMapValue(dataMap, temp);
dataMap.put(temp[0], value);
}

Set key = dataMap.keySet();
Iterator it = key.iterator();
while(it.hasNext()){
String keyStr = (String) it.next();
String value = (String) dataMap.get(keyStr);
System.out.println(keyStr+": "+value);
}
}

private static String getMapValue(Map dataMap, String[] temp) {
String mapValue = (String)dataMap.get(temp[0]);
String returnValue = temp[1];
if(mapValue!=null){
returnValue=mapValue+","+temp[1];
}
return returnValue;
}
}

LoginOut 2010-01-05
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 phyerbarte 的回复:]
改了一下,意思对了吗?
[/Quote]
List 中A的值不是固定的,你现在是写死了为1,2,3
假如从数据库读取的值为1到10000,
按照你的写法,需要些多少dataMap.get()



strList.add(new String[] { "33", "24" });
strList.add(new String[] { "352", "24" });

假如2行值,输入结果还是
1: 10,20,2
2: 10
3: 57,24
huangwj20042008 2010-01-05
  • 打赏
  • 举报
回复
public static Map<Integer,List<Integer>> statisticList1(List<Entry> list){
Map<Integer,List<Integer>> map = new HashMap<Integer,List<Integer>>();
for(int i=0;i<list.size();i++){
Entry tempEntry = list.get(i);
Integer member1 = Integer.valueOf(tempEntry.getMember1());
Integer member2 = Integer.valueOf(tempEntry.getMember2());
List<Integer> value = map.get(member1);
if(value != null){
value.add(member2);
}else{
value = new ArrayList<Integer>();
value.add(member2);
map.put(member1, value);
}
}
return map;
}
class Entry{
private int member1;
private int member2;
public Entry(int member1,int member2){
this.member1 = member1;
this.member2 = member2;
}
public int getMember1() {
return member1;
}
public void setMember1(int member1) {
this.member1 = member1;
}
public int getMember2() {
return member2;
}
public void setMember2(int member2) {
this.member2 = member2;
}

}
phyerbarte 2010-01-05
  • 打赏
  • 举报
回复
改了一下,意思对了吗?


package str;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Calculator {

/**
* @param args
*/
public static void main(String[] args) {
// 1 10
// 1 20
// 2 10
// 3 57
// 1 2
// 3 24
List strList = new ArrayList();

Map dataMap = new HashMap();


strList.add(new String[]{"1", "10"});
strList.add(new String[]{"1", "20"});
strList.add(new String[]{"2", "10"});
strList.add(new String[]{"3", "57"});
strList.add(new String[]{"1", "2"});
strList.add(new String[]{"3", "24"});

for(int i=0;i<strList.size();i++){
String[] temp = (String[]) strList.get(i);
String value = getMapValue(dataMap, temp);
dataMap.put(temp[0], value);
}

String one = (String) dataMap.get("1");
String two = (String) dataMap.get("2");
String three = (String) dataMap.get("3");


System.out.println("1: "+one);
System.out.println("2: "+two);
System.out.println("3: "+three);
}

private static String getMapValue(Map dataMap, String[] temp) {
String mapValue = (String)dataMap.get(temp[0]);
String returnValue = temp[1];
if(mapValue!=null){
returnValue=mapValue+","+temp[1];
}
return returnValue;
}
}

zhanggc1001 2010-01-05
  • 打赏
  • 举报
回复

//A,List<B>
Map<Integer, List<Integer>> total = new HashMap<Integer, List<Integer>>();
//rs is your resultset
while (rs) {
if (total.containsKey(rs.getInt(1))) {
total.get(rs.getInt(1)).add(rs.getInt(2))
}
else {
List<Integer> list = new ArrayList<Integer>();
list.add(rs.getInt(2));
total.put(rs.getInt(1), list);
}
}

不知道满足你的要求不
LoginOut 2010-01-05
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 phyerbarte 的回复:]
List 里面的元素不是固定的,是从数据库里面读取的
准确说List 有如下2列
A  B

当A里面的值相同的时候,B的值就合计,以逗号隔开

我的不是就能用?关键你数据库出来的是什么样子?
[/Quote]
A 列的值为1 到 100 或者更大
B 列的值为 时间

phyerbarte 2010-01-05
  • 打赏
  • 举报
回复
List 里面的元素不是固定的,是从数据库里面读取的
准确说List 有如下2列
A B

当A里面的值相同的时候,B的值就合计,以逗号隔开

我的不是就能用?关键你数据库出来的是什么样子?
LoginOut 2010-01-05
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 notlogin 的回复:]
从数据库读取?那直接group by一下不就行了?一定要自己写JAVA处理么?
[/Quote]
直接读取的List以列表形式显示
合计的list有其他的用处
notlogin 2010-01-05
  • 打赏
  • 举报
回复
从数据库读取?那直接group by一下不就行了?一定要自己写JAVA处理么?
加载更多回复(9)

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧