Map接口
Map与Collection 是并列在,用保存具有映射关系的数据:key-Value
Map中的key和value都可以是任何类型的数据
Map中的key用Set来存放,不允许重复,既同一个Map对象所对应的类。须重写hashCode()和equals()方法
常用String类作为Map的“键”。
key和value之间存在单向的一对一关系。既通过指定的key总能找到唯一的确定的value
HashMap
1.key是用Set来存放的,不可重复,value是用Collection来存放的。可重复,一个key-value对是一个Entry所有的Entry是用Set存放的也是不可重复的。
2.向HashMap中添加元素时。会调用key所在类的equals()方法,判断俩个key是否相同,若相同则只能添加进后加的那个元素
Map接口
|…HashMap(主要实现类)
|…LinkedHashMap;
|…TreeMap
|…Hashtable
|….Properties;
Map常用的方法
添加,删除操作
Object put(Object key,Object value)向Map中添加一个元素
Object remove(Object key)按照指定的key删除此key-value
void putAll(Map t)
void clear():清空
元素查询的操作
Object get(Object key)获取指定key的value值 若无此key则返回null
boolean containsKey(Object key)
boolean containsValue(Object value)
int size()返回集合的长度
boolean isEmpty()
boolean equlas(Object obj)
元视图操作的方法
Set keySet()
Collection values()
Set entrySet()
package Collection;
public class Person implements Comparable {
private String name;
private Integer id;
public Person() {
super();
}
public Person(String name, Integer id) {
super();
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
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;
Person other = (Person) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
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 "Person [name=" + name + ", id=" + id + "]";
}
@Override
public int compareTo(Object o) {
if(o instanceof Person){
Person p=(Person)o;
int i=this.id.compareTo(p.id);
if(i==0){
return this.name.compareTo(p.name);
}else{
return i;
}
}
return 0;
}
}
package Collection;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.junit.Test;
public class testHashMap {
@Test
public void testOne(){
Map map= new HashMap();
Map map1= new HashMap();
map1.put("AA", 123);
map1.put("BB", 235);
map1.put("SS", 1235);
map.put("AA", 123);
map.put("BB", 235);
map.put("SS", 1235);
map.put("VV", 224);
map.put("AA", 456);
map.put(123, new Person("AA",123));
map.put(new Person("BB",124), 124);
map.put(new Person("BB",124), 125);
System.out.println(map.size());
//map.remove("AA");
//System.out.println(map.size());
//map.clear();
//System.out.println(map.size());
System.out.println(map.get("AA"));
System.out.println(map.get(444));
System.out.println(map.get("AA").equals(map.get("BB")));//比较俩个是否相等
System.out.println(map.isEmpty());//判断是否为空
boolean b1=map.containsValue(224);//判断map中是否包含该values值
boolean b2=map.containsKey("AA");//判断改制中是否包含其key
System.out.println(b1);
System.out.println(b2);
//进行遍历
Set set=map.keySet();//遍历key
for(Object obj:set){
System.out.println(obj);
}
Collection com=map.values();
Iterator i=com.iterator();//遍历values
while(i.hasNext()){
System.out.println(i.next());
}
Set set1=map.keySet();
for(Object obj:set1){
System.out.println(obj+"----------------->"+map.get(obj));
}//entrySet方式一
Set ste2=map.entrySet();
for(Object obj:ste2){
Map.Entry entry=(Map.Entry)obj;
System.out.println(entry.getKey()+"--------->"+entry.getValue());
}//方式二
Set ste3=map.entrySet();
for(Object obj:ste3){
Map.Entry entry=(Map.Entry)obj;
System.out.println(entry);
}
}
}
运行结果
6
456
null
false
false
true
true
AA
BB
SS
VV
Person [name=BB, id=124]
123
456
235
1235
224
125
Person [name=AA, id=123]
AA----------------->456
BB----------------->235
SS----------------->1235
VV----------------->224
Person [name=BB, id=124]----------------->125
123----------------->Person [name=AA, id=123]
AA--------->456
BB--------->235
SS--------->1235
VV--------->224
Person [name=BB, id=124]--------->125
123--------->Person [name=AA, id=123]
AA=456
BB=235
SS=1235
VV=224
Person [name=BB, id=124]=125
123=Person [name=AA, id=123]
LinkedHashMap
使用链表维护添加进Map中的顺序,故遍历Map是按照添加的顺序遍历的
package Collection;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import org.junit.Test;
public class testHashMap {
@Test
public void tetsTwo(){
Map map= new LinkedHashMap();
map.put("BB", 235);
map.put("SS", 1235);
map.put("VV", 224);
map.put("AA", 456);
map.put(123, new Person("AA",123));
map.put(new Person("BB",124), 124);
map.put(new Person("BB",124), 125);
Set ste2=map.entrySet();
for(Object obj:ste2){
Map.Entry entry=(Map.Entry)obj;
System.out.println(entry.getKey()+"--------->"+entry.getValue());
}
}
}
运行结果
BB--------->235
SS--------->1235
VV--------->224
AA--------->456
123--------->Person [name=AA, id=123]
Person [name=BB, id=124]--------->125
TreeMap方法
//自排序
@Test//自排序
public void tetsOne(){
Map map=new TreeMap();
map.put(new Person("AA",124), 110);
map.put(new Person("CC",125), 132);
map.put(new Person("TT",126), 98);
map.put(new Person("VV",123), 214);
map.put(new Person("EE",152), 20);
map.put(new Person("AA",142), 24);
Set ste2=map.entrySet();
for(Object obj:ste2){
Map.Entry entry=(Map.Entry)obj;
System.out.println(entry.getKey()+"--------->"+entry.getValue());
}
}
运行结果
Person [name=VV, id=123]--------->214
Person [name=AA, id=124]--------->110
Person [name=CC, id=125]--------->132
Person [name=TT, id=126]--------->98
Person [name=AA, id=142]--------->24
Person [name=EE, id=152]--------->20
//定制排序
ackage Collection;
public class Customer {
private String name;
private Integer id;
public Customer() {
super();
}
public Customer(String name, Integer id) {
super();
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Override
public String toString() {
return "Customer [name=" + name + ", id=" + id + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
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;
Customer other = (Customer) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
package Collection;
import java.util.Comparator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.junit.Test;
public class testTreeMaop {
@Test
public void testTwo(){
Comparator com= new Comparator(){
@Override
public int compare(Object o1, Object o2) {
if(o1 instanceof Customer && o2 instanceof Customer){
Customer c1=(Customer)o1;
Customer c2=(Customer)o2;
int i=c1.getId().compareTo(c2.getId());
if(i==0){
return c1.getName().compareTo(c2.getName());
}else{
return i;
}
}
return 0;
}
};
Map map=new TreeMap(com);
map.put( new Customer("AA",124),124);
map.put( new Customer("SS",123),125);
map.put( new Customer("GG",126),245);
map.put( new Customer("mm",195),745);
map.put( new Customer("ss",45),245);
Set ste2=map.entrySet();
for(Object obj:ste2){
Map.Entry entry=(Map.Entry)obj;
System.out.println(entry.getKey()+"--------->"+entry.getValue());
}
}
//运行结果
Customer [name=ss, id=45]--------->245
Customer [name=SS, id=123]--------->125
Customer [name=AA, id=124]--------->124
Customer [name=GG, id=126]--------->245
Customer [name=mm, id=195]--------->745
Hashtable
:古老的实现类,线程安全,不建议使用
Properties:常用来处理属性文件:键和值都是String类型的。
使用Properties处理属性文件
package Collection;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Comparator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.TreeMap;
import org.junit.Test;
public class testTreeMaop {
@Test
public void Three() throws FileNotFoundException, IOException{
Properties pros=new Properties();
pros.load(new FileInputStream(new File("jdbc.properties")) );
String user=pros.getProperty("user");
String password=pros.getProperty("password");
System.out.println("uesr"+"="+user+"\n"+"password"+password);
}
文件内容
user=root
password=123abc
运行结果
uesr=root
password123abc