Home  >  Article  >  Java  >  Java Example - Collection Output

Java Example - Collection Output

黄舟
黄舟Original
2017-01-21 11:17:431210browse

The following example demonstrates how to use the tMap.keySet(), tMap.values() and tMap.firstKey() methods of the Java Util class to output the set elements:

/*
 author by w3cschool.cc
 Main.java
 */

import java.util.*;

public class Main{
   public static void main(String[] args) {
      System.out.println("TreeMap 实例!\n");
      TreeMap tMap = new TreeMap();
      tMap.put(1, "Sunday");
      tMap.put(2, "Monday");
      tMap.put(3, "Tuesday");
      tMap.put(4, "Wednesday");
      tMap.put(5, "Thursday");
      tMap.put(6, "Friday");
      tMap.put(7, "Saturday");
      System.out.println("TreeMap 键:" 
      + tMap.keySet());
      System.out.println("TreeMap 值:" 
      + tMap.values());
      System.out.println("键为 5 的值为: " + tMap.get(5)+ "\n");
      System.out.println("第一个键: " + tMap.firstKey() 
      + " Value: " 
      + tMap.get(tMap.firstKey()) + "\n");
      System.out.println("最后一个键: " + tMap.lastKey() 
	  + " Value: "+ tMap.get(tMap.lastKey()) + "\n");
      System.out.println("移除第一个数据: " 
      + tMap.remove(tMap.firstKey()));
      System.out.println("现在 TreeMap 键为: " 
      + tMap.keySet());
      System.out.println("现在 TreeMap 包含: " 
      + tMap.values() + "\n");
      System.out.println("移除最后一个数据: " 
      + tMap.remove(tMap.lastKey()));
      System.out.println("现在 TreeMap 键为: " 
      + tMap.keySet());
      System.out.println("现在 TreeMap 包含: " 
      + tMap.values());
   }
}

The above code runs the output The result is:

TreeMap 实例!

TreeMap 键:[1, 2, 3, 4, 5, 6, 7]
TreeMap 值:[Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday]
键为 5 的值为: Thursday

第一个键: 1 Value: Sunday

最后一个键: 7 Value: Saturday

移除第一个数据: Sunday
现在 TreeMap 键为: [2, 3, 4, 5, 6, 7]
现在 TreeMap 包含: [Monday, Tuesday, Wednesday, Thursday, Friday, Saturday]

移除最后一个数据: Saturday
现在 TreeMap 键为: [2, 3, 4, 5, 6]
现在 TreeMap 包含: [Monday, Tuesday, Wednesday, Thursday, Friday]

The above is the content of the Java instance-collection output. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn