日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

HashMap集合和TreeMap集合

發布時間:2025/3/16 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HashMap集合和TreeMap集合 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
---------------------------HashMap集合的遍歷 /第一種 package ji_he;import java.util.*;public class Example16 {public static void main(String[] args) {// TODO Auto-generated method stubMap map=new HashMap();map.put(1, "Jack");map.put(2, "Rose");map.put(3, "Lucy");map.put(3, "Lucy");Set kSet=map.keySet();//獲取鍵集合Iterator iterator=kSet.iterator();while (iterator.hasNext()) {Object key = iterator.next();Object value=map.get(key);System.out.println(key+":"+value);}}} ///第二種 package ji_he;import java.util.*;public class Example17 {public static void main(String[] args) {// TODO Auto-generated method stubHashMap map=new HashMap();map.put("1", "Jack");map.put("2", "Rose");map.put("3", "Lucy");map.put("3", "Lucy");Set entrySet=map.entrySet();Iterator iterator=entrySet.iterator();//獲取Iterator對象while (iterator.hasNext()) {Map.Entry entry=(Map.Entry) (iterator.next());//獲取集合中鍵值對的映射關系Object key = entry.getKey();//獲取Entry的鍵值Object value=entry.getValue();System.out.println(key+":"+value);}}} -------------------------------------TreeMap集合 ///TreeMap集合的遍歷 package ji_he;import java.util.*;public class Example20 {public static void main(String[] args) {// TODO Auto-generated method stubTreeMap tMap=new TreeMap();tMap.put("1", "Jack");tMap.put("2", "Rose");tMap.put("3", "Lucy");Set kSet=tMap.keySet();Iterator iterator=kSet.iterator();while (iterator.hasNext()) {Object key = (Object) iterator.next();Object values=tMap.get(key);System.out.println(key+":"+values);}}} //TreeMap集合自定義比較器package top.wthfeng.hello;import java.util.Comparator; import java.util.Map; import java.util.TreeMap;public class Map2Test{public static void main(String[]args){Map<String,String> map = new TreeMap<>(new Comparator<String>(){public int compare(String o1,String o2){return o2.compareTo(o1); //用正負表示大小值}});//以上4行可用下面一行lambda表達式代替//Map<String,String> map1 = new TreeMap<>((o1,o2)->o2.compareTo(o1));map.put("zdef","rfgh");map.put("asrg","zfg");map.put("rgd","dfgh");map.put("cbf","gddf");for(Map.Entry<String,String> entry:map.entrySet()){System.out.println("key:"+entry.getKey()+",:value:"+entry.getValue()); }} } //輸出結果(倒序): key:zdef,:value:rfgh key:rgd,:value:dfgh key:cbf,:value:gddf key:asrg,:value:zfg

總結

以上是生活随笔為你收集整理的HashMap集合和TreeMap集合的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。