请教组合求和的算法 本帖最后由 fanlyfly 于 2011-08-12 16:25:18 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 package pdm.tool;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;public class MaterialInfo { private String type; private String model; private String weight; private String spec; /** * @return the model */ public String getModel() { return model; } /** * @param model the model to set */ public void setModel(String model) { this.model = model; } /** * @return the spec */ public String getSpec() { return spec; } /** * @param spec the spec to set */ public void setSpec(String spec) { this.spec = spec; } /** * @return the type */ public String getType() { return type; } /** * @param type the type to set */ public void setType(String type) { this.type = type; } /** * @return the weight */ public String getWeight() { return weight; } /** * @param weight the weight to set */ public void setWeight(String weight) { this.weight = weight; } @Override public boolean equals(Object obj) { if(obj == null || !(obj instanceof MaterialInfo)) { return false; } MaterialInfo bean = (MaterialInfo) obj; return new String(type+model+weight+spec).equals(bean.getType()+bean.getModel()+bean.getWeight()+bean.getSpec()); } @Override public int hashCode() { return new String(type + model + weight + spec).hashCode(); } public String toString() { StringBuffer string = new StringBuffer(); string.append("type = " + type); string.append(", model = " + model); string.append(", weight = " + weight); string.append(", spec = " + spec); string.append(""); return string.toString(); } public static void main(String[] args) { List<MaterialInfo> list = new ArrayList<MaterialInfo>(); Map<MaterialInfo, Integer> map = new HashMap<MaterialInfo, Integer>(); for(int i = 0; i < 10; i++) { MaterialInfo bean = new MaterialInfo(); bean.setType("1"); bean.setModel("a"); bean.setWeight("1"); bean.setSpec("x"); if(i == 4 || i == 8) { bean.setModel("b"); } if(i == 3 || i == 5) { bean.setSpec("i"); } list.add(bean); } for(int i = 0; i < list.size(); i++) { MaterialInfo bean = list.get(i); if(map.containsKey(bean)) { map.put(bean, map.get(bean) + 1); } else { map.put(bean, 1); } } Iterator<MaterialInfo> it = map.keySet().iterator(); while(it.hasNext()) { MaterialInfo bean = it.next(); int num = map.get(bean); System.err.println(bean + ", " + num); } }} 首先你得把数据放入list中static Map toMap(List<String> list) { Map<String,Integer> map = new HashMap<String,Integer>(); for (String s : list) { if (map.containsKey(s)) { map.put(s, Integer.parseInt(String.valueOf(map.get(s))) + 1); } else { map.put(s, 1); } } return map; } public static void main(String[] args) { List list=new ArrayList(); list.add("1234"); list.add("2sss"); list.add("1234"); list.add("2sss"); System.out.println(toMap(list)); }经测试无误{1234=2, 2sss=2} 如果是数据库检索的话,group by一下就好了select type,model,weight,spec,count(1) from your_table group by type,model,weight,specLZ传进后台的数据是什么类型?是List<String>,还是List<SomeClass>用1L的方法也不错,用Map来保存,不过前提是你的类要满足Set的相同对象比较条件,即你的类重写了equals和hashCode方法,如果没有重写那么做个sort按类型(type),型号(model),重量(weight),规格(spec)排序,然后遍历一下就可以了,,假设你的后台的数据是List<SomeClass> list;Comparator<SomeClass> comp = new Comparator<SomeClass>() { public int compare(SomeClass o1, SomeClass o2) { if (o1.getType().compareTo(o2.getType()) == 0) { if (o1.getModel().compareTo(o2.getModel()) == 0) { if (o1.getWeight().compareTo(o2.getWeight()) == 0) { return o1.getSpec().compareTo(o2.getSpec()); } return o1.getWeight().compareTo(o2.getWeight()); } return o1.getModel().compareTo(o2.getModel()); } return o1.getType().compareTo(o2.getType()); }};Collections.sort(list, comp);SomeClass last = list.get(0);Map<SomeClass, Integer> map = new HashMap<SomeClass>();int count = 0;for (SomeClass o : list) { if (comp.compare(last, o) != 0) { map.put(o, count); count = 1; last = o; } else { count++ }}for (Map.Entry<SomeClass, Integer> e : map.entrySet()) { SomeClass o = e.getKey(); System.out.printf("%s, %s, %s, %s, ", o.getType(), o.getModel(), o.getWeight(), o.getSpec()); System.out.printf("%d\n", e.getValue());} java面试题求救 过滤器问题,谁解决20分归谁,要代码 ireport again......怎样让title band占多页? 用TextPad编译一个简单的JAVA程序,提示标题为1.5\javac.exe的错误对话框, 咋办? 求算法一种 如何更改session 如何控制线程的数目 java怎么处理无限循环中不可到达的语句?请指教,谢谢 JB 能作出 .exe 文件吗??如何做? 没弄懂这2段代码的结果有何区别,有何玄机呢?try catch知识点 关于hibernate缓存清除的提问 url中的中文问题
package pdm.tool;import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;public class MaterialInfo {
private String type;
private String model;
private String weight;
private String spec; /**
* @return the model
*/
public String getModel() {
return model;
}
/**
* @param model the model to set
*/
public void setModel(String model) {
this.model = model;
}
/**
* @return the spec
*/
public String getSpec() {
return spec;
}
/**
* @param spec the spec to set
*/
public void setSpec(String spec) {
this.spec = spec;
}
/**
* @return the type
*/
public String getType() {
return type;
}
/**
* @param type the type to set
*/
public void setType(String type) {
this.type = type;
}
/**
* @return the weight
*/
public String getWeight() {
return weight;
}
/**
* @param weight the weight to set
*/
public void setWeight(String weight) {
this.weight = weight;
} @Override
public boolean equals(Object obj) {
if(obj == null || !(obj instanceof MaterialInfo)) {
return false;
} MaterialInfo bean = (MaterialInfo) obj; return new String(type+model+weight+spec).equals(bean.getType()+bean.getModel()+bean.getWeight()+bean.getSpec());
} @Override
public int hashCode() {
return new String(type + model + weight + spec).hashCode();
} public String toString() {
StringBuffer string = new StringBuffer(); string.append("type = " + type);
string.append(", model = " + model);
string.append(", weight = " + weight);
string.append(", spec = " + spec);
string.append(""); return string.toString();
} public static void main(String[] args) {
List<MaterialInfo> list = new ArrayList<MaterialInfo>();
Map<MaterialInfo, Integer> map = new HashMap<MaterialInfo, Integer>(); for(int i = 0; i < 10; i++) {
MaterialInfo bean = new MaterialInfo();
bean.setType("1");
bean.setModel("a");
bean.setWeight("1");
bean.setSpec("x"); if(i == 4 || i == 8) {
bean.setModel("b");
} if(i == 3 || i == 5) {
bean.setSpec("i");
}
list.add(bean);
} for(int i = 0; i < list.size(); i++) {
MaterialInfo bean = list.get(i); if(map.containsKey(bean)) {
map.put(bean, map.get(bean) + 1);
}
else {
map.put(bean, 1);
}
} Iterator<MaterialInfo> it = map.keySet().iterator(); while(it.hasNext()) {
MaterialInfo bean = it.next();
int num = map.get(bean); System.err.println(bean + ", " + num);
}
}
}
static Map toMap(List<String> list) {
Map<String,Integer> map = new HashMap<String,Integer>();
for (String s : list) {
if (map.containsKey(s)) {
map.put(s, Integer.parseInt(String.valueOf(map.get(s))) + 1);
} else {
map.put(s, 1);
}
} return map; }
public static void main(String[] args) {
List list=new ArrayList();
list.add("1234");
list.add("2sss");
list.add("1234");
list.add("2sss");
System.out.println(toMap(list));
}
经测试无误
{1234=2, 2sss=2}
select type,model,weight,spec,count(1)
from your_table
group by type,model,weight,specLZ传进后台的数据是什么类型?是List<String>,还是List<SomeClass>
用1L的方法也不错,用Map来保存,不过前提是你的类要满足Set的相同对象比较条件,即你的类重写了equals和hashCode方法,如果没有重写
那么做个sort按类型(type),型号(model),重量(weight),规格(spec)排序,然后遍历一下就可以了,,
假设你的后台的数据是
List<SomeClass> list;
Comparator<SomeClass> comp = new Comparator<SomeClass>() {
public int compare(SomeClass o1, SomeClass o2) {
if (o1.getType().compareTo(o2.getType()) == 0) {
if (o1.getModel().compareTo(o2.getModel()) == 0) {
if (o1.getWeight().compareTo(o2.getWeight()) == 0) {
return o1.getSpec().compareTo(o2.getSpec());
}
return o1.getWeight().compareTo(o2.getWeight());
}
return o1.getModel().compareTo(o2.getModel());
}
return o1.getType().compareTo(o2.getType());
}
};
Collections.sort(list, comp);
SomeClass last = list.get(0);
Map<SomeClass, Integer> map = new HashMap<SomeClass>();
int count = 0;
for (SomeClass o : list) {
if (comp.compare(last, o) != 0) {
map.put(o, count);
count = 1;
last = o;
} else {
count++
}
}
for (Map.Entry<SomeClass, Integer> e : map.entrySet()) {
SomeClass o = e.getKey();
System.out.printf("%s, %s, %s, %s, ", o.getType(), o.getModel(), o.getWeight(), o.getSpec());
System.out.printf("%d\n", e.getValue());
}