一、請簡述Map 的特點(diǎn)约啊。
- Map每個(gè)元素由鍵與值兩部分組成
- Map鍵不能重復(fù),每個(gè)鍵對應(yīng)一個(gè)值
- 鍵和值可以為null
說出Entry鍵值對對象遍歷Map集合的原理。
Map中存放的是兩種對象挺据,一種稱為key(鍵)吉嫩,一種稱為value(值),它們在在Map中是一一對應(yīng)關(guān)系,這一對對象又稱做Map 中的一個(gè)Entry(項(xiàng))顾患。Entry將鍵值對的對應(yīng)關(guān)系封裝成了對象番捂。即鍵值對對象,這樣我們在遍歷Map集合時(shí)江解,就可以從每一個(gè)鍵值對(Entry)對象中獲取對應(yīng)的鍵與對應(yīng)的值设预。
三、請使用Map集合的方法完成添加元素犁河,根據(jù)鍵刪除鳖枕,以及根據(jù)鍵獲取值操作。
public class MapTest01{
public static void main(String[] args) {
// 1.創(chuàng)建HashMap
HashMap<String, String> hm = new HashMap<String, String>();
// 2.使用put添加元素
hm.put("黃曉明", "Baby");
hm.put("鄧超", "孫儷");
hm.put("李晨", "范冰冰");
hm.put("大黑牛", "范冰冰");
// 3.使用put修改元素
String v1 = hm.put("李晨", "白百合");
// 4.使用get獲取元素
String string = hm.get("大黑牛");
// 5.使用remove刪除元素
String v2 = hm.remove("大黑牛");
System.out.println(v2);
// 6.打印集合中的元素
System.out.println(hm);
}
}
四桨螺、往一個(gè)Map集合中添加若干元素宾符。獲取Map中的所有value,并使用增強(qiáng)for和迭代器遍歷輸出每個(gè)value灭翔。
public class MapTest02 {
public static void main(String[] args) {
// 1.創(chuàng)建HashMap
HashMap<String, String> hm = new HashMap<String, String>();
// 2.使用put添加元素
hm.put("黃曉明", "Baby");
hm.put("鄧超", "孫儷");
hm.put("李晨", "范冰冰");
hm.put("大黑牛", "范冰冰");
// 3.使用Map的values方法獲取到所有的value
Collection<String> values = hm.values();
// 4.使用增強(qiáng)for獲取每個(gè)value
for (String value : values) {
System.out.println(value);
}
System.out.println("----------------");
// 5.使用迭代器獲取每個(gè)value
Iterator<String> itr = values.iterator();
while (itr.hasNext()) {
System.out.println(itr.next());
}
}
}
五魏烫、請使用Map集合存儲(chǔ)自定義數(shù)據(jù)類型Car做鍵,對應(yīng)的價(jià)格做值肝箱。并使用keySet和entrySet兩種方式遍歷Map集合哄褒。
汽車類:
// 1.定義汽車類.包含名稱和價(jià)格屬性,重寫hashCode和equals方法
public class Car {
private String name;
private String color;
public Car() {
}
public Car(String name, String color) {
this.name = name;
this.color = color;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Car)) return false;
Car car = (Car) o;
if (name != null ? !name.equals(car.name) : car.name != null) return false;
return color != null ? color.equals(car.color) : car.color == null;
}
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + (color != null ? color.hashCode() : 0);
return result;
}
}
測試類:
public class MapTest03 {
public static void main(String[] args) {
// 2.創(chuàng)建HashMapkey保存汽車對象,value是汽車價(jià)格
HashMap<Car, Integer> hm = new HashMap<>();
// 3.添加汽車到HashMap中
Car c1 = new Car("長安奔奔", "黃色");
Car c3 = new Car("奇瑞QQ", "黑色");
Car c2 = new Car("鈴木奧拓", "白色");
hm.put(c1, 10000);
hm.put(c2, 20000);
hm.put(c3, 30000);
// 4.使用keySet方式遍歷Map
Set<Car> keySet = hm.keySet();
for (Car c : keySet) {
// 根據(jù)key獲取value
Integer value = hm.get(c);
System.out.println(c.getName() + ","+ c.getPrice() + " - "+ value);
}
System.out.println("-------------");
// 5.使用entrySet方式遍歷Map
Set<Map.Entry<Car, Integer>> entrySet = hm.entrySet();
for (Map.Entry<Car, Integer> entry : entrySet) {
Car key = entry.getKey();
Integer value = entry.getValue();
System.out.println(key.getName() + ","+ key.getPrice() + " - "+ value);
}
}
}
六、現(xiàn)在有一個(gè)map集合如下:
Map<Integer,String> map = new HashMap<Integer, String>();
map.put(1, "張三豐");
map.put(2, "周芷若");
map.put(3, "汪峰");
map.put(4, "滅絕師太");
要求:
1.遍歷集合煌张,并將序號(hào)與對應(yīng)人名打印呐赡。
2.向該map集合中插入一個(gè)編碼為5姓名為李曉紅的信息
3.移除該map中的編號(hào)為1的信息
4.將map集合中編號(hào)為2的姓名信息修改為"周林"
public class MapTest04 {
public static void main(String[] args) {
// 1.定義HashMap,編號(hào)作為key,姓名作為value
Map<Integer,String> map = new HashMap<Integer, String>();
// 2.使用put方法添加元素
map.put(1, "張三豐");
map.put(2, "周芷若");
map.put(3, "汪峰");
map.put(4, "滅絕師太");
// 3.使用keySet+增強(qiáng)for迭代map中的元素,并打印
Set<Integer> keySet = map.keySet();
for (Integer key : keySet) {
String value = map.get(key);
System.out.println(key + " -- "+ value);
}
// 4.使用put向該map集合中插入一個(gè)編碼為5姓名為李曉紅的信息
map.put(5, "李曉紅");
// 5.使用remove移除該map中的編號(hào)為1的信息
map.remove(1);
// 6.使用put將map集合中編號(hào)為2的姓名信息修改為"周林"
map.put(2, "周林");
System.out.println(map);
}
}
七、有2個(gè)數(shù)組唱矛,第一個(gè)數(shù)組內(nèi)容為:[黑龍江省,浙江省,江西省,廣東省,福建省]罚舱,第二個(gè)數(shù)組為:[哈爾濱,杭州,南昌,廣州,福州],將第一個(gè)數(shù)組元素作為key绎谦,第二個(gè)數(shù)組元素作為value存儲(chǔ)到Map集合中管闷。如{黑龍江省=哈爾濱, 浙江省=杭州, …}。
public class MapTest05 {
public static void main(String[] args) {
// 1.定義第一個(gè)數(shù)組arr1
String[] arr1 = {"黑龍江省", "浙江省", "江西省", "廣東省", "福建省"};
// 2.定義第二個(gè)數(shù)組arr2
String[] arr2 = {"哈爾濱", "杭州", "南昌", "廣州", "福州"};
// 3.創(chuàng)建HashMap,key存放省,value存放市
HashMap<String, String> hm = new HashMap<>();
// 4.使用普通for循環(huán)遍歷arr1
for (int i = 0; i < arr1.length; i++) {
// 5.根據(jù)索引到arr1中獲取到省
String key = arr1[i];
// 6.根據(jù)索引到arr2中獲取到省會(huì)城市
String value = arr2[i];
// 7.將省和省會(huì)城市添加到HashMap中
hm.put(key, value);
}
// 8.輸出HashMap中的內(nèi)容
System.out.println(hm);
}
}