JAVA学习.JAVA集合类型Map.HashMap&TreeMap,
分享于 点击 45269 次 点评:189
JAVA学习.JAVA集合类型Map.HashMap&TreeMap,
以下是一张比较简单的介绍图,在具体代码分析之前先大概的了解以下要介绍的内容。
作为key值保存的类对象:
/**
*
*/
package MapDemo;
/**
* @author fshxxxyydys
*
*/
public class Dog implements Comparable<Dog>{
private String color;
private Integer age;
public Dog() {
super();
}
public Dog(String color, Integer age) {
super();
// 子类构造方法调用父类构造方法只能放在第一行
this.color = color;
this.age = age;
}
//如果有需要可以根据需要填写不同参数的构造函数
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public boolean equals(Object obj) {
if(obj == null) return false;
if(obj == this) return true;
if(!(obj instanceof Dog)) return false;
Dog d =(Dog)obj;
if(this.age.equals(d.age)&&this.color.equals(d.color))
return true;
return false;
}
@Override
public int hashCode() {
return this.color.hashCode() + this.age.hashCode();
}
@Override
public int compareTo(Dog o) {
//传进来的对象本身就是指定对象类型无需进行判断
if(this.age.compareTo(o.age) != 0){
return this.age.compareTo(o.age);
}
//先按照年龄来排序
if(this.color.compareTo(o.color) != 0){
return this.color.compareTo(o.color);
}
//再按照颜色来排序
return 0;
//如果颜色和年龄都相同返回0的就按照相同处理
}
@Override
public String toString() {
return "Color:" + this.color + " Age:" + this.age + ".";
}
}
TreeMap:
/**
*
*/
package MapDemo;
import java.util.Set;
import java.util.TreeMap;
/**
* @author fshxxxyydys
*
*/
public class TreeMapDemo {
/**
* @param args
*/
public static void main(String[] args) {
//TreeMap是以TreeSet来进行key管理的Map接口的实现
//与TreeSet类似,如要使得TreeMap对其key值进行无
//重复的筛选以及自然顺序排序,则被当做key值得对象
//必须实现Comparable接口并实现CompareTo方法.
TreeMap<Dog,String> dogMap = new TreeMap<Dog,String>();
dogMap.put(new Dog("bule",11),"A");
dogMap.put(new Dog("bule",12),"B");
dogMap.put(new Dog("bule",10),"C");
dogMap.put(new Dog("red",1),"D");
dogMap.put(new Dog("red",21),"E");
dogMap.put(new Dog("red",4),"F");
dogMap.put(new Dog("green",8),"G");
dogMap.put(new Dog("green",12),"H");
dogMap.put(new Dog("green",4),"I");
Set<Dog> dogKey = dogMap.keySet();
//获取dogMap中key的Set值。
for(Dog d:dogKey)
System.out.println(d);
}
}
======================================================================
Result:
Color:red Age:1.
Color:green Age:4.
Color:red Age:4.
Color:green Age:8.
Color:bule Age:10.
Color:bule Age:11.
Color:bule Age:12.
Color:green Age:12.
Color:red Age:21.
======================================================================
HashMap:/**
*
*/
package MapDemo;
import java.util.HashMap;
import java.util.Set;
/**
* @author fshxxxyydys
*
*/
public class HashMapDemo {
/**
* @param args
*/
public static void main(String[] args) {
HashMap<Dog,String> dogMap = new HashMap<Dog,String>();
dogMap.put(new Dog("bule",11),"A");
dogMap.put(new Dog("bule",12),"B");
dogMap.put(new Dog("bule",10),"C");
dogMap.put(new Dog("red",1),"D");
dogMap.put(new Dog("red",21),"E");
dogMap.put(new Dog("red",4),"F");
dogMap.put(new Dog("green",8),"G");
dogMap.put(new Dog("green",12),"H");
dogMap.put(new Dog("green",4),"I");
for(Dog d:dogMap.keySet())
System.out.println(d);
}
}
======================================================================
Result:
Color:red Age:1.
Color:green Age:4.
Color:red Age:4.
Color:red Age:21.
Color:bule Age:12.
Color:bule Age:11.
Color:bule Age:10.
Color:green Age:12.
Color:green Age:8.
======================================================================
原创出处:http://blog.csdn.net/u012830807
相关文章
- 暂无相关文章
用户点评