Map与Collection并列存在。用于保存具有映射关系的数据:Key-Value。
观察底层的HashSet的方法,可以清晰的看见Set和Map是有关系的。Map是一个接口!
1、 HashMap(Map的主要实现类)
HashMap:Key使用Set来存放的,所以不能重复;Value是用Collections来存放,所以可以重复。
(1) 添加、删除操作
向HashMap中添加进去的时候,会调用key所在类的equals() 方法,判断两个key是否相同,相同的话只能添加后添加的那个元素。
① Object put(Object key,Object value);
② Object remove(Object key);
③ void putAll(Map t);
④ Void clear();
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
public class TestHashMap {
public static void main(String[] args) {
test1();
}
public static void test1 (){
Map map = new HashMap();
map.put("AA", 213);
map.put("BB", 456);
/*
我的IntelliJ明显报错了,此时仍然运行,得到的size仍是4
查看编译结果会发现明显保留的是下面的789
*/
map.put("BB", 789);
map.put(123, "CiCi");
map.put(null, null);
map.remove(null);
map.put(new People("AA",28),87);
map.clear();
System.out.println(map.size());
System.out.println(map);
Object value = map.get(123);
System.out.println("通过key获取指定的value值:" + value);
}
}
class People{
private String name;
private int age;
public People(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "People{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
People people = (People) o;
return age == people.age &&
Objects.equals(name, people.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
(2) 元视图操作
下面有三个对Map进行遍历的方法:
① Set keySet();
② Collection values();
③ Set entrySet();
请看下方实例,简单粗暴:
import java.util.*;
public class TestHashMap {
public static void main(String[] args) {
test2();
}
public static void test2(){
// 遍历map
Map map = new HashMap();
map.put("AA", 213);
map.put("BB", 456);
map.put("DD", 456);
map.put("CC", 456);
// 1. 通过keySet获取key集
Set set = map.keySet();
System.out.print("keySet遍历获取key:" );
for (Object obj : set){
System.out.print(obj+" ");
}
// 2. 通过 Collection 获取Value集
Collection values = map.values();
Iterator iterator = values.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
// 3. 普通同时获取key-value的方法
Set set1 = map.keySet();
for (Object obj:set){
System.out.println(obj +"=>"+map.get(obj));
}
// 4. 通过entrySet获取key-value
Set set2 = map.entrySet();
for (Object obj : set2){
Map.Entry entry = (Map.Entry)obj;
System.out.println(entry.getKey() + "=>" + entry.getValue());
}
}
}
2、 LinkedHashMap
使用链表维护添加进Map中的顺序。故遍历Map时,是按添加的顺序遍历的。
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
public class TestLinkedHashMap {
public static void main(String[] args) {
test1();
}
public static void test1(){
Map map = new LinkedHashMap();
map.put("AA", 213);
map.put("BB", 456);
map.put("DD", 789);
map.put("CC", 456);
Set set = map.entrySet();
for (Object o : set ){
Map.Entry entry = (Map.Entry) o;
System.out.println(entry.getKey() + "=>" + entry.getValue() );
}
}
}
3、 TreeMap
按照添加进Map中的元素的key的指定属性进行排序。要求:key必须是同一个类的对象(这句话可以这么理解,如果猫是一个类,狗是一个类,猫的某一属性和狗的不一样,那么这两个是不能进行排序的)!
import java.util.*;
public class TestTreeMap {
public static void main(String[] args) {
// test1();
test2();
}
/**
* 按照age自然排序
*/
public static void test1(){
Map map = new TreeMap();
map.put(new Person("AA",21),0);
map.put(new Person("CC",23),1);
map.put(new Person("BB",22),2);
map.put(new Person("JJ",25),2);
map.put(new Person("DD",22),2);
Set s = map.entrySet();
for (Object o : s){
Map.Entry e = (Map.Entry) o;
System.out.println(e);
}
}
/**
* ?????????????????
* 按照 age 定制排序
*/
public static void test2(){
Comparator com = new Comparator() {
@Override
public int compare(Object o1, Object o2) {
if (o1 instanceof Person && o2 instanceof Person){
Person p1 = (Person) o1;
Person p2 = (Person) o2;
int i = p1.getAge() - p2.getAge();
if (i == 0){
return p1.getName().compareTo(p2.getName());
}else{
return i;
}
}
return 0;
}
};
TreeMap map = new TreeMap(com);
map.put(new Person("AA",26),0);
map.put(new Person("CC",24),1);
map.put(new Person("BB",23),2);
map.put(new Person("JJ",23),3);
map.put(new Person("DD",21),4);
Set set = map.keySet();
for (Object o : set){
System.out.println(o +" =>" + map.get(o));
}
}
}
class Person implements Comparable{
private String name;
private Integer age;
@Override
public int compareTo(Object o) {
if (o instanceof Person){
Person p = (Person) o;
int i = this.getAge() - p.getAge();
if (i == 0){
return this.getName().compareTo(p.getName());
}else{
return i;
}
}
return 0;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return Objects.equals(name, person.name) &&
Objects.equals(age, person.age);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
4、 HashTable
(1)Hashtable是个古老的 Map 实现类,线程安全。
(2)与HashMap不同,Hashtable 不允许使用 null 作为 key 和 value
(3)与HashMap一样,Hashtable 也不能保证其中 Key-Value 对的顺序
(4)Hashtable判断两个key相等、两个value相等的标准,与hashMap一致。
下面的实例请注意,在项目的根目录新建一个并写入如下的文件及内容:
import javax.annotation.processing.FilerException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
/**
* Hashtable 中的properties 常用来处理属性文件
* 键和值都是String类型的
*/
public class TestHashTable {
public static void main(String[] args) {
try {
test();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void test() throws FilerException , IOException {
Properties pros = new Properties();
// 先载入文件流
pros.load(new FileInputStream(new File("jdbc.properties") ));
// 再获取文件中的值
String username = pros.getProperty("username");
System.out.println(username);
String pwd = pros.getProperty("password");
System.out.println(pwd);
}
}