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>");
..................
请用最效的算法解这道题
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>");
..................
请用最效的算法解这道题
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));
}
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));
} }}
如果为了自己控制 可以用对象的get() set() 最后list.add(对象实例)。
你的写的不和我一样吗,另外你相当于进行了两次hashcode查找
get一次containsKey一次。
(后者的速度我感觉会更快一点)
is.getProperties() ==〉 inPro.value
(这个到底哪个对,从楼主提供的资料看不出来,从顺序上来讲,后者对)
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引用而已。所以一次肯定比两次快。
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> ");
}
}
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;
} }}
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;
}
}
==>
tmpFilterRepeatKey.put(inProp.key, prop); // new
Map prop = (Map) list.get(i);// old: error
==>
Map prop = (Map) lstResult.get(i);// new
Map<String,StringBuilder> 不要用 Map<String,String>其它的没啥了。map的建立过程没啥特殊的。都一样的。
if(map.containsKey(strID)){
map.put(strID, map.get(strID)+
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> ");
}
原文地址:从自定义的列表转换为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;
}
这个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);
}
}
}
但是,要提醒楼主一下:
如果List里面相同的id(如:“222”),properties必须是按name,phone,email的顺序的,
如果不是这个顺序,最好还是自己写一个辅助类用来完成,数据结构的转换。