• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

JAVA遍历Map集合的方法

java 来源:周嗨嗨 1次浏览

 

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;


public class MapTest4 {
	public static void main(String[] args) {
		Map<String, String> map1=new HashMap<String, String>();
		map1.put("aqw", "安全网");
		map1.put("ceshi", "测试网");
		map1.put("ali", "阿里网");
		map1.put("ali", "阿里1网"); 
		//jdk1.4中 
		//遍历方法一
/*		Set<Entry<String, String>> se1= map1.entrySet();
		Iterator it=se1.iterator();
		while(it.hasNext()){
			Entry en1=(Entry)it.next();
			Object key=en1.getKey();
			Object value=en1.getValue();
			System.out.println("输出key:"+key+" "+"输出value:"+value);
		}*/
		
		//通过先获取到所有的key然后通过key遍历出所有的value
		//遍历方法二
		/*Set<String> set=map1.keySet(); //得到所有key的集合
		for(String str:set){
			String str1=map1.get(str);
			System.out.println(str+"  "+str1);
		}*/
		
		//通过map.entryset()遍历key和value,---推荐,尤其容量大时---
		//JDE1.5中,应用新特性For循环遍历
		//遍历方法三
		for(Entry<String, String> en1:map1.entrySet()){
			System.out.println(en1.getKey());
			System.out.println(en1.getValue());
		}
		
		//返回所有的值,但是不能得到key,如果key相同,只会取一个,按照顺序取最后一个value
		//遍历方法4
		/*for(String v:map1.values()){
			System.out.println(v);
		}*/
	}
}

 

//jsp页面放入select下拉中一种写法
<%
	Map<String, String> goods = 获取到的Map集合;
	for (Map.Entry<String, String> en : goods.entrySet()) {
		out.print("<option value=\""
				+ en.getKey()
				+ "\""
				+ (dgood.indexOf(en.getKey()) != -1 ? " selected=\"selected\""
				: "") + ">" + en.getValue() + "</option>");
		}
%>

实例:

1.jsp页面

 

<%@ page language="java" import="java.util.Map" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
Map<String,String> menus=(Map<String,String>)request.getAttribute("menus");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/1.6.4/jquery.min.js"></script>
</head>
<body>
	<input type="text" name="name" value="${name }"/>
	<select>
		<!-- 这种indexOf方式不好,多的时候容易出问题啦!如果key是拼音简写这些 -->
		<%-- <%
			Map<String, String> goods = menus;
			String dgood="al";
			for (Map.Entry<String, String> en : goods.entrySet()) {
				out.print("<option value=\""
						+ en.getKey()
						+ "\""
						+ (dgood.indexOf(en.getKey()) != -1 ? " selected=\"selected\""
						: "") + ">" + en.getValue() + "</option>");
				}
		%> --%>		
		<!--这儿简单的egood.equals(en.getkey())如果替换成用en.getkey().equals(egood)时候量很大还是其它原因是会报错的!所以就这样写  -->
		<%
			Map<String, String> egoods = menus;
			String egood="aly";
			for (Map.Entry<String, String> en : egoods.entrySet()) {
				out.print("<option value=\""
						+ en.getKey()
						+ "\""
						+ (egood.equals(en.getKey()) ? " selected=\"selected\""
						: "") + ">" + en.getValue() + "</option>");
				}
		%>		
	</select>
</body>
</html>

 

 

 

2.controller控制器

 

package com.bbaqw.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class FourController {
	
	@RequestMapping("show")
	public ModelAndView showInfo(HttpServletRequest reqeust,HttpServletResponse response){
		ModelAndView mav=new ModelAndView("/index");
		mav.addObject("name", "夏利");
		Map<String,String> map=new HashMap<String,String>();
		map.put("aly", "阿里云");
		map.put("al", "阿里");
		map.put("bd", "百度");
		map.put("albb", "阿里吧吧");
		map.put("bbal", "吧吧阿里");
		map.put("tx", "腾讯");
		map.put("txhd", "腾讯互动");
		mav.addObject("menus", map);
		return mav;
	}
}	

 

 

 

 


版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)