迭代器将map或者list分批量操作

来源:互联网 发布:流浪诗人知乎 编辑:程序博客网 时间:2024/06/03 12:40


public static void main(String [] args){


ten();

}

//map分批量操作,长度为10

public static void ten(){
Map<String,String> mapTen=new HashMap<String,String>();
mapTen.put("1", "1t");
mapTen.put("2", "2t");
mapTen.put("3", "3t");
mapTen.put("4", "4t");
mapTen.put("5", "5t");
mapTen.put("6", "6t");
mapTen.put("7", "7t");
mapTen.put("8", "8t");
mapTen.put("9", "9t");
mapTen.put("10", "10t");
mapTen.put("11", "11t");
mapTen.put("12", "12t");
mapTen.put("13", "13t");
mapTen.put("14", "14t");
mapTen.put("15", "15t");
mapTen.put("16", "16t");

if(mapTen.size() > 10){
int a=mapTen.size();
int size = (a%10==0?a/10:a/10+1);
Iterator<Entry<String, String>> iterator=  mapTen.entrySet().iterator();
for(int i=0;i<size;i++){
Map<String,String> map=new HashMap<String,String>();
int j=1;
while(iterator.hasNext()){
if(j>10){
break;

Entry<String, String>  entry=iterator.next();
map.put(entry.getKey(),entry.getValue());
j++;
}
// 操作
System.out.println(map);
}
}

}




//list迭代器分批

if(list==null||list.size()<1){
return;
}
if(list.size() > 10){
int a=list.size();
int size = (a%10==0?a/10:a/10+1);
Iterator<MessageVo> iterator=  list.iterator();
for(int i=0;i<size;i++){
Map<String,String> map=new HashMap<String,String>();
List<MessageVo> list2=new  ArrayList<MessageVo>();
int j=1;
while(iterator.hasNext()){
if(j>10){
break;

MessageVo  entry=iterator.next();
list2.add(entry);
j++;
}
map.put(MsgConstantUtil.MSG_SERVER_IMUDPSERVICE_MSGMAPVO , JSON.toJSONString(list2));
// 推送消息
HttpClientUtil.post(MsgConstantUtil.MSG_SERVER_URL + MsgConstantUtil.MSG_SERVER_SENDMSG, null,map );
}
}else{
// 推送消息
HttpClientUtil.post(MsgConstantUtil.MSG_SERVER_URL + MsgConstantUtil.MSG_SERVER_SENDMSG, null, map1);
}

0 0
原创粉丝点击