Java集合06 13.Map介面02 13.2Map介面常用方法 put():添加 remove():根據鍵鍵刪除映射關係 get():根據鍵獲取值 size():獲取元素個數 isEnpty():判斷個數是否為0 clear():清除 containsKey():查找鍵是否存在 例子1:Map接 ...
Java集合06
13.Map介面02
13.2Map介面常用方法
- put():添加
- remove():根據鍵鍵刪除映射關係
- get():根據鍵獲取值
- size():獲取元素個數
- isEnpty():判斷個數是否為0
- clear():清除
- containsKey():查找鍵是否存在
例子1:Map介面常用方法
package li.map;
import java.util.HashMap;
import java.util.Map;
@SuppressWarnings("all")
public class MapMethod {
public static void main(String[] args) {
Map map = new HashMap();
// 1.put():添加
map.put("羅貫中",new Book("111",99));//ok
map.put("羅貫中","三國演義");//替換上一個value
map.put("施耐庵","666");//ok
map.put("克魯蘇","666");//ok
map.put(null,"空空如也");//ok
map.put("空空如也",null);//ok
System.out.println(map);
// 2.remove():根據鍵鍵刪除映射關係
map.remove(null);
System.out.println(map);//null對應的"空空如也"沒有了
// 3. get():根據鍵獲取值,返回一個Object類型
System.out.println(map.get("羅貫中"));//三國演義
// 4. size():獲取k-v對數
System.out.println(map.size());//4
// 5. isEnpty():判斷個數是否為0
System.out.println(map.isEmpty());//false
// 6. clear():將所有k-v清空
map.clear();
System.out.println(map);//{}
// 7. containsKey():查找鍵是否存在
map.put("我是123","我是123的value");
System.out.println(map.containsKey("我是123"));//true
}
}
class Book{
private String name;
private int price;
public Book(String name, int price) {
this.name = name;
this.price = price;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", price=" + price +
'}';
}
}
13.3Map介面六大遍歷方式
- containsKey:查找鍵是否存在
- keySet:獲取所有的鍵
- entrySet:獲取所有的關係k-v
- values:獲取所有的值
例子:
package li.map;
import java.util.*;
@SuppressWarnings("all")
public class MapFor {
public static void main(String[] args) {
Map map = new HashMap();
map.put("羅貫中", new Book("111", 99));
map.put("羅貫中", "三國演義");
map.put("施耐庵", "666");
map.put("克魯蘇", "666");
map.put(null, "空空如也");
map.put("空空如也", null);
//第一組:先取出所有的key,通過key取出對應的value
Set keySet = map.keySet();
System.out.println("----增強for----");
//增強for
for (Object key : keySet) {
System.out.println(key + "-" + map.get(key));//get():根據鍵獲取值
}
System.out.println("----迭代器----");
//迭代器
Iterator iterator = keySet.iterator();
while (iterator.hasNext()) {
Object key = iterator.next();
System.out.println(key + "-" + map.get(key));
}
//第二組:把所有的values值取出
Collection values = map.values();
//這裡可以使用所有collection使用的遍歷方法
//增強for:
System.out.println("---取出所有的value 增強for---");
for (Object value : values) {
System.out.println(value);
}
//迭代器:
System.out.println("---取出所有的value 迭代器:---");
Iterator iterator2 = values.iterator();
while (iterator2.hasNext()) {
Object value = iterator2.next();
System.out.println(value);
}
//第三組:通過EntrySet直接取出k-v對
Set entrySet = map.entrySet();//EntrySet<Map.Entry<K,V>>
//(1)增強for
System.out.println("---使用EntrySet的增強for---");
for (Object entry : entrySet) {
//將entry轉成 Map.Entry
Map.Entry m = (Map.Entry) entry;
System.out.println(m.getKey()+"-"+m.getValue());
}
//(2)迭代器:
System.out.println("---使用EntrySet的迭代器---");
Iterator iterator3 = entrySet.iterator();
while (iterator3.hasNext()) {
Object entry = iterator3.next();//這裡next取出來的類型本質上是Node,讓偶向上轉型為Object
//System.out.println(next.getClass());//class java.util.HashMap$Node
//向下轉型Object---> Map.Entry
Map.Entry m = (Map.Entry)entry;
System.out.println(m.getKey()+"-"+m.getValue());
}
}
}
13.4Map課堂練習
使用HashMap添加三個員工對象,要求:
鍵:員工id
值:員工對象
並遍歷顯示工資>18000的員工(遍歷方式最少兩種)
員工類:姓名、工資、員工id
練習:
package li.map;
import java.util.*;
@SuppressWarnings("all")
public class MapExercise {
public static void main(String[] args) {
Map map = new HashMap();
map.put(1, new Employee("smith", 8800, 1));
map.put(2, new Employee("John", 18900, 2));
map.put(3, new Employee("Jack", 8900, 3));
map.put(4, new Employee("Marry", 19900, 4));
map.put(5, new Employee("Jack", 3000, 5));
//keySet
Set keySet = map.keySet();
System.out.println("---keySet的增強for---");
for (Object key : keySet) {
Employee value = (Employee) map.get(key);//將獲得的value對象向下轉型為Employee類型
double salary = value.getSalary();//獲取工資
if (salary > 18000) {
System.out.println(map.get(key));
}//判斷輸出
}
System.out.println("---keySet的迭代器---");
Iterator iterator = keySet.iterator();
while (iterator.hasNext()) {
Object key = iterator.next();
Employee value = (Employee) map.get(key);//將獲得的value對象向下轉型為Employee類型
double salary = value.getSalary();//獲取工資
if (salary > 18000) {
System.out.println(map.get(key));
}//判斷輸出
}
//EntrySet
Set entrySet = map.entrySet();
System.out.println("---entrySet的增強for---");
for (Object entry : entrySet) {//entry代表一對k-v
//將entry向下轉型轉成 Map.Entry
Map.Entry m = (Map.Entry) entry;
Employee employee = (Employee) m.getValue();
double salary = employee.getSalary();
if (salary > 18000) {//判斷輸出
System.out.println(m.getValue());
}
}
System.out.println("---entrySet的迭代器---");
Iterator iterator2 = entrySet.iterator();
while (iterator2.hasNext()) {
Object entry = iterator2.next();
Map.Entry m = (Map.Entry) entry;//將Object強轉為Map.Entry類型
Employee employee = (Employee) m.getValue();
double salary = employee.getSalary();
if (salary > 18000) {//判斷輸出
System.out.println(m.getValue());
}
}
}
}
class Employee {
private String name;
private double salary;
private int id;
public Employee(String name, double salary, int id) {
this.name = name;
this.salary = salary;
this.id = id;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", salary=" + salary +
", 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;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
13.5HashMap小結
- Map介面的常用實現類:HashMap、Hashtable、Properties
- HashMap是Map介面使用頻率最高的實現類
- HashMap是以key-value對的方式來存儲數據(HashMap$Node類型)
- key不能重覆,value可以重覆。允許使用null鍵和null值
- 如果添加相同的key鍵,則會覆蓋原來的key-value,等同於修改(key不會替換,value會替換)
- 與HashSet一樣,不保證映射的順序,因為底層是以hash表的順序來存儲的。(JDK8的HashMap底層:數組+鏈表+紅黑樹)
- HashMap沒有實現同步,因此線程不安全,方法沒有做同步互斥的操作,沒有synchronized