java.util包的集合框架应用


package mix.test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* @function 【武汉】歪尘(531314208) 2011-10-26 20:55:54<BR>
* set集合有一些javabean,我要怎么进行过滤?<BR>
* name 数量 <BR>
* a 10<BR>
* b 3<BR>
* a 3<BR>
* b 5<BR>
*
* name 数量 <BR>
* a 13<BR>
* b 8<BR>
*
* @author ocaicai@yeah.net
*
* @date 2011-10-27
*/
public class ListTest {

/**
* @param args
*/
public static void main(String[] args) {
List<Product> list = genernateProduct();
list = getCountByName(list);
System.out.println(Arrays.toString(list.toArray()));
}

public static List<Product> genernateProduct() {
List<Product> list = new ArrayList<Product>();
list.add(new Product("a", 10));
list.add(new Product("b", 3));
list.add(new Product("a", 3));
list.add(new Product("c", 5));
list.add(new Product("b", 5));
list.add(new Product("c", 7));
return list;
}

public static List<Product> getCountByName(List<Product> list) {

List<Product> targetList = new ArrayList<Product>();
int count = 0;
Product product = null;

// 去除掉重复name,得到有序的name
Set<String> nameSet = new HashSet<String>();
List<String> nameList = new ArrayList<String>();
for (int i = 0; i < list.size(); i++)
nameSet.add(list.get(i).getName());
for (String name : nameSet)
nameList.add(name);
Collections.sort(nameList);

// 获取每个name的count和
for (String name : nameList) {
count = 0;
for (int i = 0; i < list.size(); i++) {
product = list.get(i);
if (product.getName().equals(name)) {
count += product.getCount();
list.remove(i);
i--;
}
}
targetList.add(new Product(name, count));
}

return targetList;
}
}




package mix.test;

public class Product {
private String name;
private int count;

public Product(String name, int count) {
super();
this.name = name;
this.count = count;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getCount() {
return count;
}

public void setCount(int count) {
this.count = count;
}

@Override
public String toString() {
return this.name + ":" + this.count;
}
}



输出结果:


[a:13, b:8, c:12]


第二种方法,感觉思路更清晰,运算复杂度更小:getCountByName方法


public static List<Product> getCountByName(List<Product> productList) {

List<Product> targetList = new ArrayList<Product>();
Product product = null;

// 寻找该name的总count,然后删除该name
String name = null;
int count = 0;

while (productList.size() > 0) {
name = productList.get(0).getName();
count = 0;
for (int i = 0; i < productList.size(); i++) {
product = productList.get(i);
if (product.getName().equals(name)) {
count += product.getCount();
productList.remove(i);
i--;
}
}
targetList.add(new Product(name, count));
}

return targetList;
}



.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值