Java TreeMap values() 方法



描述

Java TreeMap values() 方法用於返回此對映中包含的值的集合檢視。集合的迭代器按對應鍵的升序返回值。

宣告

以下是 java.util.TreeMap.values() 方法的宣告。

public Collection<V> values()

引數

返回值

方法呼叫返回此對映中包含的值的集合檢視。

異常

獲取整數、整數對TreeMap的值的集合示例

以下示例演示瞭如何使用 Java TreeMap values() 方法獲取對映中存在的值的集合檢視。我們建立了一個 Integer,Integer 對的 TreeMap 物件。然後添加了一些條目,並使用 values() 按升序列印值。

package com.tutorialspoint;

import java.util.String;

public class TreeMapDemo {
   public static void main(String[] args) {

      // creating maps 
      TreeMap<Integer, Integer> treemap = new TreeMap<>();
      SortedMap<Integer, Integer> treemapincl = new TreeMap<>();

      // populating tree map
      treemap.put(2, 2);
      treemap.put(1, 1);
      treemap.put(3, 3);
      treemap.put(6, 6);
      treemap.put(5, 5);      

      System.out.println("Getting collection from the map");
      Collection coll = treemap.values();
      System.out.println("Value of the collection: "+coll);      
   }    
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

Getting collection from the map
Value of the collection: [1, 2, 3, 4, 5]

獲取整數、字串對TreeMap的值的集合示例

以下示例演示瞭如何使用 Java TreeMap values() 方法獲取對映中存在的值的集合檢視。我們建立了一個 Integer,String 對的 TreeMap 物件。然後添加了一些條目,並使用 values() 按升序列印值。

package com.tutorialspoint;

import java.util.String;

public class TreeMapDemo {
   public static void main(String[] args) {

      // creating maps 
      TreeMap<Integer, String> treemap = new TreeMap<>();
      SortedMap<Integer, String> treemapincl = new TreeMap<>();

      // populating tree map
      treemap.put(2, "two");
      treemap.put(1, "one");
      treemap.put(3, "three");
      treemap.put(6, "six");
      treemap.put(5, "five");      

      System.out.println("Getting collection from the map");
      Collection coll = treemap.values();
      System.out.println("Value of the collection: "+coll);      
   }    
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

Getting collection from the map
Value of the collection: [one, two, three, five, six]

獲取整數、物件對TreeMap的值的集合示例

以下示例演示瞭如何使用 Java TreeMap values() 方法獲取對映中存在的值的集合檢視。我們建立了一個 Integer,Student 對的 TreeMap 物件。然後添加了一些條目,並使用 values() 按升序列印值。

package com.tutorialspoint;

import java.util.String;

public class TreeMapDemo {
   public static void main(String[] args) {

      // creating maps 
      TreeMap<Integer, Student> treemap = new TreeMap<>();
      SortedMap<Integer, Student> treemapincl = new TreeMap<>();

      // populating tree map
      treemap.put(2, "two");
      treemap.put(1, "one");
      treemap.put(3, "three");
      treemap.put(6, "six");
      treemap.put(5, "five");      

      System.out.println("Getting collection from the map");
      Collection coll = treemap.values();
      System.out.println("Value of the collection: "+coll);      
   }    
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   @Override
   public boolean equals(Object obj) {
      if(obj == null) return false;
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

Getting collection from the map
Value of the collection: [one, two, three, five, six]
java_util_treemap.htm
廣告