关于一个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;
}
public void setOmap(Hashtable omap) {
this.omap = omap;
}
public static void main(String[] args){
HashtableTest hashtableTest = new HashtableTest();
Option[] options = new Option[10];
options[0] = new Option( "key1 ", "value1 ");
options[1]= new Option( "key2 ", "value2 ");
options[2] = new Option( "key3 ", "value3 ");
options[3]= new Option( "key1 ", "value12 ");
options[4] = new Option( "key2 ", "value22 ");
options[5] = new Option( "key1 ", "value13 ");
options[6] = new Option( "key4 ", "value4 ");
options[7] = new Option( "key3 ", "value31 ");
options[8] = new Option( "key1 ", "value14 ");
options[9] = new Option( "key5 ", "value5 ");
for(int i = 0;i < options.length;i++)
{
Option option = (Option)options[i];
hashtableTest.getMainList().add(options[i]);
option = null;
}
boolean flag;
for(int i = 0;i < hashtableTest.getMainList().size();i++){
Option option = (Option)(hashtableTest.getMainList().get(i));
flag = hashtableTest.getOmap().containsKey(option.getKey());
if( !flag ){
ArrayList list = new ArrayList();
list.add(option.getValue());
hashtableTest.getOmap().put(option.getKey(), list);
}
else{
ArrayList list = (ArrayList) hashtableTest.getOmap().get(option.getKey());
list.add(option.getValue());
}
}
printall(hashtableTest.getOmap());
}
public static void printall(Hashtable ht){
Enumeration en = ht.keys();
while(en.hasMoreElements()){
Object key_num = en.nextElement();
System.out.print( "DEBUG: " + key_num);
System.out.print( " = ");
System.out.println(ht.get(key_num));
}
}
}
class Option{
private String key;
private String value;
public void setKey(String key){
this.key = key;
}
public String getKey(){
return this.key;
}
public void setValue(String value){
this.value = value;
}
public String getValue(){
return this.value;
}
public Option(String key,String value){
this.key = key;
this.value = value;
}
}
eclipse下编译执行通过