Map HashMap 排序 迭代循环 修改值
HashMap dgzhMap = Dict.getDict("dgzh");
Iterator it_d = dgzhMap.entrySet().iterator();
while (it_d.hasNext()) {
Map.Entry entry_d = (Map.Entry) it_d.next();
Object key = entry_d.getKey();
Object value = entry_d.getValue();
value = value.toString().split("-")[0];
dgzhMap.put(key, value);
}
//hashmap是HashMap类型的对象 if(hashmap!=null || !hashmap.isEmpty())
{
Set set = hashmap.keySet();
Iterator it = set.iterator();
while(it.hasNext())
{
String key = (String)it.next();
Integer integer = (Integer)hashmap.get(key);
}
}
HashMap attMap;
Iterator i = attMap.entrySet().iterator();
while(i.hasNext()){
Object o = i.next();
String key = o.toString();
//这样就可以遍历该HashMap的key值了。
}
也可以
Object [] obja=attmap.keySet().toArray();
听我们的支持说遍历hashmap使用entryset快些,因大部分都是用keyset遍历的,也没有去想那么多。今天研究了一下,果然差了很多。
见示例,只是简单的hashmap信息。不多说了,大家把这个类在本地运行下,很容易看到结果。
import java.util.HashMap;
import java.util.Iterator;
import java.util.Calendar; public class HashMapTest { public static void main(String[] args) {
HashMap hashmap = new HashMap();
for(int i=0;i<1000;i++){
hashmap.put(""+i,"hello");
} long bs = Calendar.getInstance().getTimeInMillis();
Iterator iterator = hashmap.keySet().iterator();
//String value = "";
while(iterator.hasNext()) {
//value = hashmap.get(iterator.next());
System.out.println(hashmap.get(iterator.next()));
}
System.out.println(Calendar.getInstance().getTimeInMillis() - bs);
listHashMap();
} public static void listHashMap(){
java.util.HashMap hashmap = new java.util.HashMap();
for(int i=0;i<1000;i++){
hashmap.put(""+i,"hello");
}
long bs = Calendar.getInstance().getTimeInMillis();
//Set set = hashmap.entrySet() ;
java.util.Iterator it = hashmap.entrySet().iterator();
while(it.hasNext()){
java.util.Map.Entry entry = (java.util.Map.Entry)it.next();
// entry.getKey() 返回与此项对应的键
// entry.getValue() 返回与此项对应的值
System.out.println(entry.getValue());
}
System.out.println(Calendar.getInstance().getTimeInMillis() - bs);
} }
对于keySet其实是遍历了2次,一次是转为iterator,一次就从hashmap中取出key所对于的value。
而entryset只是遍历了第一次,他把key和value都放到了entry中,所以就快了。
对于我们做web的,可能不部分都是用vo对象或是form封装信息,所以用到hashmap时,其内存放的都是上面的对象。因此使用entryset遍历性能会有所提高。
hashmap使用很多,比如导入信息时就要用到,因大部分导入的信息要去判断是否有重复的信息,这样就可以利用containsKey来进行处理了,而不用在插入的时候去进行处理。
Java中对Map(HashMap,TreeMap,Hashtable等)的排序时间
首先简单说一下他们之间的区别:
HashMap: 最常用的Map,它根据键的HashCode 值存储数据,根据键可以直接获取它的值,具有很快的访问速度。HashMap最多只允许一条记录的键为Null(多条会覆盖);允许多条记录的值为 Null。非
首先简单说一下他们之间的区别:
HashMap: 最常用的Map,它根据键的HashCode 值存储数据,根据键可以直接获取它的值,具有很快的访问速度。HashMap最多只允许一条记录的键为Null(多条会覆盖);允许多条记录的值为 Null。非同步的。
TreeMap: 能够把它保存的记录根据键(key)排序,默认是按升序排序,也可以指定排序的比较器,当用Iterator 遍历TreeMap时,得到的记录是排过序的。TreeMap不允许key的值为null。非同步的。
Hashtable: 与 HashMap类似,不同的是:key和value的值均不允许为null;它支持线程的同步,即任一时刻只有一个线程能写Hashtable,因此也导致了Hashtale在写入时会比较慢。
LinkedHashMap: 保存了记录的插入顺序,在用Iterator遍历LinkedHashMap时,先得到的记录肯定是先插入的.在遍历的时候会比HashMap慢。key和value均允许为空,非同步的。
TreeMap默认按key进行升序排序,如果想改变默认的顺序,可以使用比较器:
Map<String,String> map = new TreeMap<String,String>(new Comparator<String>(){
public int compare(String obj1,String obj2){
//降序排序
return obj2.compareTo(obj1);
}
});
map.put("month", "The month");
map.put("bread", "The bread");
map.put("attack", "The attack");
Set<String> keySet = map.keySet();
Iterator<String> iter = keySet.iterator();
while(iter.hasNext()){
String key = iter.next();
System.out.println(key+":"+map.get(key));
}
如果要对TreeMap按照value的值进行排序,或者对HashMap,Hashtable,LinkedHashMap进行排序,则可以使用Map.Entry<K,V>接口结合List实现:
eg.1 对TreeMap按照value值升序:
List<Map.Entry<String,String>> mappingList = null;
Map<String,String> map = new TreeMap<String,String>();
map.put("aaaa", "month");
map.put("bbbb", "bread");
map.put("ccccc", "attack");
//通过ArrayList构造函数把map.entrySet()转换成list
mappingList = new ArrayList<Map.Entry<String,String>>(map.entrySet());
//通过比较器实现比较排序
Collections.sort(mappingList, new Comparator<Map.Entry<String,String>>(){
public int compare(Map.Entry<String,String> mapping1,Map.Entry<String,String> mapping2){
return mapping1.getValue().compareTo(mapping2.getValue());
}
});
for(Map.Entry<String,String> mapping:mappingList){
System.out.println(mapping.getKey()+":"+mapping.getValue());
}
eg.2 对HashMap(或Hashtable,LinkedHashMap)按照key的值升序:
List<Map.Entry<String,String>> mappingList = null;
Map<String,String> map = new HashMap<String,String>();
map.put("month", "month");
map.put("bread", "bread");
map.put("attack", "attack");
//通过ArrayList构造函数把map.entrySet()转换成list
mappingList = new ArrayList<Map.Entry<String,String>>(map.entrySet());
//通过比较器实现比较排序
Collections.sort(mappingList, new Comparator<Map.Entry<String,String>>(){
public int compare(Map.Entry<String,String> mapping1,Map.Entry<String,String> mapping2){
return mapping1.getKey().compareTo(mapping2.getKey());
}
});
说到遍历,首先应该想到for循环,然而map集合的遍历通常情况下是要这样在的,先要获得一个迭代器。
[java]
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循环也是可以的,很简洁吧~
[java]
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,统计出现次数最多和最少的元素
[java]
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.length;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.length;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);
}
}
for(Map.Entry<String,String> mapping:mappingList){
System.out.println(mapping.getKey()+":"+mapping.getValue());
}
Map HashMap 排序 迭代循环 修改值的更多相关文章
- Collections+Iterator 接口 | Map+HashMap+HashTable+TreeMap |
Collections+Iterator 接口 1. Collections 是一个操作 Set.List 和 Map 等集合的工具类 Collections 中提供了大量方法对集合元素进行排序.查询 ...
- 【Java进阶】---map集合排序
map集合排序 这篇文章讲的不仅仅是map排序,比如把对象按某一属性排序,它都可以解决这些问题. 比如,有N个对象,每个对象有个属性就是成绩,成绩分:优秀,良好,合格.那我们如何按 ...
- Java提高(5)---map集合排序
map集合排序 这篇文章讲的不仅仅是map排序,比如把对象按某一属性排序,它都可以解决这些问题. 比如,有N个对象,每个对象有个属性就是成绩,成绩分:优秀,良好,合格.那我们如何按照成绩的好坏进行排序 ...
- java如何对map进行排序详解(map集合的使用)
今天做统计时需要对X轴的地区按照地区代码(areaCode)进行排序,由于在构建XMLData使用的map来进行数据统计的,所以在统计过程中就需要对map进行排序. 一.简单介绍Map 在讲解Map排 ...
- Map的有序和无序实现类,与Map的排序
1.HashMap.Hashtable不是有序的: 2.TreeMap和LinkedHashMap是有序的(TreeMap默认 Key 升序,LinkedHashMap则记录了插入顺序). 今天做统计 ...
- 【java】关于Map的排序性的一次使用,有序的Map
关于Map的排序性的一次使用,有序的Map >>>>> hashmap是按key的hash码排序的,而treemap是利用comparator 进行key的自然排序的 / ...
- Java语言利用Collections.sort对Map,List排序
1.main方法包含TreeMap排序1,TreeMap排序2,HashMap排序,List<Integer>排序,List<Bean>排序,List<Map>排序 ...
- HashMap 排序
本文章,摘抄自:2018黑马程序最新面试题汇总 已知一个 HashMap<Integer,User>集合, User 有 name(String)和 age(int)属性.请写一个方法实现 ...
- HashMap排序的问题
那么已知一个HashMap<Integer,User>集合, User有name(String)和 age(int)属性.请写一个方法实现对HashMap 的排序功能,该方法接收 Hash ...
随机推荐
- poj2709
模拟题,在合成灰色的时候,每次取当前剩余最多的三种颜色,各取1mL合成.然后重新看剩余最多的是哪三个. #include <cstdio> #include <cstdlib> ...
- Linux学习笔记:常用命令grep、iconv、cp、mv、rm
本篇记录一些近期常用的命令. 一.grep过滤 grep过滤 不包含某些字符串 cat test.txt | grep -v '.jpg' 过滤jpg结尾的图片 cat test.txt | grep ...
- Kotlin中var和val的区别
Kotlin中有两个关键字定义变量,这两个关键字外形看着差别很小就只差了一个字母,但实际差别很大的. var是一个可变变量,这是一个可以通过重新分配来更改为另一个值的变量.这种声明变量的方式和Java ...
- Ubuntu 18.04 下配置 HAXM 加速 Android模拟器
Ubuntu 18.04 下配置 HAXM 加速 Android模拟器 最近在vmware环境下搭建ubuntu18.04开发环境,开始发现总是运行android模拟器在console提示加载如下错误 ...
- Python3 计算相关系数
# -*- coding: utf-8 -*- """ Created on Mon Jan 8 19:36:48 2018 @author: markli " ...
- Spring重复扫描导致事务失败的解决方案及深入分析
问题及日志使用Spring和mybatis,然后配置事务,出现SqlSession was not registered for synchronization because synchroniza ...
- 条件随机场(crf)及tensorflow代码实例
对于条件随机场的学习,我觉得应该结合HMM模型一起进行对比学习.首先浏览HMM模型:https://www.cnblogs.com/pinking/p/8531405.html 一.定义 条件随机场( ...
- android studio 查看大纲
就是 structure 面板 快捷键 Alt+7 === android studio 查看方法说明 点击菜单“View”-“Quick Documentation" 建议直接查看源代码文 ...
- SPOJ11414 COT3 博弈论 + Trie树合并
考虑对于每个子树从下往上依次考虑 对于叶子节点而言,如果可以染色,那么其\(sg\)值为\(1\),否则为\(0\) 考虑往上合并 如果选择了\(x\),那么后继状态就是其所有子树 如果选了其他子树中 ...
- 用.Net如何访问Linux下目录
很多Windows下的应用需要访问和监控Linux下的目录,本文便介绍如何实现. 只需要搭建配置samba服务,即可将Linux下的目录变得如同Windows下共享可写. 1.服务查询 默认情况下,L ...