js遍历java集合_Java如何遍历打印集合?

在Java编程中,如何遍历打印集合?

以下示例如何使用Util类的tMap.keySet(),tMap.values()和tMap.firstKey()方法打印集合。

package com.yiibai;

import java.util.*;

public class PrintCollection {

public static void main(String[] args) {

System.out.println("Tree Map Example!\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("Keys of tree map: " + tMap.keySet());

System.out.println("Values of tree map: " + tMap.values());

System.out.println("Key: 5 value: " + tMap.get(5) + "\n");

System.out.println("First key: " + tMap.firstKey() + " Value: " + tMap.get(tMap.firstKey()) + "\n");

System.out.println("Last key: " + tMap.lastKey() + " Value: " + tMap.get(tMap.lastKey()) + "\n");

System.out.println("Removing first data: " + tMap.remove(tMap.firstKey()));

System.out.println("Now the tree map Keys: " + tMap.keySet());

System.out.println("Now the tree map contain: " + tMap.values() + "\n");

System.out.println("Removing last data: " + tMap.remove(tMap.lastKey()));

System.out.println("Now the tree map Keys: " + tMap.keySet());

System.out.println("Now the tree map contain: " + tMap.values());

}

}

上述代码示例将产生以下结果。

Tree Map Example!

Keys of tree map: [1, 2, 3, 4, 5, 6, 7]

Values of tree map: [Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday]

Key: 5 value: Thursday

First key: 1 Value: Sunday

Last key: 7 Value: Saturday

Removing first data: Sunday

Now the tree map Keys: [2, 3, 4, 5, 6, 7]

Now the tree map contain: [Monday, Tuesday, Wednesday, Thursday, Friday, Saturday]

Removing last data: Saturday

Now the tree map Keys: [2, 3, 4, 5, 6]

Now the tree map contain: [Monday, Tuesday, Wednesday, Thursday, Friday]

¥ 我要打赏

纠错/补充

收藏

加QQ群啦,易百教程官方技术学习群

注意:建议每个人选自己的技术方向加群,同一个QQ最多限加 3 个群。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值