HashMap遍历之EntrySet

2023-10-21 20:52
文章标签 遍历 hashmap entryset

本文主要是介绍HashMap遍历之EntrySet,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

entry可以获取key和value的具体值

当entry<key,value> 

 public static void main(String[] args) {Map map = new HashMap();map.put("邓超", "孙俪");//替换map.put("王宝强", "马蓉");//okmap.put(null, "刘亦菲");//plmap.put("鹿晗", null);//okmap.put("lu汉", "刘亦菲");map.put("沫年", "刘亦菲");//okSystem.out.println(map);System.out.println("\n==========迭代器===========\n");Iterator ito3 = entrySet.iterator();while(ito3.hasNext()){Map.Entry entry2 = (Map.Entry) ito3.next();System.out.println(entry2.getValue());}//        Iterator ito3 = entrySet.iterator();
//        while(ito3.hasNext()){
//          Object entry = ito3.next();System.out.println(entry);//HashMap$Node ---实现 > Map.Entry (getKey,getValue)
//          //向下转型 Map.Entry
//          Map.Entry m = (Map.Entry) entry;
//            System.out.print(m.getValue()+" ");
//        }}

当entry<key,Object>,此时value = Object  

    public static void main(String[] args) {/*** 使用HashMapp添加三个员工对象,要求* 键:员工id* 值:员工对象** 并且遍历显示工资>18000的员工(遍历方式最少两种)* 员工类:姓名、工资、员工id*/Map map = new HashMap();map.put(1, new Employee("smith", 18000, "20001206"));map.put(2, new Employee("tom", 28000, "20011212"));map.put(3, new Employee("smith", 10000, "20111010"));System.out.println("员工的个数为:" + map.size());System.out.println("=========第三种遍历方式=========");Set set = map.entrySet();//先将map转化为entrySetIterator ito2 = set.iterator();//构建迭代器while (ito2.hasNext()){//迭代器循环条件Map.Entry entry = (Map.Entry) ito2.next();//将entrySet 转化为entry,并且向下移动指针//通过entry来获取key和valueEmployee emp = (Employee) entry.getValue();//将包entry向下转型为EmployeeSystem.out.println(emp);}
}

这篇关于HashMap遍历之EntrySet的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/256908

相关文章

C++中使用vector存储并遍历数据的基本步骤

《C++中使用vector存储并遍历数据的基本步骤》C++标准模板库(STL)提供了多种容器类型,包括顺序容器、关联容器、无序关联容器和容器适配器,每种容器都有其特定的用途和特性,:本文主要介绍C... 目录(1)容器及简要描述‌php顺序容器‌‌关联容器‌‌无序关联容器‌(基于哈希表):‌容器适配器‌:(

leetcode105 从前序与中序遍历序列构造二叉树

根据一棵树的前序遍历与中序遍历构造二叉树。 注意: 你可以假设树中没有重复的元素。 例如,给出 前序遍历 preorder = [3,9,20,15,7]中序遍历 inorder = [9,3,15,20,7] 返回如下的二叉树: 3/ \9 20/ \15 7   class Solution {public TreeNode buildTree(int[] pr

PHP实现二叉树遍历(非递归方式,栈模拟实现)

二叉树定义是这样的:一棵非空的二叉树由根结点及左、右子树这三个基本部分组成,根据节点的访问位置不同有三种遍历方式: ① NLR:前序遍历(PreorderTraversal亦称(先序遍历)) ——访问结点的操作发生在遍历其左右子树之前。 ② LNR:中序遍历(InorderTraversal) ——访问结点的操作发生在遍历其左右子树之中(间)。 ③ LRN:后序遍历(PostorderT

react笔记 8-17 属性绑定 class绑定 引入图片 循环遍历

1、绑定属性 constructor(){super()this.state={name:"张三",title:'我是一个title'}}render() {return (<div><div>aaaaaaa{this.state.name}<div title={this.state.title}>我是一个title</div></div></div>)} 绑定属性直接使用花括号{}   注

HashMap中常用的函数

假设如下HashMap<String, Integer> map = new HashMap<>();获取value值1、返回key为a的valueget(a)2、返回key为a的value,若没有该key返回0getOrDefault(a,0)新增键值对1、新增键值对(a,1)put(a,1)2、如果key为a的键不存在,则存入键值对(a,1)putIfAbsent(a,1)3

hashmap的存值,各种遍历方法

package com.jefflee;import java.util.HashMap;import java.util.Iterator;import java.util.Map;public class HashmapTest {// 遍历Hashmap的四种方法public static void main(String[] args) {//hashmap可以存一个null,把

Knight Moves -uva 简单的BFS遍历

昨天刚学了BFS的遍历,在uva上找了个题敲了出来,感觉还不错,最近敲代码挺有手感的,希望这种状态保持下去 #include<iostream>#include<stdio.h>#include<stdlib.h>#include<string.h>#define MAX_SIZE 10 + 5#define LEN 100 + 10using namespace std;in

笔试强训,[NOIP2002普及组]过河卒牛客.游游的水果大礼包牛客.买卖股票的最好时机(二)二叉树非递归前序遍历

目录 [NOIP2002普及组]过河卒 牛客.游游的水果大礼包 牛客.买卖股票的最好时机(二) 二叉树非递归前序遍历 [NOIP2002普及组]过河卒 题里面给的提示很有用,那个马的关系,后面就注意,dp需要作为long的类型。 import java.util.Scanner;// 注意类名必须为 Main, 不要有任何 package xxx 信息publ

二叉树的层次遍历(10道)

(写给未来遗忘的自己) 102.二叉数的层序遍历(从上到下) 题目: 代码: class Solution { public: vector<vector<int>> levelOrder(TreeNode* root) { vector<vector<int>> result; queue<TreeNode*> node; if (root == nullptr) {

关于双指针遍历

今晚跟一个朋友突然问我,你懂双指针遍历吗?并叫我敲出代码。当时自己愣住了,但是还是写出来了,第一个版本是: #include <iostream> using namespace std; int main(int argc, char** argv, char** arge) { cout<<"输出参数个数:"<<argc<<endl; cout<<"输出字符串数组:"<<endl; f