关于一个java算法的问题(急)
List mainList=new ArrayList();
String[] first={ "key1 ", "value1 "};
String[] second={ "key2 ", "value2 "};
String[] third={ "key3 ", "value3 "};
String[] four={ "key1 ", "value12 "};
String[] five={ "key2 ", "value22 "};
String[] six={ "key1 ", "value13 "};
String[] seven={ "key4 ", "value4 "};
String[] eight={ "key3 ", "value31 "};
String[] nine={ "key1 ", "value14 "};
String[] ten={ "key5 ", "value5 "};
mainList.add(first);
mainList.add(second);
mainList.add(third);
mainList.add(four);
mainList.add(five);
mainList.add(six);
mainList.add(seven);
mainList.add(eight);
mainList.add(nine);
mainList.add(ten);
Map omap=new HashMap();
我现在要把list里的属于同一个key(如“key1”)的value值(如: "value1 ", "value12 ", "value13 ", "value14 ")放在同一个list里,然后在放到omap里。最后omap里应该是 key value(List)
key1 value1,value12,value13,value14
key2 value2,value22
key3 value3,value31
key4 value4
key5 value5
请教各位有什么好的算法可以实现,谢谢!
另望有关于java算法的教材下载的话希望能给我发一本,我的邮箱是bernerhuang@ncsi.com.cn
------解决方案--------------------up
------解决方案--------------------呵呵,昨天才写过的一个算法,伪代码如下:
for(each ml in mainList){
List l = omap.get(ml[0]);
if(l==null){
l=new ArrayList();
omap.put(ml[0],l);
}
l.add(ml[1]);
}
------解决方案--------------------loop(mainList){
1. get: key, value from mainList;
2. get the List(list) from omap;
3. if(list == null) list = new List();
4. list.add(value);
5. omap.put(key,list);
}
------解决方案--------------------HashtableTest.java
======================
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
public class HashtableTest {
private ArrayList mainList = new ArrayList();
private Hashtable omap=new Hashtable();
public ArrayList getMainList() {
return mainList;
}
public void setMainList(ArrayList mainList) {
this.mainList = mainList;
}
public Hashtable getOmap() {
return omap;