例:
List<String> list = new ArrayList<String>();
list.add("aaa");
list.add("bbb");
list.add("aaa");
list.add("aaa");
list.add("ccc");
list.add("bbb");
输出结果:
aaa 3
bbb 2
ccc 1
完整代码,包括写excel:
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/**
*
*
*/
public class StatisticUser {
public static void main(String[] args) {
List<String> list = getUserLabelList(getUser());
List<JavaBean> groupList = getListByGroup1(list);
System.out.println("********************统计结果*********************");
//按数量排序
Collections.sort(groupList, new Comparator<JavaBean>(){
public int compare(JavaBean o1, JavaBean o2) {
//排序属性
if(o1.getCount() < o2.getCount()){
return 1;
}
if(o1.getCount() == o2.getCount()){
return 0;
}
return -1;
}
});
//遍历打印结果
for (JavaBean bean : groupList) {
//打印部分记录
if(bean.getCount() > 5){
System.out.print(bean.getGroup() + " ");
System.out.println(bean.getCount());
}
}
System.out.println("**************写Excel****************");
long st = System.currentTimeMillis();
writeExcel("d://user.xls", groupList);
long ed = System.currentTimeMillis();
System.out.println("耗时:" + (ed-st) );
}
/**
* 写Excel
* @param path
* @param groupList
*/
public static void writeExcel(String path,List<JavaBean> groupList) {
FileOutputStream fos = null;
HSSFCellStyle style = null;
HSSFCellStyle style2 = null;
String sheet1[] =new String[]{"用户","数量" };
String sheetName[] = {"Sheet1"};
try {
HSSFWorkbook wb = new HSSFWorkbook();
//style = createCellStyle(wb);//表头
//style2 = createCellStyle2(wb);
fos = new FileOutputStream(path);
//System.out.println("----sheet----");
HSSFSheet s = wb.createSheet(sheetName[0]);// sheet名称
//写表格的头部
HSSFRow row = s.createRow(0);
//设置行高
row.setHeight((short)(15.625*30));
//设置行宽
s.setColumnWidth(0, (int)55.7*100);
HSSFCell cell = null;
for (int i = 0; i < sheet1.length; i++) {
cell = row.createCell(i);
cell.setCellValue(sheet1[i]);
//cell.setCellStyle(style);//设置样式
}
//遍历写单元格
int flag = 1;
for (JavaBean bean : groupList) {
row = s.createRow(flag);
//System.out.print(bean.getGroup() + " ");
//System.out.println(bean.getMoney());
cell = row.createCell(0);
cell.setCellValue( bean.getGroup() );
cell = row.createCell(1);
cell.setCellValue( bean.getCount() );
flag++;
}
wb.write(fos);
fos.close();
} catch (Exception e) {
// TODO: handle exception
System.out.println("写Excel异常:" + e);
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/**
* List集合String类型
* @param list
* @return
*/
private static List<JavaBean> getListByGroup1(List<String> list) {
List<JavaBean> result = new ArrayList<JavaBean>();
Map<String, Integer> map = new HashMap<String, Integer>();
for (Iterator iterator = list.iterator(); iterator.hasNext();) {
String key = (String) iterator.next();
//System.out.println("*****" + key );
if (map.containsKey( key )) {
//System.out.println("*****" + key + "*" + map.get( key ) );
map.put( key , map.get( key ) + 1);
} else {
map.put( key , 1);
}
}
for (Entry<String, Integer> entry : map.entrySet()) {
result.add(new JavaBean(entry.getKey(), entry.getValue()));
}
return result;
}
/**
* 处理下list
* @param list
* @return
*/
private static List<String> getUserLabelList(List<String> list){
List<String> list2 = new ArrayList<String>();
for (Iterator iterator = list.iterator(); iterator.hasNext();) {
String key = (String) iterator.next();
//System.out.println("*****" + key );
String key_arr[] = key.split(",");
for (int i = 0; i < key_arr.length; i++) {
list2.add(key_arr[i]);
}
}
return list2;
}
/**
* @return
*/
public static List<String> getUser() {
List<String> list = new ArrayList<String>();
list.add("美食,名人明星,文艺,时尚");
list.add("名人明星");
list.add("动漫");
list.add("动漫,星座运势");
list.add("动漫,八卦杂谈");
list.add("动漫,宅女,看书");
list.add("美图摄影,动漫,汽车");
return list;
}
}
class JavaBean {
private String group;
private int count;
public JavaBean() {
}
public JavaBean(String group, int count) {
this.group = group;
this.count = count;
}
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
这篇博客展示了如何利用Java List集合来实现类似于MySQL Group By的功能,通过示例代码详细解释了操作过程,并提供了将结果导出到Excel的完整代码。
1363

被折叠的 条评论
为什么被折叠?



