map里面的list怎么js中遍历list中的map

随笔 - 15&
&&&&&&&&&&&
1.List&Map&String,String&&的遍历:
Map&String,String& map = new HashMap&String, String&();
map.put("1", "fds");
map.put("2", "valu");
List&Map&String, String&& list = new ArrayList&Map&String, String&&();
list.add(map);
for (Map&String, String& m : list){
for (String k : m.keySet()){
System.out.println(k + " : " + m.get(k));
 2.两个List&Map&String,String&&相互比较,有相同数据时某些字段相加减
&inOrderList与outOrderList放自己的所比较的数据,name,number,id为map的key值
for (int i = 0; i & inOrderList.size(); i++) {//循环获取入库数据
String number1 = String.valueOf(inOrderList.get(i).get("number"));
String depotName1 = inOrderList.get(i).get("name");
String proId1 = inOrderList.get(i).get("id");
for (int j = 0; j & outOrderList.size(); j++) {//循环获取出库数据
String number2 = String.valueOf(outOrderList.get(j).get("number"));
String depotName2 = outOrderList.get(j).get("name");
String proId2 = outOrderList.get(j).get("id");
if (depotName1.equals(depotName2) && proId1.equals(proId2)) {
Integer sum =Integer.parseInt(number1) - Integer.parseInt(number2);//判断是否为同一条数据,同条数据数量相减
inOrderList.get(i).put("number", sum + "");
& 3.单个List&Map&String,String&&相互比较,相同数据相加(转)
List&Map&String, String&& l = new ArrayList&Map&String,String&&();
Map&String, String& m1 = new HashMap&String, String&();
m1.put("count", "1");
m1.put("name", "帮宝适纸尿裤L164 超薄干爽夏季透气专用婴儿尿不湿");
m1.put("province", "江苏省");
m1.put("date", " 10:13:39");
m1.put("channel", "东环大润发");
m1.put("city", "苏州市");
l.add(m1);
Map&String, String& m2 = new HashMap&String, String&();
m2.put("count", "1");
m2.put("name", "帮宝适纸尿裤L164 超薄干爽夏季透气专用婴儿尿不湿");
m2.put("province", "江苏省");
m2.put("date", " 10:13:39");
m2.put("channel", "东环大润发");
m2.put("city", "泰州市");
l.add(m2);
Map&String, String& m3 = new HashMap&String, String&();
m3.put("count", "1");
m3.put("name", "帮宝适纸尿裤L164 超薄干爽夏季透气专用婴儿尿不湿");
m3.put("province", "江苏省");
m3.put("date", " 10:13:39");
m3.put("channel", "东环大润发");
m3.put("city", "泰州市");
l.add(m3);
List&Map&String, String&& countList = new ArrayList&Map&String,String&&();//用于存放最后的结果
for (int i = 0; i & l.size(); i++) {
String count = l.get(i).get("count");
String name = l.get(i).get("name");
String province = l.get(i).get("province");
String date = l.get(i).get("date");
String channel = l.get(i).get("channel");
String city = l.get(i).get("city");
int flag = 0;//0为新增数据,1为增加count
for (int j = 0; j & countList.size(); j++) {
String count_ = countList.get(j).get("count");
String name_ = countList.get(j).get("name");
String province_ = countList.get(j).get("province");
String date_ = countList.get(j).get("date");
String channel_ = l.get(j).get("channel");
String city_ = countList.get(j).get("city");
if (count.equals(count_)&&
name.equals(name_)&&
province.equals(province_)&&
date.equals(date_)&&
channel.equals(channel_)&&
city.equals(city_)) {
int sum = Integer.parseInt(l.get(i).get("count")) + Integer.parseInt(countList.get(j).get("count"));
countList.get(j).put("count", sum + "");
if (flag == 0) {
countList.add(l.get(i));
for (Map&String, String& map : countList) {
System.out.println(map);
public class Test {
* @param args
public static void main(String[] args) {
List&Map&String, String&& list = new ArrayList&Map&String,String&&();
Map&String,String& map1 = new HashMap&String,String&();
map1.put("count", "1");
map1.put("name", "帮宝适纸尿裤L164 超薄干爽夏季透气专用婴儿尿不湿");
map1.put("province", "江苏省");
map1.put("date", " 10:13:39");
map1.put("channel", "东环大润发");
map1.put("city", "苏州市");
list.add(map1);
Map&String,String& map2 = new HashMap&String,String&();
map2.put("count", "1");
map2.put("name", "帮宝适纸尿裤L164 超薄干爽夏季透气专用婴儿尿不湿");
map2.put("province", "江苏省");
map2.put("date", " 10:13:39");
map2.put("channel", "东环大润发");
map2.put("city", "泰州市");
list.add(map2);
Map&String,String& map3 = new HashMap&String,String&();
map3.put("count", "1");
map3.put("name", "帮宝适纸尿裤L164 超薄干爽夏季透气专用婴儿尿不湿");
map3.put("province", "江苏省");
map3.put("date", " 10:13:39");
map3.put("channel", "东环大润发");
map3.put("city", "泰州市");
list.add(map3);
Map&String,String& map4 = new HashMap&String,String&();
map4.put("count", "1");
map4.put("name", "帮宝适纸尿裤L164 超薄干爽夏季透气专用婴儿尿不湿");
map4.put("province", "江苏省");
map4.put("date", " 10:13:39");
map4.put("channel", "东环大润发");
map4.put("city", "苏州市");
list.add(map4);
System.out.println(mergeList(list));
* 合并相同的Map
* @param list
public static List&Map&String,String&& mergeList(List&Map&String,String&& list){
List&Map&String,String&& retList = new ArrayList&Map&String,String&&();
for(int i=0;i&list.size();i++){
Map&String,String& map = list.get(i);
String name = map.get("name");
String city = map.get("city");
String count = map.get("count");
Map&String,String& newMap=isExistSame(i,name,city,count,list);
if(null==newMap){
retList.add(map);
list.remove(map.get(i));
retList.add(newMap);
return retL
private static Map&String,String& isExistSame(int i,String name,String city,String count,List&Map&String,String&& list){
Map&String,String& newMap =
for(int j=i+1;j&list.size();j++){
Map&String,String& innerMap = list.get(j);
String name1 = innerMap.get("name");
String city1 = innerMap.get("city");
String count1 = innerMap.get("count");
if(name.equals(name1)&&city.equals(city1)){
newMap = new HashMap&String,String&();
newMap.put("count", ""+(Integer.parseInt(count)+Integer.parseInt(count1)));
Iterator&String& keyIterator = innerMap.keySet().iterator();
while(keyIterator.hasNext()){
String key = keyIterator.next();
if(!key.equals("count")){
newMap.put(key, innerMap.get(key));
list.remove(j);
return newM
ublic static void main(String[] args) {
List&Map&String, String&& list = new ArrayList&Map&String,String&&();
boolean m =
for(int i=0;i&10000;i++){
HashMap&String, String& map = new HashMap&String, String&();
map.put("count", "1");
map.put("name", "帮宝适纸尿裤L164 超薄干爽夏季透气专用婴儿尿不湿");
map.put("province", "江苏省");
map.put("date", " 10:13:39");
map.put("channel", "东环大润发");
map.put("city", m ? "苏州市":"泰州市");
list.add(map);
System.out.println(System.currentTimeMillis());
for(int i=list.size();--i&-1;){
Map&String, String& src = list.get(i);
int same = 0;
for(int j=i-1;j&-1;j--){
Map&String, String& tar = list.get(j);
if(equals(src, tar)){
int count = Integer.parseInt(src.get("count"))
+Integer.parseInt(tar.get("count"));
src.put("count", count+"");
list.remove(j);
System.out.println(list);
System.out.println(System.currentTimeMillis());
public static boolean equals(Map&String, String& src,Map&String, String& tar){
Set&Entry&String,String&& entrySet = src.entrySet();
String srcCount = src.remove("count");//count不参与比较
String srcValue = null,tarValue =
for (Iterator&Entry&String,String&& it = entrySet.iterator(); it.hasNext();) {
Entry&String, String& entry = it.next();
srcValue = entry.getValue();
tarValue = tar.get(entry.getKey());
if(srcValue.length()!=tarValue.length()||!srcValue.equals(tarValue)){
src.put("count", srcCount);
src.put("count", srcCount);
阅读(...) 评论()用户名:xie475382
文章数:64
访问量:21225
注册日期:
阅读量:1297
阅读量:3317
阅读量:586300
阅读量:472412
51CTO推荐博文
//最常规的一种方法,最常规就是最常用的,虽然不复杂,但很重要,这是我们最熟悉的,就不多说了!!
public static void work(Map&String, Student& map) {
Collection&Student& c = map.values();
Iterator it = c.iterator();
for (; it.hasNext();) {
System.out.println(it.next());
}  //利用keyset进行,它的优点在于可以根据你所想要的key值得到你想要的 values,更具灵活性!!
public static void workByKeySet(Map&String, Student& map) {
Set&String& key = map.keySet();
for (Iterator it = key.iterator(); it.hasNext();) {
String s = (String) it.next();
System.out.println(map.get(s));
}  //比较复杂的一种在这里,呵呵~~他很暴力哦,它的灵活性太强了,想得到什么就能得到什么~~
public static void workByEntry(Map&String, Student& map) {
Set&Map.Entry&String, Student&& set = map.entrySet();
for (Iterator&Map.Entry&String, Student&& it = set.iterator(); it.hasNext();) {
Map.Entry&String, Student& entry = (Map.Entry&String, Student&) it.next();
System.out.println(entry.getKey() + "---&" + entry.getValue());
了这篇文章
类别:┆阅读(0)┆评论(0)博客分类:
假定在struts2 的action的属性域中定义2个如下的List和Map变量和一个String对象:
private List&String& testlist=new ArrayList&String&();
private Map&String,String& testmap=new HashMap&String,String&();
private String testname="hello,i am in valuestack";
//直接初始化了
并同时提供getter和setter方法.
在 action中的execute方法中有如下的赋值操作:
* OGNL测试代码
HttpServletRequest request=ServletActionContext.getRequest();
//对应request
HttpSession session=request.getSession();
//对应session
ServletContext context=ServletActionContext.getServletContext();
// 对应application
//构造普通的字符串放到三个环境中
request.setAttribute("request_name",
"来自request中的name");
context.setAttribute("application_name","来自application中的name");
session.setAttribute("session_name", "来自session中的name");
//构造一个list放到request中
List&String& request_names=new ArrayList&String&();
request_names.add("reqname1");
request_names.add("reqname2");
request_names.add("reqname3");
request.setAttribute("req_names", request_names);
//构造一个map放到rquest中
Map&String,String& request_maps=new HashMap&String,String&();
request_maps.put("request_map1","request_value1");
request_maps.put("request_map2","request_value2");
request_maps.put("request_map3","request_value3");
request.setAttribute("req_maps", request_maps);
//构造list放到session中
List&String& session_names=new ArrayList&String&();
session_names.add("sessionname1");
session_names.add("sessionname2");
session_names.add("sessionname3");
session.setAttribute("session_names", session_names);
//放到request中
//构造一个map放到session中
Map&String,String& session_maps=new HashMap&String,String&();
session_maps.put("session_map1","session_value1");
session_maps.put("session_map2","session_value2");
session_maps.put("session_map3","session_value3");
session.setAttribute("session_maps", session_maps);
//赋值到ActionContext值栈中的对象
//ActionContext中的testlist
testlist.add("vs144444");
testlist.add("vs24444");
testlist.add("vs34444");
testlist.add("vs");
//ActionContext中的testmap
testmap.put("mp1", "vsmap1");
testmap.put("mp2", "vsmap2");
testmap.put("mp3", "vsmap3");
testmap.put("mp4", "vsmap5555");
//ActionContext中的score
testscore.put("语文",95);
testscore.put("数学",75);
testscore.put("英语",55);
testscore.put("生物",65);
testscore.put("物理",85);
testscore.put("化学",44);
在该action跳转后的视图jsp页面上使用struts2标签和OGNL表达式取以上的各个范围中的值的代码为:
&hr&显示来自各个范围中的值&br&
actionContext中的testname:&s:property value="testname" /& &br&
request中的request_name:&s:property value="#request.request_name" /&&br&
application中的session_name:&s:property value="#application.application_name" /&&br&
session中的session_name:&s:property value="#session.session_name" /&&br&
遍历request中的list:request_names&p&
&s:iterator value="#request.req_names"&
&s:property /&&br&
&/s:iterator&
遍历request中的map:request_maps&p&
&s:iterator value="#request.req_maps"&
&s:property value="key"/&=&s:property value="value" /&&br&
&/s:iterator&
遍历session中的list:session_names&p&
&s:iterator value="#session.session_names"&
&s:property /& &br&
&/s:iterator&
遍历session中的map:session_maps&p&
&s:iterator value="#session.session_maps"&
&s:property value="key"/&=&s:property value="value" /&&br&
&/s:iterator&
遍历ActionContext中的testlist=========================&p&
&s:iterator value="testlist" &
&s:property /&&br&
&/s:iterator&
遍历actionContext中的testmap&p&
&s:iterator value="testmap"&
&s:property value="key"/&=&s:property value="value" /&&br&
&/s:iterator&
直接遍历list&p&
&s:iterator value="{'第一年','第二年','第三年'}" &
&s:property /&&br&
&/s:iterator&
直接遍历map &p&
&s:iterator value="#{'luo1':'name,'luo2':'name2','luo3':'name3'}"&
&s:property value="key" /&:&s:property value="value"/&&br&
&/s:iterator&
遍历actionContext中的testscore这个map&p&
&s:iterator value="testscore"&
&s:property value="key"/&:
&s:if test="value&60"&
不及格&br&
&s:elseif test="value&=60 && value&75"&
&/s:elseif&
&s:elseif test="value&=75 && value&85"&
&/s:elseif&
&/s:iterator&
显示的结果为:
显示来自各个范围中的值
actionContext中的testname:hello,i am in valuestack
request中的request_name:来自request中的name
application中的session_name:来自application中的name
session中的session_name:来自session中的name
遍历request中的list:request_names
遍历request中的map:request_maps
request_map1=request_value1
request_map2=request_value2
request_map3=request_value3
遍历session中的list:session_names
sessionname1
sessionname2
sessionname3
遍历session中的map:session_maps
session_map1=session_value1
session_map2=session_value2
session_map3=session_value3
遍历ActionContext中的testlist=========================
遍历actionContext中的testmap
mp1=vsmap1
mp2=vsmap2
mp4=vsmap5555
mp3=vsmap3
直接遍历list
直接遍历map
luo2:name2
luo3:name3
遍历actionContext中的testscore这个map
物理: 优秀
语文: 优秀
英语: 不及格
数学: 良好
化学: 不及格
生物: 及格
说明几个地方:
1. struts2总是将action的实例放置在栈顶,因为action在值栈中,而值栈又是OGNL的根,所以引用action中的属性可以省略#
例如我们访问action中的属性testname:
actionContext中的testname:&s:property value="testname" /& &br&
2. 访问request,application,session以及请求参数parameters的值,需要使用#request.xxxx 这样的访问方式访问
浏览: 85865 次
来自: 深圳
如果我们不准备实现一个接口的全部方法时,因为我们这个类不需要用 ...
显然这个错误是在使用struts标签时产生的,原因是没有在st ...
{} xtcpcgx 写道还是有点不明白,感觉缺省适配类跟接口 ...
还是有点不明白,感觉缺省适配类跟接口代码一模一样啊。
这段代码的结果是,dopost根本没有执行?
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'js Map List 遍历
Map& (exMap:{&name&:&abc&,&sex&,'male'}):
在不知道key的情况下遍历map:
网上说过这种方法:
[javascript]
for(var key in exMap){&
&&& Console.write(&key:&+key+&;value:&+exMap[key]);//the key is undefined.But,some times i use it is good .So why is it caused?Can you help me ? .&&
for(var key in exMap){
&Console.write(&key:&+key+&;value:&+exMap[key]);//the key is undefined.But,some times i use it is good .So why is it caused?Can you help me ? .
}另外一种方法(Good):
[javascript]
$.each(exMap,function(key,value){&
&&& Console.write(&key:&+key+&;value:&+value);&
$.each(exMap,function(key,value){
&Console.write(&key:&+key+&;value:&+value);
在知道的key的情况下遍历map自然就跟数组一样的访问罗,这里就不说了。
List访问很简单:
[javascript]
for(var i =0;i&list.i++){&
&&& value = list[i]&
for(var i =0;i&list.i++){
&value = list[i]
参考(jquery筛选数组之grep、each、inArray、map的用法及遍历json对象 ):}

我要回帖

更多关于 java遍历map中的list 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信