测试HashMap:
package zz.itheima.map;
import java.util.Collection;
import java.util.HashMap;
import java.util.Set;
public class TestHashMap {
public static void main(String[] args) {
HashMap<String, String> hm = new HashMap<>();
hm.put("CN", "chinese");
hm.put("JP", "japan");
hm.put("US", "usa");
System.out.println(hm);
hm.remove("JP");
System.out.println(hm);
System.out.println(hm.containsKey("JP"));
System.out.println(hm.containsValue("usa"));
Set hs = hm.entrySet();
System.out.println(hs);
System.out.println(hm.get("CN"));
Set keys = hm.keySet();
System.out.println(keys);
System.out.println(hm.size());
Collection valuses = hm.values();
System.out.println(valuses);
}
}
运行结果:
{JP=japan, US=usa, CN=chinese}
{US=usa, CN=chinese}
false
true
[US=usa, CN=chinese]
chinese
[US, CN]
2
[usa, chinese]
测试LinkedHashMap:
package zz.itheima.map;
import java.util.LinkedHashMap;
public class TestLinkedHashMap {
public static void main(String[] args) {
LinkedHashMap<String, Integer> lhm = new LinkedHashMap<>();
lhm.put("a", 1);
lhm.put("b", 2);
lhm.put("c", 3);
lhm.put("d", 4);
lhm.put("e", 5);
System.out.println(lhm);
}
}
运行结果:
{a=1, b=2, c=3, d=4, e=5}
测试TreeMap:
package zz.itheima.map;
import java.util.TreeMap;
public class TestTreeMap {
public static void main(String[] args) {
TreeMap<String, Integer> tm = new TreeMap<>();
tm.put("a", 1);
tm.put("b", 1);
tm.put("d", 1);
tm.put("c", 1);
tm.put("e", 1);
System.out.println(tm);
System.out.println(tm.ceilingEntry("a"));
System.out.println(tm.ceilingKey("a"));
System.out.println(tm.firstEntry());
System.out.println(tm.firstKey());
System.out.println(tm.higherEntry("a"));
System.out.println(tm.lowerEntry("b"));
}
}
运行结果:
{a=1, b=1, c=1, d=1, e=1}
a=1
a
a=1
a
b=1
a=1
测试containsKey:
package zz.itheima.map;
import java.util.HashMap;
public class TestContainsKey {
public static void main(String[] args) {
HashMap<Student, Integer> hm=new HashMap<>();
hm.put(new Student("aaa", 20), 1);
hm.put(new Student("bbb", 21), 1);
hm.put(new Student("aaa", 20), 1);
System.out.println(hm);
}
}
运行结果:
{Student [name=aaa, age=20]=1, Student [name=bbb, age=21]=1}
测试Map集合的迭代器:
package zz.itheima.map;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
public class TestIteratorMap {
public static void main(String[] args) {
HashMap<String, String> hm = new HashMap<>();
hm.put("CN", "China");
hm.put("JP", "Japan");
hm.put("RU", "Russia");
hm.put("USA", "American");
System.out.println(hm);
Set keys = hm.keySet();
Iterator it = keys.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
Collection values = hm.values();
it = values.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
keys = hm.keySet();
Iterator<String> itt = keys.iterator();
while (itt.hasNext()) {
String key = itt.next();
String val = hm.get(key);
System.out.println(key+"="+val);
}
System.out.println("方式二:Map.Entry接口,用于获取键值对的集合");
for (Entry<String, String> o : hm.entrySet()) {
System.out.println(o);
System.out.println(o.getKey());
System.out.println(o.getValue());
}
System.out.println("************************");
Iterator<Entry<String, String>> iit = hm.entrySet().iterator();
while (iit.hasNext()) {
Entry<String , String> o = iit.next();
System.out.println(o);
System.out.println(o.getKey());
System.out.println(o.getValue());
}
}
}
运行结果:
{JP=Japan, USA=American, RU=Russia, CN=China}
JP
USA
RU
CN
Japan
American
Russia
China
JP=Japan
USA=American
RU=Russia
CN=China
方式二:Map.Entry接口,用于获取键值对的集合
JP=Japan
JP
Japan
USA=American
USA
American
RU=Russia
RU
Russia
CN=China
CN
China
************************
JP=Japan
JP
Japan
USA=American
USA
American
RU=Russia
RU
Russia
CN=China
CN
China
Map里面存List省市级联:
package zz.itheima.map;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;
public class ShengShiJiLian {
public static void main(String[] args) {
List<String> hnCity=new ArrayList<String>();
hnCity.add("郑州市");
hnCity.add("洛阳市");
List<String> sdCity=new ArrayList<String>();
sdCity.add("济南市");
sdCity.add("青岛市");
HashMap<String,List<String>> prov=new HashMap<String, List<String>>();
prov.put("hn", hnCity);
prov.put("sd", sdCity);
Scanner sc=new Scanner(System.in);
System.out.println("请选择省份:1.河南 2.山东");
int sel=sc.nextInt();
if(sel==1)
{
System.out.println(prov.get("hn"));
}
if(sel==2)
{
System.out.println(prov.get("sd"));
}
}
}
运行结果:
请选择省份:1.河南 2.山东
1
[郑州市, 洛阳市]
例子1:
package zz.itheima.map;
import java.util.HashMap;
import java.util.Set;
public class Demo1 {
public static void main(String[] args) {
HashMap<String , Book> hm = new HashMap<>();
hm.put("1234", new Book("aaa", 10));
hm.put("5678", new Book("bbb", 10));
hm.put("67889", new Book("ccc", 10));
System.out.println(hm);
if (hm.containsKey("1234")) {
System.out.println("存在");
}else {
System.out.println("不存在");
}
String keyName = "1234";
hm.remove(keyName);
System.out.println(hm);
Set<String > keys = hm.keySet();
for (String temp : keys) {
System.out.println(temp +"="+hm.get(temp));
}
}
}
运行结果
{5678=Book [name=bbb, price=10], 1234=Book [name=aaa, price=10], 67889=Book [name=ccc, price=10]}
存在
{5678=Book [name=bbb, price=10], 67889=Book [name=ccc, price=10]}
5678=Book [name=bbb, price=10]
67889=Book [name=ccc, price=10]
例子2:
package zz.itheima.map;
import java.util.HashMap;
import java.util.Set;
public class Demo2 {
public static void main(String[] args) {
HashMap<Integer, Student> hm=new HashMap<Integer, Student>();
hm.put(1234, new Student("张三", 10));
hm.put(5678, new Student("李四", 10));
hm.put(6789, new Student("王五", 10));
System.out.println(hm);
int key=1234567;
if(hm.containsKey(key))
System.out.println(hm.get(key));
else {
System.out.println("不存在");
}
System.out.println("学生总数:"+hm.size());
Set<Integer> keys=hm.keySet();
for (Integer temp : keys) {
System.out.println(temp);
}
}
}
运行结果:
{5678=Student [name=李四, age=10], 1234=Student [name=张三, age=10], 6789=Student [name=王五, age=10]}
不存在
学生总数:3
5678
1234
6789
例子3:
package zz.itheima.map;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class Demo3 {
public static void main(String[] args) {
List<Student> classOne=new ArrayList<Student>();
classOne.add(new Student("aaa", 20));
classOne.add(new Student("bbb", 20));
classOne.add(new Student("ccc", 20));
List<Student> classTwo=new ArrayList<Student>();
classTwo.add(new Student("rty", 20));
classTwo.add(new Student("fgh", 20));
classTwo.add(new Student("asd", 20));
HashMap<String, List<Student>> hm=new HashMap<String, List<Student>>();
hm.put("一班", classOne);
hm.put("二班", classTwo);
System.out.println(hm.get("二班"));
}
}
运行结果:
[Student [name=rty, age=20], Student [name=fgh, age=20], Student [name=asd, age=20]]
bean类:
package zz.itheima.map;
public class Book {
private String name;
private int price;
public Book(String name, int price) {
super();
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + price;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Book other = (Book) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (price != other.price)
return false;
return true;
}
@Override
public String toString() {
return "Book [name=" + name + ", price=" + price + "]";
}
}
package zz.itheima.map;
public class Student {
private String name;
private int age;
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}