Map集合练习,TreeMap保存自定义对象实现比较的Comparable和Comparator两种方式。。。

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/*
 * Map集合练习
 */
public class Demo03 {
	public static void main(String[] args) {
		// 创建map对象
		Map map = new HashMap();
		
		// 添加元素
		map.put(1, "我爱JAVA");
		map.put(2, "我爱北京");
		
		// 移除元素
//		map.remove(1);
		
		// 根据指定的key获取对应的value
//		Object o = map.get(2);
//		System.out.println(o);
		
		// 是否包含指定的key,包含返回true,不包含返回false。
//		boolean b = map.containsKey(1);
//		System.out.println(b);
		
//		//是否包含指定的value,包含返回true,不包含返回false。
//		boolean c = map.containsValue("我爱JAVA");
//		System.out.println(c);
		
		//map集合的keyset遍历,使用foreach。
//		Set set = map.keySet();
//		for(Object key:set) {
		    
//			Object value = map.get(key);
//			System.out.println(key+"..."+value);
//		}
		
//		//map集合的keyset遍历,使用迭代器
//		Set set = map.keySet();
//		 Iterator it = set.iterator();
//		 while(it.hasNext()) {
//			 Object key = it.next();
//			 Object value = map.get(key);
//			 System.out.println(key+"..."+value);
//		 }
		
//		//map集合的entrySet遍历,使用foreach
//		 Set set = map.entrySet();
//		 for(Object obj:set) {
//			 Entry en =(Entry) obj;
//			 Object key = en.getKey();
//			 Object value = en.getValue();
//			 System.out.println(key+"..."+value);
//		 }
		
		//map集合的values方法,获取到的map集合的value的值组成的collection集合
		Collection coll = map.values();
		for(Object obj:coll) {
			System.out.println(obj);
		}
		 
import java.util.Set;
import java.util.TreeMap;

/*
 *TreeMap使用练习 
 */
public class Demo04 {
	public static void main(String[] args) {
		TreeMap map = new TreeMap();

		map.put(new Person("lisi02", 18), "china");
		map.put(new Person("lisi01", 19), "china");
		map.put(new Person("lisi01", 10), "china");
		map.put(new Person("lisi22", 10), "china");
		map.put(new Person("lisi12", 17), "china");

		Set set = map.keySet();
		for (Object key : set) {
			Object value = map.get(key);
			System.out.println(key + "..." + value);
		}

	}
}

class Person implements Comparable {
	String name;
	int age;

	public Person(String name, int age) {
		this.name = name;
		this.age = age;
	}

	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		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 (age != other.age)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

	@Override
	public int compareTo(Object o) {
		if (o instanceof Person) {
			Person p = (Person) o;
			int num = name.compareTo(p.name);
			if (num == 0)
				return age - p.age;
			return num;
		}
		throw new ClassCastException("不是Person类型");
	}

}
import java.util.Comparator;
import java.util.Set;
import java.util.TreeMap;

/*
 *TreeMap使用练习 
 */
public class Demo05 {
	public static void main(String[] args) {
		TreeMap map = new TreeMap(new MyCompare());

		map.put(new Personal("lisi02", 18), "china");
		map.put(new Personal("lisi01", 19), "china");
		map.put(new Personal("lisi01", 10), "china");
		map.put(new Personal("lisi22", 10), "china");
		map.put(new Personal("lisi12", 17), "china");

		Set set = map.keySet();
		for (Object key : set) {
			Object value = map.get(key);
			System.out.println(key + "..." + value);
		}

	}
}

class Personal {
	String name;
	int age;

	public Personal(String name, int age) {
		this.name = name;
		this.age = age;
	}

	@Override
	public String toString() {
		return "Personal [name=" + name + ", age=" + age + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		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 (age != other.age)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

}

class MyCompare implements Comparator{

	@Override
	public int compare(Object o1, Object o2) {
		if(o1 instanceof Personal && o2 instanceof Personal){
			Personal p1 = (Personal)o1;
			Personal p2 = (Personal)o2;
			int num = p1.age - p2.age;
			if(num == 0)
				return p1.name.compareTo(p2.name);
			return num;
		}
		throw new ClassCastException("不是Personal类型");
	}
	
}

在这里插入图片描述
按年龄排序,并且不会出现重复的对象!!!厉害了我的JAVA !!!

猜你喜欢

转载自blog.csdn.net/weixin_43117449/article/details/82857838
今日推荐