map的使用方法
package cn.stat.p8.map.demo; import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set; public class MapDemo2 { public static void main(String[] args) {
Map<Integer,String> mapset=new HashMap<Integer,String>();
method_2(mapset);
} private static void method(Map<Integer, String> mapset) {
mapset.put(8,"list");
mapset.put(9,"list6");
mapset.put(3,"list2");
mapset.put(6,"list7");
mapset.put(1,"list9"); Set<Map.Entry<Integer,String>> ks=mapset.entrySet();
Iterator<Map.Entry<Integer,String>> it=ks.iterator();
while(it.hasNext())
{
Map.Entry<Integer,String> me=it.next(); Integer key=me.getKey();
String value=me.getValue(); System.out.println(value+" "+key);
}
}
public static void method_2(Map<Integer, String> mapset) {
mapset.put(8,"list");
mapset.put(9,"list6");
mapset.put(3,"list2");
mapset.put(6,"list7");
mapset.put(1,"list9");
Collection<String> values=mapset.values();
Iterator<String> it2=values.iterator();
while(it2.hasNext())
{
System.out.println(it2.next());
}
} }
treemap使用
package cn.stat.p7.map.demo; import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap; public class MapDemo { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Map<Integer,String> map=new TreeMap<Integer,String>();
method(map);
}
public static void method(Map<Integer,String> map)
{
//添加
System.out.println(map.put(8,"wangcai"));
System.out.println(map.put(8,"xiaoxiao"));
map.put(2, "lili");
map.put(9, "niuniu"); System.out.println(map); //删除
System.out.println(map.remove(2)); System.out.println(map); //判断
System.out.println("containskey:"+map.containsKey(8));
System.out.println("containsvalues:"+map.containsValue("222")); //获取
System.out.println(map.get(8));//不存在返回null
} }
map的使用方法的更多相关文章
- .NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法
.NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法 0x00 为什么需要Map(MapWhen)扩展 如果业务逻辑比较简单的话,一条主管道就够了,确实用不到 ...
- 集合框架Map之entrySet方法的使用
Map的entrySet函数的使用,取得是键和值的映射关系,Entry就是Map接口中的内部接口,类似与我们熟悉的内部类一样,内部类定义在外部类内部,可以直接访问到外部类中的成员 package cn ...
- 在JavaScript函数式编程里使用Map和Reduce方法
所有人都谈论道workflows支持ECMAScript6里出现的令人吃惊的新特性,因此我们很容易忘掉ECMAScript5带给我们一些很棒的工具方法来支持在JavaScript里进行函数编程,这些工 ...
- Map的遍历方法及String和其它类型的相互转化
Map的遍历方法: package com.lky.test; import java.util.HashMap; import java.util.Iterator; import java.uti ...
- 使用Map/MapWhen扩展方法
使用Map/MapWhen扩展方法 .NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法 0x00 为什么需要Map(MapWhen)扩展 如果业务逻辑比较简 ...
- forEach和map和for方法的区别
JS中的forEach.$.each.map方法推荐 转载 2016-04-05 投稿:jingxian 我要评论 下面小编就为大家带来一篇JS中的forEach.$.each.map方法 ...
- Java 遍历Map集合的方法
方法一:通过Map.keySet,遍历key和value Map<String, Object> map = new HashMap<>(); for (String key ...
- Java基础关于Map(字典)的方法使用
Java基础关于Map(字典)的方法使用 java中一般用map与hashmap来创建一个key-value对象 使用前提是要导入方法包: import java.util.HashMap: impo ...
- 数组的forEach和map和for方法的区别
一.定义 foreach(): 从头到尾遍历数组,为每个元素调用指定的函数. map(): 将调用的数组的每个元素传递给指定的函数,并返回一个数组,他包含该函数的返回值. 传递的函数是 forea ...
- 红黑树规则,TreeSet原理,HashSet特点,什么是哈希值,HashSet底层原理,Map集合特点,Map集合遍历方法
==学习目标== 1.能够了解红黑树 2.能够掌握HashSet集合的特点以及使用(特点以及使用,哈希表数据结构) 3.能够掌握Map集合的特点以及使用(特点,常见方法,Map集合的遍历) 4.能够掌 ...
随机推荐
- 新安装ubuntu后几项配置
新安的ubuntu13.04 为了编程方便 进行如下设置 安装右键terminal sudo apt-get install nautilus-open-terminal 安装远程连接ssh ...
- iOS开发——C篇&动态内存分配
再C语言中关于内存是一个很重要的知识点,所以今天我就从c语言的内存分配开始为大家解析一下C语言再iOS开发中非常重要的一些知识. 1:malloc函数的介绍 C语言中开辟内存空间:malloc函数 再 ...
- mac 常用的开发工具
http://www.oschina.net/news/53946/mac-dev-tools 要清楚的认识到,我们寻找的不是开始按钮,而是程序入口,任何一个操作系统,用户要做的事情并不是找到开始菜单 ...
- html文字有光晕
<style> .tb{ font-size:40px; filter:glow(color=pink,direction=); font-family:华文行楷; } </styl ...
- 在线安装maven插件问题:Cannot complete the install because one or more required items could not be found.
用Eclipse在线安装的方式:Help-->Install New Software 地址输入:http://m2eclipse.sonatype.org/sites/m2e/,列表中打勾勾 ...
- hdu 2992 Hotel booking
http://acm.hdu.edu.cn/showproblem.php?pid=2992 #include <cstdio> #include <cstring> #inc ...
- 设计模式(十一):FACADE外观模式 -- 结构型模式
1. 概述 外观模式,我们通过外观的包装,使应用程序只能看到外观对象,而不会看到具体的细节对象,这样无疑会降低应用程序的复杂度,并且提高了程序的可维护性.例子1:一个电源总开关可以控制四盏灯.一个风扇 ...
- ASP.NET MVC 3 Razor Nested foreach with if statements
You need to write code this way. @Html.Raw("<tr>") Copy the below code and paste it ...
- [LeetCode] 230. Kth Smallest Element in a BST 解题思路
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...
- 深入浅出-iOS函数式编程的实现 && 响应式编程概念
简介 本篇主要回顾一下--iOS函数式编程 && 响应式编程概念 ,如何一步步实现函数式编程的过程,对阅读Masonry && SnapKit源码有一定的帮助. 配图 ...