Map<String,String> map = new HashMap<String,String>();
map.put("1","v1");
map.put("2","v1");
map.put("3","v1");
map.put("4","v2");
map.put("5","v2");
map.put("6","v2");
Map<String,ArrayList<String>> new_map = new HashMap<String,ArrayList<String>>();
Iterator<Entry<String,String>> iter = map.entrySet().iterator();
while(iter.hasNext()){
Entry<String,String> entry = iter.next();
String key = entry.getKey();
String value = entry.getValue();
if (new_map.get(key) == null) {
ArrayList<JSONObject> arrList= new ArrayList<JSONObject>();
evtDocList.add(key);
new_map.put(value, arrList);
} else {
if (!new_map.get(value).contains(key))
{
new_map.get(value).add(key);
}
}
}
这样就把具有相同value的key赋值给了arrayList,从而生成以value作为key的新map
但是,如果arrayList中需要不同类型比如String、Integer、double等,那么就需要使用泛型来支持。
public class test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JsonUtils utils = JsonUtils.getInstance();
Map<String, ArrayList<String>> t1 = new HashMap<String, ArrayList<String>>();
Map<String, ArrayList<Integer>> t2 = new HashMap<String, ArrayList<Integer>>();
utils.jsonMap(t2, "a", 2);
utils.jsonMap(t2, "a", 3);
utils.jsonMap(t2, "a", 4);
utils.jsonMap(t1, "b", "t1");
utils.jsonMap(t1, "b", "t2");
utils.jsonMap(t1, "b", "t3");
Iterator<Entry<String,ArrayList<Integer>>> iter = t2.entrySet().iterator();
while(iter.hasNext()){
Entry<String,ArrayList<Integer>> entry = iter.next();
String key = entry.getKey();
ArrayList<Integer> value = entry.getValue();
for(Integer str : value){
System.out.println(key+"========="+str);
}
}
Iterator<Entry<String,ArrayList<String>>> iter2 = t1.entrySet().iterator();
while(iter2.hasNext()){
Entry<String,ArrayList<String>> entry = iter2.next();
String key = entry.getKey();
ArrayList<String> value = entry.getValue();
for(String str : value){
System.out.println(key+"========="+str);
}
}
}
}
public final class JsonUtils {
private static JsonUtils INSTANCE = new JsonUtils();
private JsonUtils()
{
}
public static JsonUtils getInstance(){
return INSTANCE;
}
public <T> Map<String, ArrayList<T>> jsonMap(Map<String, ArrayList<T>> map,String key,T value){
if (map.get(key) == null) {
ArrayList<T> eIdList = new ArrayList<T>();
eIdList.add(value);
map.put(key, eIdList);
} else {
if (!map.get(key).contains(value)) {
map.get(key).add(value);
}
}
return map;
}
}
分享到:
相关推荐
List<Map<String, String>> userList = new ArrayList<>(); for (Map<String, String> user : userList) { String userName = user.get("name"); String userEmail = user.get("email"); // 处理每个用户的姓名和...
List<Map<String, Object>> list = new ArrayList<>(); for (int i = 0; i < jsonArray.length(); i++) { JSONObject obj = jsonArray.getJSONObject(i); Map<String, Object> itemMap = new HashMap<>(); for ...
List<Map<String,Object>> results = new ArrayList<Map<String,Object>>(); ResultSetMetaData rsmd = rs.getMetaData(); int colCount = rsmd.getColumnCount(); List<String> colNameList = new ArrayList...
标题中提到的“JCF(List、Set、Map)学习,实现了<key,value>按value排序”是一个关键点,我们将深入探讨这个话题。 首先,让我们了解List、Set和Map的区别。List是有序的集合,允许重复元素,并且可以保持插入顺序...
List<Map.Entry<String, String>> list = new ArrayList<>(map.entrySet()); Collections.sort(list, new Comparator<Map.Entry<String, String>>() { public int compare(Map.Entry<String, String> obj1, Map....
List<User> userList = new ArrayList<>(); userList.add(new User("Bob", 30)); userList.add(new User("Charlie", 35)); String xmlList = xstream.toXML(userList); ``` 输出的XML会将List中的每个元素...
<entry key="key1" value="value1"/> <entry key="key2" value="value2"/> </map> </property> </bean> ``` 此外,Spring还提供了集合的工具类`org.springframework.util.CollectionUtils`,提供了诸如检查集合...
memcachedList = new ArrayList<>(data.values()); } return SUCCESS; } } ``` 在这个示例中,`MemcachedAction`继承了`BaseAction`类,并实现了与Memcached交互的方法。`test()`方法演示了如何将数据存入缓存...
TreeMap<Province, ArrayList<City>> map = new TreeMap<>(); ArrayList<City> bj = new ArrayList<>(); bj.add(new City(1,"北京市")); ArrayList<City> tj = new ArrayList<>(); tj.add(new City(4,"天津...
3. `<map>`:用于创建HashMap实例,可以设置键值对,键用`<key>`标签,值用`<value>`标签。 4. `<props>`:用于创建Properties对象,常用来存储配置属性,键值对的形式与Java Properties文件相同。 例如,如果你有...
<prop key="key1">value1</prop> <prop key="key2">value2</prop> <prop key="key3">value3</prop> </props> </property> </bean> ``` 以上就是在 Spring 框架中实体类复杂属性的注入方法,通过这些标签可以...
Map<String, List<Interest>> map = new HashMap<>(); map.put("1", inList); map.put("2", inList2); ``` 4. **Map<String, String[]>**: - 获取键以及对应的字符串数组的每个元素: ```jsp <s:iterator ...
<entry key="key1" value-ref="bean1"/> <entry key="key2" value-ref="bean2"/> </map> </constructor-arg> </bean> ``` 这里,`container` Bean的Map将有两项,键`key1`对应`bean1`,键`key2`对应`bean2`。 ...
1. 创建Map对象:`Map<String, Integer> map = new HashMap<>();` 2. 插入键值对:`map.put("key", 1);` 3. 获取值:`int value = map.get("key");` 4. 删除键值对:`map.remove("key");` 5. 检查键是否存在:`...
3. `<map>`:用于注入`java.util.Map`对象,你可以通过`key`和`value`属性定义键值对。例如: ```xml <map> <entry key="key1" value="value1"/> <entry key-ref="beanKey1" value-ref="beanValue1"/> </map> ``` ...
private List<Employee> employees = new ArrayList<>(); // getters and setters } ``` **讨论** 多对一关联是非常常见的关系类型。它可以用来模拟现实世界中的很多关系,比如员工和部门之间的关系等。 #### ...
List<Map.Entry<String, String>> entryList = new ArrayList<Map.Entry<String, String>>(oriMap.entrySet()); Collections.sort(entryList, new Comparator<Map.Entry<String, String>>() { public int compare...
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Map<String, Object> map1 = new HashMap<String, Object>(); map1.put("name", "p"); map1.put("cj", "5"); Map<String, Object> map2 = ...
List<Map.Entry<String, String>> list = new ArrayList<>(map.entrySet()); // 定义一个Comparator,用于value排序 Collections.sort(list, new Comparator<Map.Entry<String, String>>() { @Override public...
List<Map.Entry<String, Integer>> list = new ArrayList<>(map.entrySet()); Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() { @Override public int compare(Map.Entry<String, Integer>...