Map六大遍历方式
-
KeySet:获取所有键
-
entrySet: 获取所有关系
-
values: 获取所有的值
import java.util.*;
public class MapFor {
public static void main(String[] args) {
HashMap hashMap = new HashMap();
hashMap.put(1,"Jack");
hashMap.put(2,"Tom");
hashMap.put(3,"CiCi");
hashMap.put(4,"CoCo");
hashMap.put(5,"Rose");
//1.先取出所有的key,通过key 取出对应的Value
System.out.println("=============取出key实现===============");
Set set = hashMap.keySet();
//增强for
for (Object o : set) {
System.out.println("增强for方式:" + o + "-" + hashMap.get(o));
}
Iterator iterator = set.iterator();
while(iterator.hasNext()){
Object next = iterator.next();
System.out.println("iterator方式:" + next + "-" + hashMap.get(next));
}
//2.直接取出value
System.out.println("=============取出value实现===============");
Collection values = hashMap.values();
for (Object o : values) {
System.out.println("增强for方式:" + o);
}
Iterator iterator1 = values.iterator();
while(iterator1.hasNext()){
Object next = iterator1.next();
System.out.println("iterator方式:" + next);
}
//3.通过entrySet 获取 k - v
System.out.println("=============通过entrySet实现===============");
Set set1 = hashMap.entrySet();
for (Object o : set1) {
//向下转型 EntrySetz转换为EntryNode
Map.Entry next = (Map.Entry) o;
System.out.println("增强for方式:" + next.getKey() + " - " +next.getValue());
}
Iterator iterator2 = set1.iterator();
while(iterator2.hasNext()){
Object o = iterator2.next();
Map.Entry entry = (Map.Entry) o;
System.out.println("iterator方式:" + entry.getKey() + " - " + entry.getValue());
}
}
}
HashMap练习
import java.util.*;
public class MapExercise {
public static void main(String[] args) {
/*
使用HashMap添加三个员工对象,要求:
键:员工id
值:员工对象
并遍历显示工资 > 18000 的员工(遍历方式至少两种)
员工类:姓名、工资、员工id
*/
HashMap hashMap = new HashMap();
hashMap.put(001,new Persons(001, "Jack",20000));
hashMap.put(002,new Persons(002, "Tom",15000));
hashMap.put(003,new Persons(003, "CiCi",25000));
System.out.println("===========通过keySet获取所有键============");
//通过keySet获取所有键
Set set = hashMap.keySet();
for (Object o : set) {
//转换类型
Persons persons = (Persons) hashMap.get(o);
if (persons.getSalary()>18000){
System.out.println("增强for方法遍历:" + o + " - " + hashMap.get(o));
}
}
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
Object next = iterator.next();
Persons persons = (Persons) hashMap.get(next);
if (persons.getSalary()>18000){
System.out.println("Iterator方法遍历:" + persons);
}
}
System.out.println("===========通过entrySet方法获取 k-v============");
//通过entrySet方法获取 k-v
Set set1 = hashMap.entrySet();
for (Object o : set1) {
Map.Entry entry = (Map.Entry)o;
Persons persons = (Persons)entry.getValue();
if(persons.getSalary()>18000){
System.out.println("增强for方法遍历:" + persons);
}
}
Iterator iterator1 = set1.iterator();
while (iterator1.hasNext()) {
Map.Entry next = (Map.Entry) iterator1.next();
Persons persons = (Persons) next.getValue();
if(persons.getSalary()>18000){
System.out.println("Iterator方法遍历:" + persons);
}
}
System.out.println("===========通过Values方法获取 value值============");
Collection values = hashMap.values();
for (Object o : values) {
Persons persons = (Persons)o;
if(persons.getSalary()>18000){
System.out.println("增强for方法遍历:" + persons);
}
}
Iterator iterator2 = values.iterator();
while (iterator2.hasNext()) {
Object next = iterator2.next();
Persons persons = (Persons)next;
if(persons.getSalary()>18000){
System.out.println("Iterator方法遍历:" + persons);
}
}
}
}
class Persons{
private int id;
private String name;
private double salary;
public Persons(int id, String name, double salary) {
this.id = id;
this.name = name;
this.salary = salary;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Persons{" +
"id=" + id +
", name='" + name + '\'' +
", salary=" + salary +
'}';
}
}
/*
心得:
1.六种遍历方式,做题逻辑是先得出value,如果得不出就用Map.Entry进行转换,在得到value,用entry变量保存
2.然后将entry变量转换为Persons,用persons变量保存,就可以用persons变量取Person类得值进行比较了
运行结果:
===========通过keySet获取所有键============
增强for方法遍历:1 - Persons{id=1, name='Jack', salary=20000.0}
增强for方法遍历:3 - Persons{id=3, name='CiCi', salary=25000.0}
Iterator方法遍历:Persons{id=1, name='Jack', salary=20000.0}
Iterator方法遍历:Persons{id=3, name='CiCi', salary=25000.0}
===========通过entrySet方法获取 k-v============
增强for方法遍历:Persons{id=1, name='Jack', salary=20000.0}
增强for方法遍历:Persons{id=3, name='CiCi', salary=25000.0}
Iterator方法遍历:Persons{id=1, name='Jack', salary=20000.0}
Iterator方法遍历:Persons{id=3, name='CiCi', salary=25000.0}
===========通过Values方法获取 value值============
增强for方法遍历:Persons{id=1, name='Jack', salary=20000.0}
增强for方法遍历:Persons{id=3, name='CiCi', salary=25000.0}
Iterator方法遍历:Persons{id=1, name='Jack', salary=20000.0}
Iterator方法遍历:Persons{id=3, name='CiCi', salary=25000.0}
*/
HashMap小结
-
Map接口得常用实现类:HashMap、Hashtable、Properties
-
HashMap是Map接口使用频率最高得实现类你
-
HashMap是以 key - val 的方式来存储数据(HashMap$Node类型)
-
Key不能重复,但值可以重复,允许使用null键和null值
-
如果添加相同的key,则会覆盖原来的key - val,等同于修改,(key 不会替换,val会替换)
-
与HashSet一样,不保证映射的顺序,因为底层是以hash表的方式来存储的。(JDK 8 的 HashMap 底层:数组+ 链表 + 红黑树)
-
HashMap没有实现同步,因此是线程不安全的