这是我调通了的程序 你要是哪点不理解 可以发给我 或者自己先debug一下
package testDemo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;
public class Test {
public static void main(String[] args) {
Vector v = getV();
Map amtMap = getMixDateAmt(v);
double sum = 0;
Iterator iter = amtMap.keySet().iterator();
while (iter.hasNext()) {
sum += Double.parseDouble((String) amtMap.get(iter.next()));
System.out.println(sum);
}
System.out.println(sum);
}
private static Vector getV() {
// TODO Auto-generated method stub
Vector vector = new Vector();
Map<String, String> map1 = new HashMap<String, String>();
Map<String, String> map2 = new HashMap<String, String>();
Map<String, String> map3 = new HashMap<String, String>();
Map<String, String> map4 = new HashMap<String, String>();
Map<String, String> map5 = new HashMap<String, String>();
map1.put("seqno", "0");
map1.put("amt", "1000");
map1.put("id", "1");
map1.put("date", " 20101010");
map2.put("seqno", "1");
map2.put("amt", "1000");
map2.put("id", "2");
map2.put("date", " 20101010");
map3.put("seqno", "2");
map3.put("amt", "1000");
map3.put("id", "1");
map3.put("date", " 20101009");
map4.put("seqno", "3");
map4.put("amt", "1000");
map4.put("id", "1");
map4.put("date", " 20101008");
map5.put("seqno", " 1");
map5.put("amt", "1000");
map5.put("id", "2");
map5.put("date", " 20101013");
vector.add(map1);
vector.add(map2);
vector.add(map3);
vector.add(map4);
vector.add(map5);
return vector;
}
private static Map getMixDateAmt(Vector v) {
Map dateMap = new HashMap();
Map amtMap = new HashMap();
String id1 = (String) ((Map) v.get(0)).get("id");
String date1 = (String) ((Map) v.get(0)).get("date");
String amt1 = (String) ((Map) v.get(0)).get("amt");
dateMap.put(id1, date1);
amtMap.put(id1, amt1);
for (int i = 0; i < v.size(); i++) {
Map m = (Map) v.get(i);
String id2 = (String) m.get("id");
String date2 = (String) m.get("date");
String amt2 = (String) m.get("amt");
if (!dateMap.containsKey(id2)) {
dateMap.put(id2, date2);
amtMap.put(id2, amt2);
} else {
if (date2.compareTo((String) dateMap.get(id2)) < 0) {
dateMap.put(id2, date2);
amtMap.put(id2, amt2);
}
}
}
return amtMap;
}
public static ArrayList getDistinctId(Vector v) {
String id1 = (String) ((Map) v.get(0)).get("id");
ArrayList list = new ArrayList();
list.add(id1);
for (int i = 0; i < v.size(); i++) {
Map m2 = (Map) v.get(i);
String id2 = (String) m2.get("id");
if (!id1.equals(id2) && !list.contains(id2)) {
list.add(id2);
}
}
System.out.println(list.toString());
return list;
}
}
|