如何高效的遍历HashMap 以及对key 进行排序
Map<Integer ,Object> map = new HashMap<Integer,Object>();
for(int i = 0; i<=100;i++){
map.put(i,i);
}
Iterator<Entry<Integer, Object>> iterator = map.entrySet().iterator();
while(iterator.hasNext()){
Map.Entry<Integer ,Object> entry = (Entry<Integer ,Object>)iterator.next();
Object key = entry.getKey();
Object value = entry.getValue(); }
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);
}
效率低,以后尽量少使用!
关于hashmap 按value排序
map_Data.put("A", "98");
map_Data.put("B", "50");
map_Data.put("C", "50");
map_Data.put("D", "25");
map_Data.put("E", "85");
System.out.println(map_Data);
List<Map.Entry<String, String>> list_Data = new ArrayList<Map.Entry<String, String>>(map_Data.entrySet());
{
public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2)
{
if(o2.getValue()!=null&&o1.getValue()!=null&&o2.getValue().compareTo(o1.getValue())>0){
return 1;
}else{
return -1;
}
}
});
System.out.println(list_Data);
如何高效的遍历HashMap 以及对key 进行排序的更多相关文章
- 遍历 HashMap 的 5 种最佳方式
使用 Iterator 遍历 HashMap EntrySet 使用 Iterator 遍历 HashMap KeySet 使用 For-each 循环迭代 HashMap 使用 Lambda 表达式 ...
- 遍历hashMap的两种方式
第一种: Map map = new HashMap(); Iterator iter = map.entrySet().iterator(); while (iter.hasNext()) { Ma ...
- java 中遍历hashmap 和hashset 的方法
一.java中遍历hashmap: for (Map.Entry<String, Integer> entry : tempMap.entrySet()) { String ...
- 使用多种方式实现遍历HashMap
今天讲解的主要是使用多种方式来实现遍历HashMap取出Key和value,首先在java中如果想让一个集合能够用for增强来实现迭代,那么此接口或类必须实现Iterable接口,那么Iterable ...
- java遍历Hashmap/Hashtable的几种方法
一>java遍历Hashtabe: import java.util.Hashtable; import java.util.Set; public class HashTableTest { ...
- 遍历hashMap对效率的影响
测试环境:jdk1.7.0_79\Processor 1.7 GHz Intel Core i5 遍历Map的方式有很多,通常场景下我们需要的是遍历Map中的Key和Value. 写了两个方法: pu ...
- java遍历hashMap、hashSet、Hashtable
一.遍历HashMap Map<Integer, String> map = new HashMap<Integer, String>(); 方法一:效率高 for(Entry ...
- Java遍历HashMap并修改(remove)(转载)
遍历HashMap的方法有多种,比如通过获取map的keySet, entrySet, iterator之后,都可以实现遍历,然而如果在遍历过程中对map进行读取之外的操作则需要注意使用的遍历方式和操 ...
- 四种遍历hashMap的方法及比较
学习怎样遍历Java hashMap及不同方法的性能. // hashMap的遍历 public void testHashMap() { Map<String, String> map ...
随机推荐
- Thinkphp中如何书写按照指定字段同步更新的ORM
群友提出一个问题,如何在更新某个字段的时候同步另一个字段数据过来,即 update table set column1 =column2 where xxx 写原生SQL当然可行,不过既然有ORM那就 ...
- php如何互换一个数组的首尾元素 中间不变 首尾互换
群里有人提出一个问题 如何互换一个数组的首尾元素 中间不变 首尾互换 代码如下: <?php $array=array(1,2,3,4,5,6,7,8,9,10); $first=array_s ...
- sessionStorage与localStorage
客户端存储数据的两个对象为: localStorage和sessionStorage一样都是用来存储客户端临时信息的对象. 他们均只能存储字符串类型的对象(虽然规范中可以存储其他原生类型的对象,但是目 ...
- 此类目的是防治序列化Json字符串时的循环引用问题-------最好解决方案
http://james.newtonking.com/json/help/index.html using Newtonsoft.Json;using System;using System.Col ...
- 【BZOJ2424】[HAOI2010]订货 最小费用流
[BZOJ2424][HAOI2010]订货 Description 某公司估计市场在第i个月对某产品的需求量为Ui,已知在第i月该产品的订货单价为di,上个月月底未销完的单位产品要付存贮费用m,假定 ...
- LeetCode 笔记系列 14 N-Queen II [思考的深度问题]
题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...
- LeetCode 笔记系列12 Trapping Rain Water [复杂的代码是错误的代码]
题目:Given n non-negative integers representing an elevation map where the width of each bar is 1, com ...
- 160520、MyBatis的几种批量操作
MyBatis中批量插入 方法一: <insert id="insertbatch" parameterType="Java.util.List"> ...
- 进击的RecyclerView入门一(简单上手)
虽然RecyclerView面世有一段时间了,但由于它的学习成本相对较高,很多码友只是粗略的认识了一下而没有细致的品味RecyclerView的真谛. 那么从现在开始我将带你装逼带你飞,一起领略Goo ...
- 基于ZooKeeper的服务注册中心
本文介绍基于ZooKeeper的Dubbo服务注册中心的原理. 1.ZooKeeper中的节点 ZooKeeper是一个树形结构的目录服务,支持变更推送,因此非常适合作为Dubbo服务的注册中心. 注 ...