android中怎么遍历list中的map集合map集合得到values值

最简单的修改HashMap value值的方法 -android100学习网
最简单的修改HashMap value值的方法
说到遍历,首先应该想到for循环,然而map集合的遍历通常情况下是要这样在的,先要获得一个迭代器。
[java] Map<Integer,String> map = new HashMap<
说到遍历,首先应该想到for循环,然而map集合的遍历通常情况下是要这样在的,先要获得一个迭代器。
Map&Integer,String& 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();&&&
Map&Integer,String& 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();&
实际上一个foreach循环也是可以的,很简洁吧~
for(Map.Entry&Integer,Integer& m:map.entrySet())&
&&&&&&&&&&&&&&& {&
&&&&&&&&&&&&&&&&&&& if(arr[i]==(int)m.getKey())&
&&&&&&&&&&&&&&&&&&&&&&& map.put((int)m.getKey(),(int)m.getValue()+1);&
&&&&&&&&&&&&&&& }&
for(Map.Entry&Integer,Integer& m:map.entrySet())
&&&&&if(arr[i]==(int)m.getKey())
&&&&&&map.put((int)m.getKey(),(int)m.getValue()+1);
附上一个完整的小程序例子。
随机生成长度为100的数组,数组元素为1到10,统计出现次数最多和最少的元素
mport java.util.*;&
class&& Count&&
&&& public void count(int[] arr)&
&&&&&&& int num=0;&
&&&&&&& Map&Integer,Integer& map=new HashMap&Integer,Integer&();&
&&&&&&& for(int i=1;i&=10;i++)&
&&&&&&& {&
&&&&&&&&&&& map.put(i,num);&
&&&&&&& }&
&&&&&&& for(int i=0;i&arr.i++)&
&&&&&&& {&
&&&&&&&&&&& /*Iterator it = map.entrySet().iterator();&&&&
&&&&&&&&&&& while(it.hasNext())
&&&&&&&&&&&&&&& {&&&&&
&&&&&&&&&&&&&&&&&&& Map.Entry m=(Map.Entry)it.next();
&&&&&&&&&&&&&&&&&&& if(arr[i]==(int)m.getKey())
&&&&&&&&&&&&&&&&&&&&&&& map.put((int)m.getKey(),(int)m.getValue()+1);
&&&&&&&&&&&&&&& }*/&
&&&&&&&&&&&&&&& for(Map.Entry&Integer,Integer& m:map.entrySet())&
&&&&&&&&&&&&&&& {&
&&&&&&&&&&&&&&&&&&& if(arr[i]==(int)m.getKey())&
&&&&&&&&&&&&&&&&&&&&&&& map.put((int)m.getKey(),(int)m.getValue()+1);&
&&&&&&&&&&&&&&& }&
&&&&&&& }&
&&&&&&& for(Map.Entry&Integer,Integer& m:map.entrySet())&
&&&&&&& {&
&&&&&&&&&&& System.out.println(""+m.getKey()+"出现的次数为:"+m.getValue()+"次");&
&&&&&&& }&
&&& public static void main(String[] args)&&
&&&&&&& Random rd=new Random();&
&&&&&&& int[] arr=new int[100];&
&&&&&&& for(int i=0;i&100;i++)&
&&&&&&& {&
&&&&&&&&&&& arr[i]=rd.nextInt(10)+1;&
&&&&&&& }&
&&&&&&& new Count().count(arr);&
import java.util.*;
class&Count
&public void count(int[] arr)
&&int num=0;
&&Map&Integer,Integer& map=new HashMap&Integer,Integer&();
&&for(int i=1;i&=10;i++)
&&&map.put(i,num);
&&for(int i=0;i&arr.i++)
&&&/*Iterator it = map.entrySet().iterator();&&&
&&&while(it.hasNext())
&&&&&Map.Entry m=(Map.Entry)it.next();
&&&&&if(arr[i]==(int)m.getKey())
&&&&&&map.put((int)m.getKey(),(int)m.getValue()+1);
&&&&for(Map.Entry&Integer,Integer& m:map.entrySet())
&&&&&if(arr[i]==(int)m.getKey())
&&&&&&map.put((int)m.getKey(),(int)m.getValue()+1);
&&for(Map.Entry&Integer,Integer& m:map.entrySet())
&&&System.out.println(""+m.getKey()+"出现的次数为:"+m.getValue()+"次");
&public static void main(String[] args)
&&Random rd=new Random();
&&int[] arr=new int[100];
&&for(int i=0;i&100;i++)
&&&arr[i]=rd.nextInt(10)+1;
&&new Count().count(arr);android里面map值如何用数组来获得_百度知道
android里面map值如何用数组来获得
map.values() 是集合,有如下转成数组方法abstract
toArray(T[] array)Returns an array containing all elements contained in this Collection.
toArray()Returns a new array containing all elements contained in this Collection.
android-sdk-windows/docs/reference/java/util/Collection.html
其他类似问题
为您推荐:
就相反呗,循环用put方法
不行的,数组是无法追加的。。
普通数组当然无法追加,动态数组可以
android的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁HashMap 的遍历key与value的方法,hashmap历keyvalue-android100学习网
HashMap 的遍历key与value的方法,hashmap历keyvalue
HashMap 的遍历key与value的方法,hashmap历keyvalue
HashMap最常用的用法是根据key增删改查value,有时候会需要知道一个HashMap有多少个keys以
HashMap 的遍历key与value的方法,hashmap历keyvalue
HashMap最常用的用法是根据key增删改查value,有时候会需要知道一个HashMap有多少个keys以及这些keys都是什么,可以用下面的代码实现。
Map map = new HashMap();
  Iterator iter = map.entrySet().iterator();
  while (iter.hasNext()) {
  Map.Entry entry = (Map.Entry) iter.next();
  Object key = entry.getKey();
  Object val = entry.getValue();
Map map = new HashMap();
  Iterator iter = map.keySet().iterator();
  while (iter.hasNext()) {
  Object key = iter.next();
  Object val = map.get(key);
方法1比方法2高效一些
HashMap和ArrayList结合起来用,向HashMap中存值时 把name存入ArrayList中:HashMap a = new HashMap(); ArrayList al = new ArrayList();a.put("name1", "abcdef"); // key是name,value是字符串abcdef al.add("name1");a.put("name2","me"); al.add("name2");a.put("name3","you"); al.add("name3");a.put("name4","he"); al.add("name4");for(int i=0;i&al.size();i++){System.out.println(a.get(al.get(i)));}java中怎么遍历HashMap-android100学习网
java中怎么遍历HashMap
  一.  HashMap staff = new HashMap();  添加关键字值对,自己写遍历  Set entries = staff.entrySet();  Iterator
  HashMap staff = new HashMap();
  添加关键字值对,自己写遍历
  Set entries = staff.entrySet();
  Iterator iter = entries.iterator();
  while(iter.hasNext())
   Map.Entry entry = (Map.Entry)iter.next();
   Object key = entry.getKey();得么关键字
   Object value = entry.getValue();得到值
  Map map = new HashMap();
  for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) {
   Map.Entry entry = (Map.Entry) iter.next(); //map.entry 同时取出键值对
   Object key = entry.getKey();
   Object val = entry.getValue();
  Map map = new HashMap();
  for (Iterator iter = map.keySet().iterator(); iter.hasNext();) {
   Object key = iter.next();
   Object val = map.get(key);
  Iterator是迭代器
  对于keySet其实是遍历了2次,一次是转为iterator,一次就从hashmap中取出key所对于的value。
  而entryset只是遍历了第一次,他把key和value都放到了entry中,所以就快了。
  对于我们做web的,可能不部分都是用vo对象或是form封装信息,所以用到hashmap时,其内存放的都是上面的对象。因此使用entryset遍历性能会有所提高。
  hashmap使用很多,比如导入信息时就要用到,因大部分导入的信息要去判断是否有重复的信息,这样就可以利用containsKey来进行处理了,而不用在插入的时候去进行处理。

我要回帖

更多关于 android遍历map集合 的文章

 

随机推荐