集合遍历

集合遍历

经常会用到集合的遍历,但是还是记不住常用的遍历方法,每次都得到处去找,现在索性就都找好了放在博客里面,方便以后查阅的同时也顺带着水一篇博客。

一、map遍历

方法一:

System.out.println("通过Map.keySet遍历key和value:");
for (String key : map.keySet()) {
   System.out.println("key= "+ key + " and value= " + map.get(key));
 }

方法二:

System.out.println("通过Map.entrySet使用iterator遍历key和value:");
Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
while (it.hasNext()) {
   Map.Entry<String, String> entry = it.next();
   System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
  }

方法三:

System.out.println("通过Map.entrySet遍历key和value");
for (Map.Entry<String, String> entry : map.entrySet()) {
   System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
  }

方法四:

System.out.println("通过Map.values()遍历所有的value,但不能遍历key");
for (String v : map.values()) {
   System.out.println("value= " + v);
  }

  

二、set遍历

方法一:

Set<String> set = new HashSet<String>();
Iterator<String> it = set.iterator();
while (it.hasNext()) {
  String str = it.next();
  System.out.println(str);
}

方法二:

for (String str : set) {
      System.out.println(str);
}

方法三:

Set<Object> set = new HashSet<Object>();

for (Object obj: set) {
      if(obj instanceof Integer){
                int aa= (Integer)obj;
             }else if(obj instanceof String){
               String aa = (String)obj
             }
    System.out.println(aa);
} 

  

 

三、map按照value值排序

public static HashMap sortByValue(HashMap<String, Integer> map) {//desc  
	List list = new LinkedList(map.entrySet());  
	Collections.sort(list, new Comparator(){  
	       public int compare(Object o1, Object o2) {  
	                 return ((Comparable) ((Map.Entry)o2).getValue())  
	                    .compareTo(((Map.Entry)o1).getValue());  
	       }  
     });  
	 LinkedHashMap result = new LinkedHashMap();  
	  
	 for (Iterator it = list.iterator(); it.hasNext();) {  
	      Map.Entry entry = (Map .Entry) it.next();  
	      result.put(entry.getKey(), entry.getValue());  
	  }  
	  return result;  
	}  

  

  

转载于:https://www.cnblogs.com/zidiancao/p/3948750.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/109742.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • PhpStorm激活码2021.04(最新序列号破解)

    PhpStorm激活码2021.04(最新序列号破解),https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月19日
    46
  • JavaScript数组-冒泡排序

    JavaScript数组-冒泡排序数组的冒泡排序算法也算一道经典面试题了,这里也给大家分享一下JavaScript中关于数组的冒泡排序的写法和思路:先给大家上代码:<script>//冒泡排序:将数组中的数字按照从大到小或从小到大的顺序排序vararr=[2,4,5,1,3];for(vari=0;i<arr.length-1;i++){//外层循环管趟数,即数组的全部项数都排好一共需要比较多少次一趟排好一个,注意趟

    2022年5月18日
    40
  • intellij和pycharm_idea激活成功教程步骤

    intellij和pycharm_idea激活成功教程步骤写在最前:    可以的话,请购买产品,支持创作成果。因为IntelliJIDEA和Pycharm这一系列产品对于学生和老师都是forfree,所以,有教育邮箱的可以用邮箱认证,一年验证一次。下面是讲解激活成功教程方法了(针对2018版本系列产品):到官网下载你想要的IDE,这里用PyCharm为例,将鼠标移到PyCharm位置,会有Download按钮显示出来,点击,…

    2022年8月28日
    18
  • 安装Chrome驱动[通俗易懂]

    安装Chrome驱动[通俗易懂]相信许多小伙伴在学习selenium时候遇到驱动器无法运行的错误,就跟我一样,所以写一篇博客讲一讲如何安装这就是谷歌浏览器驱动没有安装成功而产生的报错。下面我给大家简单说说如何安装谷歌驱动器。Windows系统1.下载谷歌浏览器可以参考以下链接https://www.google.cn/intl/zh-CN/chrome然后检测自己的版本2.下载对应的Chrome驱动参考以下的链接http://npm.taobao.org/mirrors/chromedriver/应该可以看到以下

    2022年6月29日
    31
  • PHP Laravel系列之环境搭建( VirtualBox+Vagrant+Homestead+系列网址)

    PHP Laravel系列之环境搭建( VirtualBox+Vagrant+Homestead+系列网址)

    2021年10月28日
    46
  • SVM和logistic regression的一些对比

    SVM和logistic regression的一些对比

    2021年6月15日
    112

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号