java hashmap 修改_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值了。

}

当然也可以遍历Map.Entry项,值。方法类似。

也可以

Object [] obja=attmap.keySet().toArray();

听我们的支持说遍历hashmap使用entryset快些,因大部分都是用keyset遍历的,也没有去想那么多。今天研究了一下,果然差了很多。

见示例,只是简单的hashmap信息。不多说了,大家把这个类在本地运行下,很容易看到结果。

importjava.util.HashMap;importjava.util.Iterator;importjava.util.Calendar;public classHashMapTest {public static voidmain(String[] args) {

HashMap hashmap= newHashMap();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 voidlistHashMap(){

java.util.HashMap hashmap= newjava.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 map = new TreeMap(new Comparator(){

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 keySet = map.keySet();

Iterator iter = keySet.iterator();

while(iter.hasNext()){

String key = iter.next();

System.out.println(key+":"+map.get(key));

}

如果要对TreeMap按照value的值进行排序,或者对HashMap,Hashtable,LinkedHashMap进行排序,则可以使用Map.Entry接口结合List实现:

eg.1 对TreeMap按照value值升序:

List> mappingList = null;

Map map = new TreeMap();

map.put("aaaa", "month");

map.put("bbbb", "bread");

map.put("ccccc", "attack");

//通过ArrayList构造函数把map.entrySet()转换成list

mappingList = new ArrayList>(map.entrySet());

//通过比较器实现比较排序

Collections.sort(mappingList, new Comparator>(){

public int compare(Map.Entry mapping1,Map.Entry mapping2){

return mapping1.getValue().compareTo(mapping2.getValue());

}

});

for(Map.Entry mapping:mappingList){

System.out.println(mapping.getKey()+":"+mapping.getValue());

}

eg.2 对HashMap(或Hashtable,LinkedHashMap)按照key的值升序:

List> mappingList = null;

Map map = new HashMap();

map.put("month", "month");

map.put("bread", "bread");

map.put("attack", "attack");

//通过ArrayList构造函数把map.entrySet()转换成list

mappingList = new ArrayList>(map.entrySet());

//通过比较器实现比较排序

Collections.sort(mappingList, new Comparator>(){

public int compare(Map.Entry mapping1,Map.Entry mapping2){

return mapping1.getKey().compareTo(mapping2.getKey());

}

});

说到遍历,首先应该想到for循环,然而map集合的遍历通常情况下是要这样在的,先要获得一个迭代器。

[java]

Map 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 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 m:map.entrySet())

{

if(arr[i]==(int)m.getKey())

map.put((int)m.getKey(),(int)m.getValue()+1);

}

for(Map.Entry 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 map=new HashMap();

for(int i=1;i<=10;i++)

{

map.put(i,num);

}

for(int i=0;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 m:map.entrySet())

{

if(arr[i]==(int)m.getKey())

map.put((int)m.getKey(),(int)m.getValue()+1);

}

}

for(Map.Entry 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 map=new HashMap();

for(int i=1;i<=10;i++)

{

map.put(i,num);

}

for(int i=0;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 m:map.entrySet())

{

if(arr[i]==(int)m.getKey())

map.put((int)m.getKey(),(int)m.getValue()+1);

}

}

for(Map.Entry 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 mapping:mappingList){

System.out.println(mapping.getKey()+":"+mapping.getValue());

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值