❶ java中map的常用遍历方法
方法一 在for-each循环中使用entries来遍历
这是最常见的并且在大多数情况下也是最可取的遍历方式。在键值都需要时使用。
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
注意:for-each循环在java 5中被引入所以该方法只能应用于java 5或更高的版本中。如果你遍历的是一个空的map对象,for-each循环将抛出NullPointerException,因此在遍历前你总是应该检查空引用。
方法二 在for-each循环中遍历keys或values。
如果只需要map中的键或者值,你可以通过keySet或values来实现遍历,而不是用entrySet。
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
//遍历map中的键
for (Integer key : map.keySet()) {
System.out.println("Key = " + key);
}
//遍历map中的值
for (Integer value : map.values()) {
System.out.println("Value = " + value);
}
该方法比entrySet遍历在性能上稍好
方法三使用Iterator遍历
使用泛型:
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
Iterator<Map.Entry<Integer, Integer>> entries = map.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry<Integer, Integer> entry = entries.next();
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
不使用泛型:
Map map = new HashMap();
Iterator entries = map.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry entry = (Map.Entry) entries.next();
Integer key = (Integer)entry.getKey();
Integer value = (Integer)entry.getValue();
System.out.println("Key = " + key + ", Value = " + value);
}
方法四、通过键找值遍历(效率低)
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (Integer key : map.keySet()) {
Integer value = map.get(key);
System.out.println("Key = " + key + ", Value = " + value);
}
总结
如果仅需要键(keys)或值(values)使用方法二。
如果你使用的语言版本低于java 5,或是打算在遍历时删除entries,必须使用方法三。
否则使用方法一(键值都要)。
❷ java里面的map是什么
java为数据结构中的映射定义了一个接口java.util.Map
Map主要用于存储健值对,根据键得到值,因此不允许键重复(重复了覆盖了),但允许值重复。
❸ java中Map<,>是什么意思
Map<?,?>是一对对的值,比如说里面加入的是你的名字和身份证号码时,可以通过你的名字找到省份证号码,通过身份证可以找到你名字。
❹ java中map有几种写法,一般怎样写,有什么区别吗
Map<String, Object> map = new HashMap<String, Object>();
Map<String, Object> ma = new HashMap<>();
简写,就是前面指定泛型,后面就可以省略。
HashMap<String, Object> m = new HashMap<>();
HashMap<String, Object> mm = new HashMap<String, Object>();
不建议这么写,一般是接口在左,实现类在右,实现依赖倒置原则。
❺ JAVA 里 的Map
1.User.java
publicclassUser{
privateStringuserName;//用户名
privateStringpassword;//密码
privateStringgender;
privateIntegerage;
privatedoublemoney;//钱
publicUser(StringuserName,Stringpassword,doublemoney){
super();
this.userName=userName;
this.password=password;
this.money=money;
}
publicUser(StringuserName,Stringpassword,Stringgender,Integerage,doublemoney){
super();
this.userName=userName;
this.password=password;
this.gender=gender;
this.age=age;
this.money=money;
}
publicStringgetGender(){
returngender;
}
publicvoidsetGender(Stringgender){
this.gender=gender;
}
publicIntegergetAge(){
returnage;
}
publicvoidsetAge(Integerage){
this.age=age;
}
publicStringgetUserName(){
returnuserName;
}
publicvoidsetUserName(StringuserName){
this.userName=userName;
}
publicStringgetPassword(){
returnpassword;
}
publicvoidsetPassword(Stringpassword){
this.password=password;
}
publicdoublegetMoney(){
returnmoney;
}
publicvoidsetMoney(doublemoney){
this.money=money;
}
publicStringtoString(){
return"Name:"+this.userName+"password:"+password+"gender:"+gender+"age:"+age
+"money:"+this.money;
}
}
2.Bank.java
importjava.util.HashMap;
importjava.util.Map;
importjava.util.Scanner;
{
=1L;
publicPermissionException(StringerrMsg){
super(errMsg);
}
}
publicclassBank{
privatestaticMap<String,User>bankMap=newHashMap<String,User>();
privatestaticScannerinput=newScanner(System.in);
/**
*增加用户
*
*@paramuser
*用户对象
*/
(Useruser){
bankMap.put(user.getUserName(),user);
}
/**
*取出用户
*
*@paramuserName
*用户名
*@returnuserornull
*/
publicstaticUsergetUser(StringuserName){
if(null==userName){
userName="";
}
returnbankMap.get(userName);
}
/**
*login
*/
publicstaticvoidlogin(){
intinputTimes=0;
Useruser;
System.out.print("Account:");
StringuserName=input.next();
do{
System.out.print("Password:");
Stringpassword=input.next();
user=getUser(userName);
if(user!=null&&user.getPassword().equals(password)){
break;
}
inputTimes++;
}while(inputTimes<3);
if(inputTimes>=3){
deny();
}else{
takeMoney(user);
}
}
/**
*takemoney
*/
publicstaticvoidtakeMoney(Useruser){
doubletake=0;
doubleremain=user.getMoney();
do{
System.out.print("Takehowmuch?input:");
take=input.nextDouble();
if(take>remain){
System.out.println(" Remain:"+remain+" ");
}
}while(take>remain);
System.err.println(" success,take:"+take+",remainis:"+(remain-take));
user.setMoney(remain-take);
}
/**
*permissiondeny
*/
publicstaticvoiddeny(){
try{
thrownewPermissionException("密码错误,请重新输进");
}catch(Exceptione){
System.out.println("密码错误,请重新输进");
}
}
publicstaticvoidmain(String[]args){
Useruser=newUser("3306","3306","JavaMonkey",26,1000);
createNewAccount(user);
login();
}
}
❻ Java中,map分为哪些种类
您好,提问者:
Map:HashMap、TreeMap、Hashtable。
1、HashMap:线程不安全,键、值不允许为null。
2、Hashtable:线程安全,键、值允许为null。
3、TreeMap:线程不安全、键、值不允许为null,底层二叉树。
❼ java中map有哪几种实现方式,并且比较异同
Map是一种把键对象和值对象进行关联的容器,而一个值对象又可以是一个Map,依次类推,这样就可形成一个多级映射。对于键对象来说,像Set 一样,一个Map容器中的键对象不允许重复,这是为了保持查找结果的一致性;如果有两个键对象一样,那你想得到那个键对象所对应的值对象时就有问题了,可 能你得到的并不是你想的那个值对象,结果会造成混乱,所以键的唯一性很重要,也是符合集合的性质的。当然在使用过程中,某个键所对应的值对象可能会发生变 化,这时会按照最后一次修改的值对象与键对应。对于值对象则没有唯一性的要求。你可以将任意多个键都映射到一个值对象上,这不会发生任何问题(不过对你的 使用却可能会造成不便,你不知道你得到的到底是那一个键所对应的值对象)。
Map有两种比较常用的实现:HashMap和TreeMap,WeakMap。HashMap 也用到了哈希码的算法,以便快速查找一个键,TreeMap则是对键按序存放,因此它便有一些扩展的方法,比如firstKey(),lastKey() 等,你还可以从TreeMap中指定一个范围以取得其子Map。键和值的关联很简单,用pub(Object key,Object value)方法即可将一个键与一个值对象相关联。用get(Object key)可得到与此key对象所对应的值对象,WeakMap这个用于内存自动释放的。
❽ java中map的常用遍历方法有哪些
ava中map的常用遍历的具体方法有:
一 、在for-each循环中使用entries来遍历。这是最常见的并且在大多数情况下也是最可取的遍历方式。在键值都需要时使用。
二、 在for-each循环中遍历keys或values。如果只需要map中的键或者值,你可以通过keySet或values来实现遍历,而不是用entrySet。
三、使用Iterator遍历。
四、通过键找值遍历(效率低)。
总结:如果仅需要键(keys)或值(values)使用方法二。如果你使用的语言版本低于java 5,或是打算在遍历时删除entries,必须使用方法三。否则使用方法一(键值都要)。
❾ java map 的用法
map是个接口
用都是用HasMap等实现Map接口的类
创建:Map<String,String> map = new HashMap<String,String>();
插入元素:map.put("1","a");
移除元素: map.remove("1");
清空: map.clear();
具体参照java API
java.uitl.HashMap
❿ java中map集合用法
Map map=new HashMap();//实例化map对象map.put("key","value");//存放值(值以键(key)-值(value)方式存放。)System.out.print(map.get("key").toString());//取值 根据键就可以取到值