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向右偏移一位,iapp导入源码之后重复上一步,小猪多开器源码直至所有元素都作为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,区块链源码开放 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入集合,javaweb商城项目 源码遍历打印
执行比较,最后再输出一次集合
定义内部类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);
2024-12-24 09:10809人浏览
2024-12-24 09:081067人浏览
2024-12-24 08:472399人浏览
2024-12-24 08:19856人浏览
2024-12-24 07:33279人浏览
2024-12-24 07:201866人浏览
炎炎夏日,很多人都會習慣來杯手搖飲,但您知道喝到的鮮奶茶飲品,用的鮮奶是什麼品牌嗎?消基會調查,市面上的手搖飲、咖啡店,還有早午餐店,發現僅有10.8%的店家,會在品牌官網或菜單上揭露;調查還發現,像
1.一套完整的视频直播聊天室源码怎么开发?2.直播源码是什么意思?3.什么是直播源码4.直播源码是什么?5.请问谁有微信直播系统的视频直播系统源码吗 属于网络直播系统平台的直播系统源码一套完整的视频直
1.开发一套壹加壹拼团分销代理分红系统APP要多少钱?2.医药行业比较不错的软件厂商有哪些呀?开发一套壹加壹拼团分销代理分红系统APP要多少钱? 开发一个APP需要多少钱。其实,这个价格的计算公式