TreeMap樹映射取出對象的方式

1.直接獲取該TreeMap集合中的關係:
entrySet()java

  Map接口中的方法,返回值類型是該集合中的各個關係;返回值類型是:Set類型的Map.EntrySet類型;而後在經過Set集合中特有的元素取出方式:將集合中的各個元素迭代取出;
例子:this

 1 import java.util.*;
 2 class MapDemo{
 3 pulbic static void main(String args[]){
 4 TreeMap<String,String> tr=new TreeMap<String,String>();
 5 tr.put("asdfda","asdfd");
 6 tr.put("asdfda","asdfd");
 7 tr.put("asdfda","asdfd");
 8 tr.put("asdfda","asdfd");
 9 Set<Map.EntrySet<String,String>> entryset=tr.entrySet();
10 //將TreeSet中的各個映射關係經過他自身提供的方法(entrySet())轉存到Set集合中,目的是爲了使用Set集合中迭代器取出方法
11 Iterator<Map.Entry<String,String>> it=entryset.iterator();//新建一個迭代器,準備遍歷整個Set<Map.EntrySet<String,String>>集合;
12 while(it.hasNext()){
13 Map.Entry<String,String> en=it.next();//
14 System.out.println(en.getKey()+":"+en.getValue());//在迭代每個元素的同時,同時調用Map.Entry中的方法分別獲取鍵和值
15 }
16 }
17 }

 


2.首先得到TreeSet集合中的全部的建(keySet()方法),而後在經過每一個建得到各個建所對應的值spa

 1 import java.util.*;
 2 class MapDemo4{
 3 pulbic static void main(String args[]){
 4 TreeMap<String,String> tr=new TreeMap<String,String>();
 5 tr.put("luwenxiang0","123");
 6 tr.put("luwenxiang1","123");
 7 tr.put("luwenxiang2","123");
 8 tr.put("luwenxiang3","123");
 9 tr.put("luwenxiang4","123");
10 Set<String> arr=tr.keySet();
11 Iterator<String> it=arr.iterator();
12 while(it.hasNext()){
13 String str=it.next();
14 System.out.println(str+"::"+tr.get(str));
15 }
16 }
17 }

 

3.將map轉化爲集合code

 1 package com.Champter15;
 2 
 3 import java.util.Collection;
 4 import java.util.Iterator;
 5 import java.util.Set;
 6 import java.util.TreeMap;
 7 
 8 class UDiskPrice implements Comparable<UDiskPrice>{
 9     int price;
10     public UDiskPrice(int price){
11         this.price = price;
12     }
13     public int compareTo(UDiskPrice uDiskPrice){
14         if(this.price-uDiskPrice.price==0) return 1;
15         else return this.price-uDiskPrice.price;
16     }
17 }
18 
19 class UDiskCapacity implements Comparable<UDiskCapacity>{
20     int capacity;
21     public UDiskCapacity(int capacity){
22         this.capacity = capacity;
23     }
24     public int compareTo(UDiskCapacity uDiskCapacity){
25         if(this.capacity-uDiskCapacity.capacity==0) return 1;
26         else return this.capacity-uDiskCapacity.capacity;
27     }
28 }
29 
30 public class Work3_3 {
31     public static void main(String[] args) {     
32   TreeMap<UDiskPrice,UDiskCapacity> uDiskTreeMap = new TreeMap<>();
33         uDiskTreeMap.put(new UDiskPrice(22),new UDiskCapacity(16));
34         uDiskTreeMap.put(new UDiskPrice(11),new UDiskCapacity(8));
35         uDiskTreeMap.put(new UDiskPrice(33),new UDiskCapacity(64));
36         uDiskTreeMap.put(new UDiskPrice(55),new UDiskCapacity(256));
37         uDiskTreeMap.put(new UDiskPrice(44),new UDiskCapacity(128));
38 
39      Collection<UDiskCapacity> collection = uDiskTreeMap.values();//因爲map沒有迭代器,將映射的值存到集合中
40        Iterator<UDiskCapacity> iterator = collection.iterator();//使用集合才自帶的迭代器訪問值,值的類型爲UDiskCapacity
41         while (iterator.hasNext()){
42             UDiskCapacity uDiskCapacity = iterator.next();//使用UDiskCapacity類型聲明的對象變量接收
43             System.out.println("按照價格升序的U盤容量:"+uDiskCapacity.capacity);
44         }  }
相關文章
相關標籤/搜索