JAVABEAN如下
public class InProperties {
   
     private String ID;
     private String properties;
     private String value;
}然后有一个List包含这些数据,List list = new ArrayList();
list.add(new InProperties(“2222”, "name", "you") );
list.add(new InProperties(“2222”, "phone", "139") );
list.add(new InProperties(“2222”, "email", "[email protected]") );
list.add(new InProperties(“3333”, "name", "she") );
list.add(new InProperties(“3333”, "phone", "138") );

LIST的个数是没限制的, 如何把这个LIST 变成
一个这样的HASHMAP或LIST
hashMap.put("222","<td>you</td><td>139</td><td>[email protected]</td>");
hashMap.put("333","<td>she</td><td>138</td><td>..</td>");
..................
请用最效的算法解这道题

解决方案 »

  1.   

    看不出能多有效了,我只知道我最笨的就是用循环来遍历这个list,得到一个一个的InProperties,然后就是hashMap.put(bean.getxxx," <td>"+bean.getyyyy+"</td> <td>"+bean.getjjjjj+"</td> <td>"+bean.getpppppppp+"</td>");如果说这个String的算法不太好,那你就来一个StringBuffer吧
      

  2.   

    List list = new ArrayList();
    list.add(new InProperties("2222", "name", "you"));
    list.add(new InProperties("2222", "phone", "139"));
    list.add(new InProperties("2222", "email", "[email protected]"));
    list.add(new InProperties("3333", "name", "she"));
    list.add(new InProperties("3333", "phone", "138"));
    Map map = new HashMap();
    for (int i = 0, size = list.size(); i < size; i++) {
    InProperties is = (InProperties) list.get(i);
    String str = (String) map.get(is.getId());
    if (str == null) {
    map.put(is.getId(), "<td>" + is.getProperties() + "</td> ");
    } else {
    map.put(is.getId(), str + "<td>" + is.getProperties()
    + "</td> ");
    }
    }
    //下面是测试上面代码的
    Iterator iterator=map.keySet().iterator();
    while(iterator.hasNext()){
    String key=(String)iterator.next();
    System.out.println(map.get(key));
    }
      

  3.   

    只遍历一次,hashcode的查找速度lz可以放心,毋庸置疑。
      

  4.   

    没必要这么做啊,直接在页面上使用iterator标签就可以了。
      

  5.   

    三楼的哥们儿。好像不太对吧。下面这个:貌似可以import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
     class InProperties {
      
        private String ID; 
        private String properties; 
        private String value; 
       public InProperties(String ID, String properties, String value) {
         this.ID = ID;
         this.properties = properties;
         this.value = value;
        }
       
    /**
     * @param args
     */
    public static void main(String[] args) {
    List list = new ArrayList();
            list.add(new InProperties("2222", "name", "you"));
            list.add(new InProperties("2222", "phone", "139"));
            list.add(new InProperties("2222", "email", "[email protected]"));
            list.add(new InProperties("3333", "name", "she"));
            list.add(new InProperties("3333", "phone", "138"));
            Map map = new HashMap();
            for (int i = 0, size = list.size(); i < size; i++) {
                InProperties inPro = (InProperties) list.get(i);
                String strID = inPro.ID;
                if(map.containsKey(strID)){
                 map.put(strID, map.get(strID)+ "<td>" + inPro.value + "</td> ");
                } else {
                    map.put(strID,"<td>" + inPro.value + "</td> ");
                }
            }
            //下面是测试上面代码的
            Iterator iterator=map.keySet().iterator();
            while(iterator.hasNext()){
                String key=(String)iterator.next();
                System.out.println(map.get(key));
            } }}
      

  6.   

    我想学JAVA!!
      

  7.   

    要在页面显示 没必要这样做。加到list 方法返回list就是了。外面接受下。<logic:iterator >
    如果为了自己控制 可以用对象的get() set()  最后list.add(对象实例)。
      

  8.   

    请问哪里不对???
    你的写的不和我一样吗,另外你相当于进行了两次hashcode查找
    get一次containsKey一次。
      

  9.   

    6楼的那个应当是和3楼的算法差不多,不过6楼修改了两点:(String) map.get(is.getId()); ==〉map.containsKey(strID)){ 
    (后者的速度我感觉会更快一点)
    is.getProperties() ==〉 inPro.value 
    (这个到底哪个对,从楼主提供的资料看不出来,从顺序上来讲,后者对)
      

  10.   

    这个keySet()方法返回一个此映射中包含的键的 set 视图。什么意思?
      

  11.   

    对于是value还是properties属性,这个问题不具有讨论价值get两次快还是一次快,关键看源码  public boolean containsKey(Object key) {
            Object k = maskNull(key);
            int hash = hash(k);
            int i = indexFor(hash, table.length);
            Entry e = table[i]; 
            while (e != null) {
                if (e.hash == hash && eq(k, e.key)) 
                    return true;
                e = e.next;
            }
            return false;
        }
        public V get(Object key) {
            Object k = maskNull(key);
            int hash = hash(k);
            int i = indexFor(hash, table.length);
            Entry<K,V> e = table[i]; 
            while (true) {
                if (e == null)
                    return null;
                if (e.hash == hash && eq(k, e.key)) 
                    return e.value;
                e = e.next;
            }
        }
    看源码可以看出containsKey和get是一样的,只是没有return引用而已。所以一次肯定比两次快。
      

  12.   

    List list = new ArrayList(); 
            list.add(new InProperties("2222", "name", "you")); 
            list.add(new InProperties("2222", "phone", "139")); 
            list.add(new InProperties("2222", "email", "[email protected]")); 
            list.add(new InProperties("3333", "name", "she")); 
            list.add(new InProperties("3333", "phone", "138")); 
            Map map = new HashMap(); 
            for (int i = 0, size = list.size(); i < size; i++) { 
                InProperties inPro = (InProperties) list.get(i); 
                String strID = inPro.ID; 
                if(map.containsKey(strID)){ 
                map.put(strID, map.get(strID)+ " <td>" + inPro.value + " </td> "); 
                } else { 
                    map.put(strID," <td>" + inPro.value + " </td> "); 
                } 
            } 
      

  13.   

    提供不同的思路。也是O(n)的复杂度。import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.Map.Entry;public class InPropertiesTest { public Map<String, String> changeToHashMap(List<InProperties> list) {
    Map<String, String> map = new HashMap<String, String>();
    int index = 0;
    InProperties oldInProperties =null;
    String info = "";
    for(InProperties properties : list) {
    if(index == 0) oldInProperties = properties;
    if(properties.getID() != oldInProperties.getID()) {
    map.put(oldInProperties.getID(), info);
    info = "";
    oldInProperties = properties;
    }
    info += "<td>" + properties.getValue() + "</td>";
    if(index == list.size()-1) {
    map.put(properties.getID(), info);
    }
    index ++;
    }
    return map;
    }
     
    public List<InProperties> init() {
    List<InProperties> list = new ArrayList<InProperties>(); 
    list.add(new InProperties("2222", "name", "you"));
        list.add(new InProperties("2222", "phone", "139"));
        list.add(new InProperties("2222", "email", "[email protected]"));
        list.add(new InProperties("3333", "name", "she"));
        list.add(new InProperties("3333", "phone", "138"));
        return list;
    } public static void main(String[] args) {
    InPropertiesTest test = new InPropertiesTest();
    Map<String, String> map = test.changeToHashMap(test.init());
    for(Entry<String, String> entry : map.entrySet()) {
    String key = entry.getKey();
    String value = entry.getValue();
    System.out.println(key+":"+value);
    } }

    class InProperties {
        private String ID; 
    private String properties; 
    private String value;

    public InProperties(){

    }

    public InProperties(String id, String properties, String value) {
    super();
    ID = id;
    this.properties = properties;
    this.value = value;
    }
    public String getID() {
    return ID;
    }
    public void setID(String id) {
    ID = id;
    }
    public String getProperties() {
    return properties;
    }
    public void setProperties(String properties) {
    this.properties = properties;
    }
    public String getValue() {
    return value;
    }
    public void setValue(String value) {
    this.value = value;
    }  }}
      

  14.   


    package com.yaray.test;import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    public class CsdnTest {
        
        /**
         * [method function description]
         * 
         * @param args
         */
        public static void main(String[] args) {        List list = new ArrayList();
            list.add(new InProperties("2222", "name", "you") );
            list.add(new InProperties("2222", "phone", "139") );
            list.add(new InProperties("2222", "email", "[email protected]") );
            list.add(new InProperties("3333", "name", "she") );
            list.add(new InProperties("3333", "phone", "138") );
            
            String keyKey = "InProperties.key", keyName = "name", keyPhone="phone", keyEmail="email";
            
            List lstResult = new ArrayList();
            Map tmpFilterRepeatKey = new HashMap();
            for (int i=0, len=list.size(); i<len; i++) {
                InProperties inProp = (InProperties) list.get(i);
                
                Object obj = tmpFilterRepeatKey.get(inProp.key);
                if (obj!=null) {
                    Map prop = (Map) obj;
                    prop.put(inProp.name, inProp.value);
                } else {
                    Map prop = new HashMap(4);
                    
                    prop.put(keyKey, inProp.key);
                    prop.put(inProp.name, inProp.value);
                    
                    tmpFilterRepeatKey.put(inProp.key, inProp);
                    lstResult.add(prop);
                }
            }
            
            String tmpTdStart = "<td>", tmpTdEnd = "</td>";
            
            Map mapResult = new HashMap(lstResult.size());
            for (int i=0, len=lstResult.size(); i<len; i++) {
                Map prop = (Map)  list.get(i);
                
                String keyValue = (String) prop.get(keyKey);
                
                String name = (String) prop.get(keyName);
                String phone = (String) prop.get(keyPhone);
                String email = (String) prop.get(keyEmail);
                
                StringBuffer sb = new StringBuffer();
                sb.append(tmpTdStart).append(name).append(tmpTdEnd);
                sb.append(tmpTdStart).append(phone).append(tmpTdEnd);
                sb.append(tmpTdStart).append(email).append(tmpTdEnd);
                
                mapResult.put(keyValue, sb.toString());
            }    }}class InProperties {
        String key, name, value;
        InProperties(String key, String name, String value){
            this.key = key;
            this.name = name;
            this.value = value;
        }
    }
      

  15.   

    纠正一行代码:tmpFilterRepeatKey.put(inProp.key, inProp); // old: error
    ==>
    tmpFilterRepeatKey.put(inProp.key, prop); // new
      

  16.   

    再纠正一行代码:
    Map prop = (Map)  list.get(i);// old: error
    ==>
    Map prop = (Map)  lstResult.get(i);// new
      

  17.   

    有意思,本身properties得数据结构我感觉设计就不合理,还要转换成hashmap   组装list就不要了 直接组装hashmap不是更快吗嘿嘿
      

  18.   

    我只能提示一句,既然数量很多,而且重复的多,那么可以用
    Map<String,StringBuilder> 不要用 Map<String,String>其它的没啥了。map的建立过程没啥特殊的。都一样的。
      

  19.   

    6楼containsKey一次后面接着又get了一次注意看这
    if(map.containsKey(strID)){ 
                map.put(strID, map.get(strID)+
      

  20.   

    3楼的
        String str = (String) map.get(is.getId());
                if (str == null) {
                    map.put(is.getId(), "<td>" + is.getProperties() + "</td> ");
                } else {
                    map.put(is.getId(), str + "<td>" + is.getProperties()
                            + "</td> ");
                }
      

  21.   

    我也写了一个,参考小孙的,不过我用了StringBuilder,看上去更有效一些。
    原文地址:从自定义的列表转换为Map类型
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;/**
     * 从自定义的列表转换为Map类型。
     * 
     * @author 赵学庆,Java世纪网(java2000.net)
     * 
     */
    public class T {
      public static void main(String[] args) {
        List<InProperties> list = new ArrayList<InProperties>();
        list.add(new InProperties("2222", "name", "you"));
        list.add(new InProperties("2222", "phone", "139"));
        list.add(new InProperties("2222", "email", "[email protected]"));
        list.add(new InProperties("3333", "name", "she"));
        list.add(new InProperties("3333", "phone", "138"));
        Map<String, StringBuilder> map = new HashMap<String, StringBuilder>();
        StringBuilder b = null;
        for (int i = 0, size = list.size(); i < size; i++) {
          InProperties is = (InProperties) list.get(i);
          b = map.get(is.getID());
          if (b == null) {
            map.put(is.getID(), new StringBuilder("<td>" + is.getValue() + "</td>"));
          } else {
            b.append("<td>" + is.getValue() + "</td>");
          }
        }
        Iterator<String> iterator = map.keySet().iterator();
        while (iterator.hasNext()) {
          String key = iterator.next();
          System.out.println(key + "=" + map.get(key));
        }
      }}class InProperties {
      InProperties(String ID, String properties, String value) {
        this.ID = ID;
        this.properties = properties;
        this.value = value;  }  private String ID;  public String getID() {
        return ID;
      }  public void setID(String id) {
        ID = id;
      }  public String getProperties() {
        return properties;
      }  public void setProperties(String properties) {
        this.properties = properties;
      }  public String getValue() {
        return value;
      }  public void setValue(String value) {
        this.value = value;
      }  private String properties;
      private String value;
    }
      

  22.   


    这个InProperties类设计的是不合理,应当设计一个PO的类,如Person,我也写了一个代码如下:import java.util.*;class Person {
    private String name;
    private String phone;
    private String email;
    public Person(String name, String phone, String email){
    this.name = name;
    this.phone = phone;
    this.email = email;
    }
    public String getEmail() {
    return email;
    }
    public void setEmail(String email) {
    this.email = email;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public String getPhone() {
    return phone;
    }
    public void setPhone(String phone) {
    this.phone = phone;
    }
    public String toString() {
    return "<td>"+name+"</td><td>"+phone+"</td><td>"+email+"</td>";
    }
    }
    public class Test {
        public static void main(String[] args){
         List<Person> persons = new ArrayList<Person>();
         persons.add(new Person("you","139","[email protected]"));
         persons.add(new Person("she","138","[email protected]"));
         for(Person p : persons) {
         System.out.println(p);
         }
        }
    }
      

  23.   

    楼上几位,老紫竹的代码,我觉得最好。
    但是,要提醒楼主一下:
    如果List里面相同的id(如:“222”),properties必须是按name,phone,email的顺序的,
    如果不是这个顺序,最好还是自己写一个辅助类用来完成,数据结构的转换。