对有序list分组
近来要对一个有序的list进行分组,由于我是初学者,以前没有编程经验,所以出了许多bug。下面分享一下。
List里面放着author,author有属性id、name和writeCount三个属性,分别表示作者id、昵称和编辑文章数量。对于按照id排序的list,统计每个作者编辑了多少文章。
public class author { private int id; private String name; private int writeCount; //set get 方法 } List的初始值是:original list author id=1 author name=1ligang author id=2 author name=2ligang author id=2 author name=2ligang author id=3 author name=3ligang author id=3 author name=3ligang author id=3 author name=3ligang author id=4 author name=4ligang author id=4 author name=4ligang author id=4 author name=4ligang author id=4 author name=4ligang 开始我想都没有想打算用2个for循环遍历,可是后面就出现了bug。哈哈,死循环.....代码如下: public void groupList(List list) { if(list==null || list.size()==0) { return; } List resultList=new ArrayList(); int tempWriteCount=1; author tempAuthor=new author(); for(int i=0;i<list.size();i++) { //tempAuthor=list.get(i); for(int j=1;j<list.size();j++) { if(list.get(i).getId()==list.get(j).getId()) { tempWriteCount+=1; }else { tempAuthor.setWriteCount(tempWriteCount); resultList.add(tempAuthor); tempWriteCount=1; i=j;
}
}
}
tempAuthor.setWriteCount(tempWriteCount);
resultList.add(tempAuthor);
for(author temp:resultList)
{
System.out.println("id="+temp.getId()+" name="+temp.getName()+" write "+temp.getWriteCount()+" book");
}
}
结果出错,Exception in thread "main" java.lang.OutOfMemoryError: Java heap space。自己想想就算是i=j没执行,也只执行10*10次不至于没存耗尽。最后发现在i=j处出现了问题,让i永远都小于10,出错。囧 后来改了下: public void groupList(List list) { if(list==null || list.size()==0) { return; } List resultList=new ArrayList(); int tempWriteCount=1; author tempAuthor=new author(); for(int i=0;i<list.size();i++) { tempAuthor=list.get(i); for(int j=i+1;j<list.size();) { if(tempAuthor.getId()==list.get(j).getId()) { tempWriteCount+=1; break; }else { tempAuthor.setWriteCount(tempWriteCount); resultList.add(tempAuthor); tempWriteCount=1; i=j; break;
}
}
}
tempAuthor.setWriteCount(tempWriteCount);
resultList.add(tempAuthor);
for(author temp:resultList)
{
System.out.println("id="+temp.getId()+" name="+temp.getName()+" write "+temp.getWriteCount()+" book");
}
}
运行结果 id=1 name=1ligang write 1 book id=2 name=2ligang write 1 book id=3 name=3ligang write 2 book id=4 name=4ligang write 3 book 分组对了,计算结果不对。 改进时候将i=j;改为i=j-1;。成功。 再后来一想,只用遍历一遍list比较相邻的id是否相同即可。代码如下: public void groupList(List list) { if (list == null || list.size() == 0) return; int tempWriterCount=1; author tempAuthor = new author(); List resultList = new ArrayList(); for (int i = 0; i < list.size()-1; i++) {
tempAuthor = list.get(i);//get i
if(tempAuthor.getId()==list.get(i+1).getId())
{
tempWriterCount+=1;
}else {
tempAuthor.setWriteCount(tempWriterCount);
resultList.add(tempAuthor);
tempAuthor=new author();
tempWriterCount=1;
}
}
tempAuthor.setWriteCount(tempWriterCount);
resultList.add(tempAuthor);//add the last one
System.out.println("after group:");
for (author temp : resultList)
{
temp.setAuthorCount(resultList.size());
System.out.println(temp.getId()+" "+temp.getName()+" "+temp.getWriteCount());
}
}
最后还可以用List<List>来分组,代码如下: public void groupOrderedList(List list) { List<List> tempList = new ArrayList<List>(); List group = new ArrayList();
List<author> displayList=new ArrayList<author>();
author temp = list.get(0);
group.add(temp);
for(int i=1;i<list.size();i++)
{
if(temp.getId()==list.get(i).getId())
{
group.add(temp);
}else {
tempList.add(group);
temp=list.get(i);
group=new ArrayList<author>();
group.add(temp);
}
}
tempList.add(group);
for (List<author> tempDisplayList : tempList)
{
author displayAuthor=new author();
displayAuthor=tempDisplayList.get(0);
displayAuthor.setWriteCount(tempDisplayList.size());
displayList.add(displayAuthor);
}
System.out.println("grouped list:");
for(author tempAuthor:displayList)
{
System.out.println(tempAuthor.getId()+" "+tempAuthor.getName()+" "+tempAuthor.getWriteCount());
}
System.out.println("total author is "+displayList.size());
}