1.JS Map å List çç®åå®ç°ä»£ç
2.java的List集合里面放了Map,List<Map<String,Object>>,如何判定人名相同,就组合成一个对象?
3.List<Map<String, Object>>[{ a:1,b:2,c:3},{ a:1,b:2,c:3},{ a:2,b:2,c:3}]
JS Map å List çç®åå®ç°ä»£ç
æ¬ç¯æç« æ¯å¯¹å¨JSä¸MapåListçç®åå®ç°ä»£ç è¿è¡äºè¯¦ç»çåæä»ç» éè¦çæååèä¸ å¤å¶ä»£ç 代ç å¦ä¸: /* * MAP对象 å®ç°MAPåè½ * * æ¥å£ * size() è·åMAPå ç´ ä¸ªæ° * isEmpty() å¤æMAPæ¯å¦ä¸ºç©º * clear() å é¤MAPææå ç´ * put(key value) åMAPä¸å¢å å ç´ ï¼key value) * remove(key) å é¤æå®KEYçå ç´ æåè¿åTrue 失败è¿åFalse * get(key) è·åæå®KEYçå ç´ å¼VALUE 失败è¿åNULL * element(index) è·åæå®ç´¢å¼çå ç´ ï¼ä½¿ç¨element key element valueè·åKEYåVALUEï¼ å¤±è´¥è¿åNULL * containsKey(key) å¤æMAPä¸æ¯å¦å«ææå®KEYçå ç´ * containsValue(value) å¤æMAPä¸æ¯å¦å«ææå®VALUEçå ç´ * values() è·åMAPä¸ææVALUEçæ°ç»ï¼ARRAYï¼ * keys() è·åMAPä¸ææKEYçæ°ç»ï¼ARRAYï¼ * * ä¾å * var map = new Map(); * * map put("key" "value"); * var val = map get("key") * â¦â¦ * */ function Map() { this elements = new Array(); //è·åMAPå ç´ ä¸ªæ° this size = function() { return this elements length; }; //å¤æMAPæ¯å¦ä¸ºç©º this isEmpty = function() { return (this elements length < ); }; //å é¤MAPææå ç´ this clear = function() { this elements = new Array(); }; //åMAPä¸å¢å å ç´ ï¼key value) this put = function(_key _value) { this elements push( { key : _key value : _value }); }; //å é¤æå®KEYçå ç´ æåè¿åTrue 失败è¿åFalse this remove = function(_key) { var bln = false; try { for (i = ; i < this elements length; i++) { if (this elements[i] key == _key) { this elements splice(i ); return true; } } } catch (e) { bln = false; } return bln; }; //è·åæå®KEYçå ç´ å¼VALUE 失败è¿åNULL this get = function(_key) { try { for (i = ; i < this elements length; i++) { if (this elements[i] key == _key) { return this elements[i] value; } } } catch (e) { return null; } }; //è·åæå®ç´¢å¼çå ç´ ï¼ä½¿ç¨element key element valueè·åKEYåVALUEï¼ å¤±è´¥è¿åNULL this element = function(_index) { if (_index < || _index >= this elements length) { return null; } return this elements[_index]; }; //å¤æMAPä¸æ¯å¦å«ææå®KEYçå ç´ this containsKey = function(_key) { var bln = false; try { for (i = ; i < this elements length; i++) { if (this elements[i] key == _key) { bln = true; } } } catch (e) { bln = false; } return bln; }; //å¤æMAPä¸æ¯å¦å«ææå®VALUEçå ç´ this containsValue = function(_value) { var bln = false; try { for (i = ; i < this elements length; i++) { if (this elements[i] value == _value) { bln = true; } } } catch (e) { bln = false; } return bln; }; //è·åMAPä¸ææVALUEçæ°ç»ï¼ARRAYï¼ this values = function() { var arr = new Array(); for (i = ; i < this elements length; i++) { arr push(this elements[i] value); } return arr; }; //è·åMAPä¸ææKEYçæ°ç»ï¼ARRAYï¼ this keys = function() { var arr = new Array(); for (i = ; i < this elements length; i++) { arr push(this elements[i] key); } return arr; }; } å¤å¶ä»£ç 代ç å¦ä¸: /** * jså®ç°list * */ function List() { this value = []; /* æ·»å */ this add = function(obj) { return this value push(obj); }; /* å¤§å° */ this size = function() { return this value length; }; /* è¿åæå®ç´¢å¼çå¼ */ this get = function(index) { return this value[index]; }; /* å é¤æå®ç´¢å¼çå¼ */ this remove = function(index) { this value splice(index ); return this value; }; /* å é¤å ¨é¨å¼ */ this removeAll = function() { return this value = []; }; /* æ¯å¦å å«æ个对象 */ this constains = function(obj) { for ( var i in this value) { if (obj == this value[i]) { return true; } else { continue; } } return false; }; /* æ¯å¦å å«æ个对象 */ this getAll = function() { var allInfos = ; for ( var i in this value) { if(i != (value length )){ allInfos += this value[i]+" "; }else{ allInfos += this value[i]; } } alert(allInfos); return allInfos += this value[i]+" ";; }; } lishixinzhi/Article/program/Java/JSP//
java的List集合里面放了Map,List<Map<String,Object>>,如何判定人名相同,就组合成一个对象?
受楼上两位老哥启发,代码如下public class Test2 {
public static void main(String[] args) {
Map<String, String> map1 = new MyMap<>();
map1.put("name", "张三");
map1.put("公司", "腾讯");
Map<String, String> map2 = new MyMap<>();
map2.put("name", "李四");
map2.put("公司", "阿里");
Map<String, String> map3 = new MyMap<>();
map3.put("name", "王五");
map3.put("公司", "百度");
Map<String, String> map4 = new MyMap<>();
map4.put("name", "哈哈哈");
map4.put("公司", "嘿嘿嘿");
Map<String, String> map5 = new MyMap<>();
map5.put("name", "张三");
map5.put("地址", "四川");
Map<String, String> map6 = new MyMap<>();
map6.put("name", "李四");
map6.put("地址", "湖南");
Map<String, String> map7 = new MyMap<>();
map7.put("name", "王五");
map7.put("地址", "广东");
List<Map<String, String>> list = new ArrayList<>();
list.add(map1);
list.add(map2);
list.add(map3);
list.add(map4);
list.add(map5);
list.add(map6);
list.add(map7);
//lambda遍历输出
list.forEach(map -> System.out.println(map.toString()));
System.out.println("==========================");
Map<String, String> m1;
Map<String, String> m2;
/* 遍历方式:
* 将集合中当前元素m1与它后一个元素m2比较,若name相同,联盟馆源码则将m2中的内容put到m1中,同时删除m2
* 指针j向右偏移一位,阅读源码的好处重复上一步,系统卡劵源码直至所有元素都作为m1过一次
* */
for (int i = 0; i < list.size() - 1; i++) {
for (int j = i; j < list.size() - 1; j++) {
m1 = list.get(i);
m2 = list.get(j + 1);
//若m1与m2的name相同
if (m1.equals(m2)) {
//则将m2中的内容put到m1中
m2.forEach(m1::put);//lambda遍历
//删除m2
list.remove(j + 1);
}
}
}
System.out.println("==========================");
//lambda遍历输出
list.forEach(map -> System.out.println(map.toString()));
}
@SuppressWarnings("unchecked")
static class MyMap<K, V> extends LinkedHashMap<K, V> {
/
*** 重写equals方法,this为m1,o为m2
* 这里其实需要instanceof来检查泛型的,但是我不会使用instanceof来检查泛型,
* 不能直接使用if ( o instanceof LinkedHashMap<String, String>),这样是
* 编译失败的,所以我没办法只能用try弄一下
*
* @param o m2
* @return 若m1与m2的name相同,返回true
*/
@Override
public final boolean equals(Object o) {
if (o == this)
return true;
try {
LinkedHashMap<String,.net系统框架 源码 String> map = (LinkedHashMap<String, String>) o;
if (this.get("name").equals(map.get("name"))) {
return true;
}
} catch (ClassCastException e) {
e.printStackTrace();
}
return false;
}
}
}
我怕代码挤在一起,截图给你:
定义myMap,放入数据
map入集合,c tcpip源码下载遍历打印
执行比较,最后再输出一次集合
定义内部类MyMap,重写equals方法
运行结果:
List<Map<String, Object>>[{ a:1,b:2,c:3},{ a:1,b:2,c:3},{ a:2,b:2,c:3}]
List> list1 = new ArrayList>(); Map m1 = new HashMap(); m1.put("a", 3); m1.put("b", 1); m1.put("c", 1); m1.put("d", 2); list1.add(m1); Map m2 = new HashMap(); m2.put("a", ); m2.put("b", ); m2.put("c", ); m2.put("d", ); list1.add(m2); List> list2 = new ArrayList>(); int a = 0; int b = 0; int c = 0; int d = 0; for(int i=0; i m = list1.get(i); a += m.get("a"); b += m.get("b"); c += m.get("c"); d += m.get("d"); } Map m = new HashMap(); m.put("a", a); m.put("b", b); m.put("c", c); m.put("d", d); list2.add(m);