Map是一个key:value键值对结构(key不能重复,value可以重复,通过key可以获得value),本节学习Map接口及其两个实现类HashMap和TreeMap。
HashMap的key们相当于哈希表HashSet,TreeMap的key们相当于树TreeSet。value则是挂载在key上的映射值。
*测试HashMap
package day34;
import java.util.HashMap;
import java.util.Set;
public class Test {
public static void main(String[] args) {
HashMap<String,String> hm=new HashMap<String,String>();
//1.put
hm.put("1", "one");
hm.put("2", "two");
hm.put("3", "three");
hm.put("1", "four");//验证key不能重复
hm.put("5", "three");//验证value可以重复
//2.遍历
Set<String> keys=hm.keySet();//获得key集合
for(String k:keys)//再对key集合遍历
{
String value=hm.get(k);
System.out.println(k+":"+value);
}
//3.remove
hm.remove(2);
//4.contains
boolean b=hm.containsKey(2);
System.out.println(b);
boolean b2=hm.containsValue("three");
System.out.println(b2);
}
}
运行结果:
1:four
2:two
3:three
5:three
false
true
上面的例子中用的key是String,它本身已经实现了hashCode()和equals(),对于一般的类型做key时:
package day34;
import java.util.HashMap;
import java.util.Set;
class MyInt{
int i;
public MyInt(int i) {
this.i = i;
}
@Override
public String toString() {
return i+"";
}
}
public class Test {
public static void main(String[] args) {
HashMap<MyInt,String> hm=new HashMap<MyInt,String>();
MyInt i1=new MyInt(1);
MyInt i2=new MyInt(2);
MyInt i3=new MyInt(3);
MyInt i4=new MyInt(1);
MyInt i5=new MyInt(5);
//1.put
hm.put(i1, "one");
hm.put(i2, "two");
hm.put(i3, "three");
hm.put(i4, "four");//验证key不能重复
hm.put(i5, "three");//验证value可以重复
//2.遍历
Set<MyInt> keys=hm.keySet();//获得key集合
for(MyInt k:keys)//再对key集合遍历
{
String value=hm.get(k);
System.out.println(k+":"+value);
}
//3.remove
hm.remove(i2);
//4.contains
boolean b=hm.containsKey(i2);
System.out.println(b);
boolean b2=hm.containsValue("three");
System.out.println(b2);
}
}
运行结果:
3:three
5:three
1:one
2:two
1:four
false
true
可以看到两个1是不同对象,作key时就没有被区分开(表面上是不同的对象而内在的属性都相同),这就需要我们自己覆盖hashCode()方法和equals()方法:
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + i;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MyInt other = (MyInt) obj;
if (i != other.i)
return false;
return true;
}
运行结果:
1:four
2:two
3:three
5:three
false
true
*测试TreeMap
就像TreeSet可以排序一样,TreeMap的key是可以排序的。
package day34;
import java.util.TreeMap;
import java.util.Set;
public class Test {
public static void main(String[] args) {
TreeMap<String,String> tm=new TreeMap<String,String>();
//1.put
tm.put("2", "two");
tm.put("1", "one");
tm.put("3", "three");
tm.put("1", "four");//验证key不能重复
tm.put("5", "three");//验证value可以重复
//2.遍历
Set<String> keys=tm.keySet();//获得key集合
for(String k:keys)//再对key集合遍历
{
String value=tm.get(k);
System.out.println(k+":"+value);
}
//3.remove
tm.remove("2");
//4.contains
boolean b=tm.containsKey("2");
System.out.println(b);
boolean b2=tm.containsValue("three");
System.out.println(b2);
}
}
运行结果:
1:four
2:two
3:three
5:three
false
true
使用一般类型作key来测试:
package day34;
import java.util.TreeMap;
import java.util.Set;
class MyInt{
int i;
public MyInt(int i) {
this.i = i;
}
@Override
public String toString() {
return i+"";
}
}
public class Test {
public static void main(String[] args) {
TreeMap<MyInt,String> tm=new TreeMap<MyInt,String>();
MyInt i2=new MyInt(2);
MyInt i1=new MyInt(1);
MyInt i3=new MyInt(3);
MyInt i4=new MyInt(1);
MyInt i5=new MyInt(5);
//1.put
tm.put(i2, "two");
tm.put(i1, "one");
tm.put(i3, "three");
tm.put(i4, "four");//验证key不能重复
tm.put(i5, "three");//验证value可以重复
//2.遍历
Set<MyInt> keys=tm.keySet();//获得key集合
for(MyInt k:keys)//再对key集合遍历
{
String value=tm.get(k);
System.out.println(k+":"+value);
}
//3.remove
tm.remove(i2);
//4.contains
boolean b=tm.containsKey(i2);
System.out.println(b);
boolean b2=tm.containsValue("three");
System.out.println(b2);
}
}
运行时会报错:
因为需要实现Comparable接口才能作排序再输出:
package day34;
import java.util.TreeMap;
import java.util.Set;
class MyInt implements Comparable<MyInt>{
int i;
public MyInt(int i) {
this.i = i;
}
@Override
public String toString() {
return i+"";
}
@Override
public int compareTo(MyInt o) {
if(this.i>o.i)
return 1;
else if(this.i<o.i)
return -1;
return 0;
}
}
public class Test {
public static void main(String[] args) {
TreeMap<MyInt,String> tm=new TreeMap<MyInt,String>();
MyInt i2=new MyInt(2);
MyInt i1=new MyInt(1);
MyInt i3=new MyInt(3);
MyInt i4=new MyInt(1);
MyInt i5=new MyInt(5);
//1.put
tm.put(i2, "two");
tm.put(i1, "one");
tm.put(i3, "three");
tm.put(i4, "four");//验证key不能重复
tm.put(i5, "three");//验证value可以重复
//2.遍历
Set<MyInt> keys=tm.keySet();//获得key集合
for(MyInt k:keys)//再对key集合遍历
{
String value=tm.get(k);
System.out.println(k+":"+value);
}
//3.remove
tm.remove(i2);
//4.contains
boolean b=tm.containsKey(i2);
System.out.println(b);
boolean b2=tm.containsValue("three");
System.out.println(b2);
}
}
运行结果:
1:four
2:two
3:three
5:three
false
true
若要key降序排列:
@Override
public int compareTo(MyInt o) {
if(this.i>o.i)
return -1;
else if(this.i<o.i)
return 1;
return 0;
}
运行结果:
5:three
3:three
2:two
1:four
false
true