zoukankan      html  css  js  c++  java
  • HashMap的遍历和排序

    1.HashMap的遍历

    package com.sheepmu;
    
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Map.Entry;
    
    public class KMPText 
    {
    	public static void main(String[] args)
    	{
    		 Map<String,String> map=new HashMap<String,String>();
    		 map.put("key1", "bb");
    		 map.put("key0", "aaa");	
    		 map.put("key3", "dddd");	 
    		 map.put("key2", "cccccccc");
    		 
    		 //遍历方法0                  我个人最喜欢的遍历。集合的遍历还是用for-each的遍历是最爽滴~
    		 for(Map.Entry<String, String> entry:map.entrySet())//值和value都需要遍历时
    		 {
    			 String key= entry.getKey().toString();
    			 String value=entry.getValue().toString();
     		    	 System.out.println("entry--->"+entry);
    // 			 System.out.println("key---->"+key+"    value---->"+value+"   ");
    		 }
    		 
    		  
    		 //遍历方法1                       此方法效率也很高
    		 Iterator it=map.entrySet().iterator();
    		 while(it.hasNext())
    		 {
    			 Map.Entry entry=(Map.Entry) it.next();
    			 String key= entry.getKey().toString();
    			 String value=entry.getValue().toString();
     			 System.out.println("key---->"+key+"    value---->"+value+"   ");
    		 }
    		 
    	}
    	
    }
    

    2.HashMap的排序

     (1).方法:把map的entry取出来放到list里面,这样就相当于排list

              eg:对上面例子的HashMap按key从小到大排orvalue从长到短排

    package com.sheepmu;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    import java.util.Map.Entry;
    
    public class KMPText 
    {
    	public static void main(String[] args)
    	{
    		 Map<String,String> map=new HashMap<String,String>();
    		 map.put("key1", "bb");
    		 map.put("key0", "aaa");	
    		 map.put("key3", "dddd");	 
    		 map.put("key2", "cccccccc");
    		 
    		 //遍历 hashmap
    		 for(Map.Entry<String, String> entry:map.entrySet()) 
    		 {
    			 String key= entry.getKey().toString();
    			 String value=entry.getValue().toString();
     		 	 System.out.println("key---->"+key+"    value---->"+value+"   ");
    		 }
    		 //按要求排序hashmap
    		 List<Map.Entry<String, String>> list=new ArrayList<Map.Entry<String, String>>(map.entrySet());//!!!
    		  Collections.sort(list, new Comparator<Map.Entry<String, String>>(){//按key值字符串比较从小到大
    			@Override
    			public int compare(Entry<String, String> o1,Entry<String, String> o2) {	 
    				return o1.getKey().compareTo(o2.getKey());
    			}});
    		  
    		  System.out.println("list---->"+list);
    		  
    		  Collections.sort(list, new Comparator<Map.Entry<String, String>>(){//按value值字符串长度比较从大到小
    				@Override
    				public int compare(Entry<String, String> o1,Entry<String, String> o2) {				 
    					return o2.getValue().length()-o1.getValue().length();
    				}});
    			  
    			  System.out.println("list---->"+list);
    	}
    	
    }
    



    注:如果希望遍历后的顺序与put进去的顺序一致,则采用LinkendHashMap

    package com.sheepmu;
     
    import java.util.HashMap;
    import java.util.LinkedHashMap;
    import java.util.Map;
    
    public class KMPText 
    {
    	public static void main(String[] args)
    	{
    		 Map<String,String> map=new HashMap<String,String>();
    		 map.put("key1", "bb");
    		 map.put("key0", "aaa");	
    		 map.put("key3", "dddd");	 
    		 map.put("key2", "cccccccc");
    		 System.out.println("hashmap--->"+map); 
    		 
    		 Map<String,String> lmap=new LinkedHashMap<String,String>();
    		 lmap.put("key1", "bb");
    		 lmap.put("key0", "aaa");	
    		 lmap.put("key3", "dddd");	 
    		 lmap.put("key2", "cccccccc");
    		 System.out.println("linkedhashmap--->"+lmap); 
    		 		 
    	}
    	
    }
    




  • 相关阅读:
    C#基础视频教程5.3 如何编写简单的超级热键
    spring boot中注入jpa时报could not autowire.No beans of 'PersonRepository' type found
    SpringBoot中常用注解@Controller/@RestController/@RequestMapping的区别
    idea如何搭建springboot框架
    Fiddler建好代理后,能连到手机,但手机不能上网了是什么原因
    如何用Fiddler对Android应用进行抓包
    【fiddler】抓取https数据失败,全部显示“Tunnel to......443”
    将excel的数据导入到数据库后都乱码了是怎么回事
    java保存繁体字到数据库时就报错Incorrect string value: 'xF0xA6x8Dx8BxE5xA4...' for column 'name' at row 1
    将爬取的网页数据保存到数据库时报错不能提交JPA,Caused by: java.sql.SQLException: Incorrect string value: 'xF0x9Fx98xB6 xE2...' for column 'content' at row 1
  • 原文地址:https://www.cnblogs.com/oversea201405/p/3766890.html
Copyright © 2011-2022 走看看