Home > Java > Java Tutorial > body text

Java Example - Collection Output

黄舟
Release: 2017-01-21 11:17:43
Original
1095 people have browsed it

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());
   }
}
Copy after login

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]
Copy after login

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)!


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!