获取map中的一个value值以及遍历map获得map里所有key、value的值

前言:

1.声明一个map: Map map = new HashMap();2.向map中放值,注意:map是key-value的形式存放的.如:

map.put(”sa”,”dd”);

3.从map中取值:String str = map.get(”sa”).toString();结果是:str = ”dd”;4.遍历一个map,从中取得key 和valueMap map = new HashMap() ;

Iterator it = map.entrySet().iterator() ;while (it.hasNext()){Map.Entry entry = (Map.Entry) it.next() ;Object key = entry.getKey() ;Object value = entry.getValue() ;}

Java代码如下:

package Test01;

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

public class Test03 {
	public static void main(String[] args){
		a();
		b();
	}
	@SuppressWarnings("unchecked")
	public static void a(){
		@SuppressWarnings("rawtypes")
		Map map = new HashMap();
		map.put("1","aa");
		map.put("2","bb");
		map.put("3","cc");
		map.put("4","dd");
		map.put("5","ee");
		map.put("6","ff");
		map.put("7","gg");

		String str = map.get("5").toString();
		System.out.println(str);
	}

	@SuppressWarnings("unchecked")
	public static void b(){
		@SuppressWarnings("rawtypes")
		Map map = new HashMap();
		map.put("1","aa");
		map.put("2","bb");
		map.put("3","cc");
		map.put("4","dd");
		map.put("5","ee");
		map.put("6","ff");
		map.put("7","gg");
		@SuppressWarnings("rawtypes")
		Iterator it = map.entrySet().iterator() ;
		while (it.hasNext())
		{
		@SuppressWarnings("rawtypes")
		Map.Entry entry = (Map.Entry) it.next() ;
		Object key = entry.getKey() ;
		Object value = entry.getValue() ;
		System.out.print("["+key+"、");
		System.out.print(value+"]");
		System.out.print(",");
		}
	}
}

代码运行后效果如下:

				
时间: 2024-08-03 07:52:20

获取map中的一个value值以及遍历map获得map里所有key、value的值的相关文章

Javascript RegExp对象---获取url中某一个参数的值

RegExp 对象 RegExp 对象表示正则表达式,它是对字符串执行模式匹配的强大工具. 直接量语法 /pattern/attributes实例:window.location.href:http://localhost:8100/aspx/main/ServiceCenter_list.aspx?category_id=93&page=2要匹配到的category_id=93:/category_id=\d+/g 创建 RegExp 对象的语法: new RegExp(pattern, at

java面试题 "aababcabcdabcde",获取字符串中每一个字母出现的次数要求结果:a(5)b(4)c(3)d(2)e(1)

题目:"aababcabcdabcde",获取字符串中每一个字母出现的次数要求结果:a(5)b(4)c(3)d(2)e(1) 刚开始可能对这题无从下手,现在就一步步分析 1:首先它是一个字符串,但是要对每个字符进行分析它出现的次数,那么肯定是要进行循环遍历,要进行遍历一般要么是集合,要么是数组,而在这里变成数组比较好, 2:变成数组后,我们可以用一个map集合存储字符和出现的次数也就是说key是Character,value是Integer,然后在遍历的时候拿key获取value值进行

获取字符串中某一个字段的数据,GetValueFromStr

gps数据格式为:$GPRMC,024813.640,A,3158.4608,N,11848.3737,E,10.05,324.27,150706,,,A* /********************************************************************** *版权所有 (C)2015, Wuyq. * *文件名称: GetValueFromStr.c *内容摘要:用于演示从gps数据字符串中获取相应的内容 *其它说明:无 *当前版本: V1.0 *作

java "aababcabcdabcde",获取字符串中每一个字母出现的次数

需求:"aababcabcdabcde",获取字符串中每一个字母出现的次数 分析: * A:定义一个字符串(可以改进为键盘录入) * B:定义一个TreeMap集合 * 键:Character * 值:Integer * C:把字符串转换为字符数组 * D:遍历字符数组,得到每一个字符 * E:拿刚才得到的字符作为键到集合中去找值,看返回值 * 是null:说明该键不存在,就把该字符作为键,1作为值存储 * 不是null:说明该键存在,就把值加1,然后重写存储该键和值 * F:定义字符

获取页面中任意一个元素距离body的偏移量

//offSet:等同于jQuery中的offSet方法,获取页面中任意一个元素距离body的偏移量 function offSet(curEle) {     var totalLeft = null;     var totalTop = null;     var par = curEle.offsetParent;     //首先把自己本身的相加     totalLeft += curEle.offsetLeft;     totalTop += curEle.offsetTop;

获取列表中某一个文件夹下的列表项集合(不包含子文件夹对象,也不包含子文件夹中的列表项)

RT,方法如下: 1 SPListItemCollection GetSubItemsWithoutFoldersInParrentFolder(SPFolder parrent) 2 { 3 SPList list = parrent.Item.ParentList; 4 SPQuery query = new SPQuery(); 5 query.Folder = parrent; 6 query.Query = "<Where><Eq><FieldRef Na

遍历map和删除map中的一个entry

一.最常见的,需要key和value都需要时 public static void main(String[] args) { Map<Integer,Integer> map = new HashMap<Integer,Integer>(); map.put(33, 333); map.put(22, 222); map.put(11, 111); for(Map.Entry<Integer, Integer> entry:map.entrySet()){ Syste

获取数组中最后一个元素

Array.prototype.slice(begin,end)用来获取begin和end之间的数组元素.如果你不设置end参数,将会将数组的默认长度值当作end值.但有些同学可能不知道这个函数还可以接受负值作为参数.如果你设置一个负值作为begin的值,那么你可以获取数组的最后一个元素.如: var array = [1,2,3,4,5,6]; console.log(array.slice(-1)); // [6] console.log(array.slice(-2)); // [5,6]

stream根据map中的一个字段排序

org = org.stream().sorted(Comparator.comparing(SynchronousDataService::sort2)).collect(Collectors.toList());// 排序 private static String sort2(Map<String, Object> a) {  return (String) a.get("uri"); } 原文地址:https://www.cnblogs.com/sikuaiwu/p