获取value
参考:以集合形式获取value
关于Map集合中,迭代出其所有元素方法
1.直接遍历 先取set再取get
先调用.keySet(),在调用.get()
2.将map集合转化为Set集合
Map.Entry<> 是一个静态内部类 其中保存着指向Node结点的引用
所以在转化和迭代的时候 要带上
先调用.entrySet() 转化为Set集合
再调用Set集合里的迭代器
//Iterator<Map.Entry<Integer,String>> it=m.entrySet().iterator();
Set<Map.Entry<Integer,String>> x=m.entrySet();
Iterator<Map.Entry<Integer,String>> it=x.iterator();
while (it.hasNext()){
Map.Entry<Integer,String> entry=it.next();
System.out.println("key值:"+entry.getKey()+",对应的value值:"+entry.getValue());
}
3.大容量下,先转Set,再使用foreach
4.直接遍历调用m.values()
package com.hdu.hashmap01;
import java.util.*;
public class hashmap02 {
public static void main(String[] args) {
//HashMap key为null时 只能有一个 且会被覆盖
/* Map map=new HashMap();
map.put(null,null);
System.out.println("HashMap单放null大小"+map.size());
map.put(null,123);
System.out.println("HashMap两个nullkey大小"+map.size());
//Hashtable 的key和value都不能为null
Map map2=new Hashtable();
map2.put(null,null);//空指针异常*/
/*遍历HashMap*/
Map<Integer,String> m=new HashMap<>();
m.put(1949,"新中国");
m.put(1997,"香港");
m.put(1999,"澳门");
m.put(2033,"台湾");
/*直接遍历 先取set再取get*/
System.out.println("***********直接遍历 先取set再取get***********");
for (Integer key01s:m.keySet()) {
System.out.println("key:"+key01s+",对应的value值:"+m.get(key01s));
}
/*将map集合转化为Set集合*/
//entry里的setValue什么意思
System.out.println("***********将map集合转化为Set集合***********");
//Iterator<Map.Entry<Integer,String>> it=m.entrySet().iterator();
Set<Map.Entry<Integer,String>> x=m.entrySet();
Iterator<Map.Entry<Integer,String>> it=x.iterator();
while (it.hasNext()){
Map.Entry<Integer,String> entry=it.next();
System.out.println("key值:"+entry.getKey()+",对应的value值:"+entry.getValue());
//System.out.println(entry.setValue("蒙古"));//entry.setValue("蒙古");
}
/*大容量下情况 使用Map转Set集合*/
System.out.println("***********大容量下情况 使用Map转Set集合***********");
for (Map.Entry<Integer,String> entry:m.entrySet()) {
System.out.println("key值:"+entry.getKey()+"对应的value值"+entry.getValue());
}
/*通过Map.values()遍历所有的value,但不能遍历key*/
System.out.println("***********通过Map.values()遍历所有的value,但不能遍历key***********");
for (String s:m.values()) {
System.out.println("value:"+s);
}
}
}
选择性输出1
package com.hdu.hashmap01;
import com.sun.deploy.panel.ITreeNode;
import java.util.*;
public class HashMapTest04 {
public static void main(String[] args) {
Employee02<String,Double,Integer> e001=new Employee02<>("张三",16800.0,116040500);
Employee02<String,Double,Integer> e002=new Employee02<>("李四",18000.5,116040600);
Employee02<String,Double,Integer> e003=new Employee02<>("王五",19800.0,116040700);
Employee02<String,Double,Integer> e004=new Employee02<>("刘六",22222.0,116040800);
// Employee02<String,Double,Integer> e005=new Employee02<>("刘六",22222.0,116040900);
HashMap<Integer,Employee02> hashMap=new HashMap<>();
hashMap.put(e001.getId(),e001);
hashMap.put(e002.getId(),e002);
hashMap.put(e003.getId(),e003);
hashMap.put(e004.getId(),e004);
System.out.println("初次添加元素"+hashMap);
System.out.println("第一种遍历方法:先取set再取get");
/*第一种遍历方法:先取set再取get*/
for (Integer key01:hashMap.keySet()) {
System.out.println("key:"+key01+",对应的value值:"+hashMap.get(key01));
}
System.out.println("第一种遍历方法:输出salary在18000以上的信息");
//输出salary在18000以上的信息
for (Integer key01:hashMap.keySet()) {
double s=Double.parseDouble(hashMap.get(key01).getSalary().toString());
if(s>=18000){
System.out.println("key:"+key01+",对应的value值:"+hashMap.get(key01));
}
}
System.out.println("第二种遍历方法:转化为Set集合");
//Map.Entry<> 是一个静态内部类 其中保存着指向Node结点的引用
//Map.Entry<> 是一个内部类,所以在转化和迭代的时候 要带上
Set<Map.Entry<Integer,Employee02>> entry=hashMap.entrySet();
Iterator<Map.Entry<Integer,Employee02>> iterator=entry.iterator();
while(iterator.hasNext()){
Map.Entry<Integer,Employee02> key=iterator.next();
//选择性输出
double s=Double.parseDouble(hashMap.get(key.getKey()).getSalary().toString());
if(s>=18000){
System.out.println("key:"+key+",对应的value值:"+hashMap.get(key));
}
}
System.out.println("转化为Map.Entry 类型");
for (Map.Entry<Integer,Employee02> e:hashMap.entrySet()) {
System.out.println("key:"+e.getKey()+",对应的value值"+e.getValue().toString());
}
}
}
class Employee02<N,S,I>{
private N name;
private S salary;
private I id;
public Employee02(N name, S salary, I id) {
this.name = name;
this.salary = salary;
this.id = id;
}
public N getName() {
return name;
}
public void setName(N name) {
this.name = name;
}
public S getSalary() {
return salary;
}
public void setSalary(S salary) {
this.salary = salary;
}
public I getId() {
return id;
}
public void setId(I id) {
this.id = id;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Employee02)) return false;
Employee02<?, ?, ?> that = (Employee02<?, ?, ?>) o;
return Objects.equals(getName(), that.getName()) &&
Objects.equals(getId(), that.getId());
}
@Override
public int hashCode() {
return Objects.hash(getId());
}
@Override
public String toString() {
return "Employee02{" +
"name=" + name +
", salary=" + salary +
", id=" + id +
'}';
}
}