java treemap类型,JAVA学习.JAVA集合类型Map.HashMap&TreeMap

以下是一张比较简单的介绍图,在具体代码分析之前先大概的了解以下要介绍的内容。

0818b9ca8b590ca3270a3433284dd417.png

作为key值保存的类对象:

/**

*

*/

package MapDemo;

/**

* @author fshxxxyydys

*

*/

public class Dog implements Comparable{

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 dogMap = new TreeMap();

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 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 dogMap = new HashMap();

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.

======================================================================

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值