TreeMap树映射取出对象的方式

2019-11-22 18:32:56 浏览数 (1)

1.直接获取该TreeMap集合中的关系: entrySet()

Map接口中的方法,返回值类型是该集合中的各个关系;返回值类型是:Set类型的Map.EntrySet类型;然后在通过Set集合中特有的元素取出方式:将集合中的各个元素迭代取出; 例子:

代码语言:javascript复制
 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()方法),然后在通过每个建获得各个建所对应的值

代码语言:javascript复制
 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转化为集合

代码语言:javascript复制
 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         }
  }

0 人点赞