开发者社区> 问答> 正文

JAVA中Commons Collections - MapIterator如何理解?

JAVA中Commons Collections - MapIterator如何理解?

展开
收起
vncamyi27xznk 2021-11-13 23:53:12 286 0
1 条回答
写回答
取消 提交回答
  • "JDK Map 接口很难迭代,因为迭代要在 EntrySet 或 KeySet 对象上完成。 MapIterator 提供了对 Map 的简单迭代。 console.log(import org.apache.commons.collections4.IterableMap; import org.apache.commons.collections4.MapIterator; import org.apache.commons.collections4.map.HashedMap; public class MapIteratorTester { public static void main(String[] args) { IterableMap<String, String> map = new HashedMap<>(); map.put(""1"", ""One""); map.put(""2"", ""Two""); map.put(""3"", ""Three""); map.put(""4"", ""Four""); map.put(""5"", ""Five""); MapIterator<String, String> iterator = map.mapIterator(); while (iterator.hasNext()) { Object key = iterator.next(); Object value = iterator.getValue(); System.out.println(""key: "" + key); System.out.println(""Value: "" + value); iterator.setValue(value + ""_""); } System.out.println(map); } })

    它将打印以下结果: console.log(key: 3 Value: Three key: 5 Value: Five key: 2 Value: Two key: 4 Value: Four key: 1 Value: One {3=Three_, 5=Five_, 2=Two_, 4=Four_, 1=One_} )

    资料来源:《Java工程师成神之路(基础篇)》,链接:https://developer.aliyun.com/topic/download?id=923"

    2021-11-16 13:28:18
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载