TreeSet集合两个排序方法方法的分析&&Map集合获取的两中方式

<pre name="code" class="plain">
 

一、TreeSet集合1、 底层是一个二叉树,存储对象的时候,依据对象的自然顺序,自定义对象具备自然顺序,实现Comparable接口,重写compareTo方法。Person写的,姓名为主要排序条件,年龄次要的条件。按照人的年龄为主要的条件,排序。

import java.util.*;
public class TreeSetDemo1 {
	public static void main(String[] args) {


		TreeSet ts = new TreeSet();
		
		ts.add(new Person("lisi",20));
		ts.add(new Person("zhangsan",22));
		ts.add(new Person("lisa",30));
		ts.add(new Person("xiaoqiao",18));
		ts.add(new Person("daqiao",23));
		
		Iterator it = ts.iterator();
		
		while(it.hasNext()){
			
			Person p = (Person)it.next();
			System.out.println(p);
		}
	}
}


//实现Comparable接口,让Person具备自然顺序
//自然顺序,我们自己定义的,比较姓名,姓名相同,比较年龄
public class Person implements Comparable{
	private String name;
	private int age;
	
	public Person(String name,int age){
		this.age = age;
		this.name = name;
	}	
	public int compareTo(Object obj){
		//this 后进来的, obj先进来的对象
		Person p = (Person)obj;
		int num = this.name.compareTo(p.name);


		return num == 0 ? this.age - p.age : num;
	}
	public int hashCode(){
	//利用name变量 age变量
	 return	name.hashCode()+22*13;//乘以13是为了降低出现相同哈希值得概率
		
	}
	//同名的,和同年龄的对象,返回真
	public boolean equals(Object obj){


		if( this == obj)
			return true;
		if(obj == null)
			return false;
		if(obj instanceof Person){
			Person p = (Person)obj;
			//this对象中的姓名和年龄,p对象中的姓名和年龄
			//名字是String类型
			return this.name.equals(p.name) && this.age == p.age;
		}
		return false;
	}
	public String toString(){
		return "Person ..." + this.name +"..."+this.age;
	}
}


2、在JDK中,提供了另外的一种排序方式,比较器排序,比较器接口java..util.Comparator,TreeSet集合的构造方法中,传递一个比较器对象,按照比较器排序了。自定义比较器,写一个类,实现Comparator接口,重写compare方法。

代码实现如下:

/*
 * TreeSet比较器排序
 */
import java.util.*;
public class TreeSettDemo {
	public static void main(String[] args) {
		
		TreeSet ts = new TreeSet(new MyComparator());
		ts.add(new Person("lisi",20));
		ts.add(new Person("zisi",21));
		ts.add(new Person("wangwu",19));
		
		Iterator it = ts.iterator();//迭代器
		while(it.hasNext()){
			System.out.println(it.next());
		}
	}
}


public class Person {
	private String name;
	private int age;
	public Person(String name, int age) {
		this.name = name;
		this.age = age;
	}
	@Override
	//重写toString()方法
	public String toString() {
		return "Person [name=" + name + ", age=" + age + "]";
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}

/*
 * 自定义比较器:对于TreeSet集合中存储的Person对象进行排序
 */
public class MyComparator implements java.util.Comparator {
	/*
	 * 重写compare方法----比较Person对象的年龄和姓名
	 */
	public int compare(Object o1,Object o2){
		//o1和o2比较一下,年龄,主要条件,年龄不同,不需要比较姓名
		
		Person p1 = (Person)o1;
		Person p2 = (Person)o2;
		//Person的变量,只能用get方法获取
		int num = p1.getAge()-p2.getAge();
		return num == 0?p1.getName().compareTo(p2.getName()):num;
	}
}

二、Map集合的获取方式

1、利用Map中的一个方法keySet(),Map集合中的键,存储到Set集合迭代Set集合,获取出来的是所有的键通过键获取值,Map接口中的get方法。

代码实现如下:

private static void method(){
	Map<String,Integer> map = new HashMap<String, Integer>();
	map.put("a", 1);
	map.put("b", 2);
	map.put("c", 3);
	map.put("d", 4);
	//调用Map接口中的keySet方法, 将键存储到Set集合
	Set<String> set = map.keySet();
<span style="white-space:pre">	</span>//迭代Set集合
	Iterator<String> it = set.iterator();
	while(it.hasNext()){
		//it.next方法,返回的是String类,Map集合中的键
		String key = it.next();
		//通过Map集合的get()方法,传递键,获取值
		Integer value = map.get(key);
		System.out.println(key+"..."+value);
	}
}

2、 利用Map集合中的键值对映射关系获取 Map接口中有一个方法entrySet(),获取键值对的映射关系对象Entry,将这个对象Entry存储到了Set集合 迭代Set集合,获取出来的Set集合中存储的是映射关系对象Entry 通过关系对象的方法getKey  getValue

代码实现如下:

private static void method_1(){
	Map<String,Integer> map = new HashMap<String, Integer>();
	map.put("a", 1);
	map.put("b", 2);
	map.put("c", 3);
<span style="white-space:pre">	</span>map.put("d", 4);
<span style="white-space:pre">	</span>//使用Map接口方法entrySet,获取键值对映射关系对象Entry,对象存储到Set集合
	Set<Map.Entry<String, Integer>> set  = map.entrySet();
	//迭代器,迭代Set集合
	Iterator<Map.Entry<String, Integer>> it = set.iterator();
	while(it.hasNext()){
	<span style="white-space:pre">	</span>//it.next()获取出来的是什么,键值对映射关系对象Map.Entry
	<span style="white-space:pre">	</span>Map.Entry<String, Integer> me = it.next();
	<span style="white-space:pre">	</span>//利用键值对映射关系对象中的方法 getKey getValue
	<span style="white-space:pre">	</span>String key = me.getKey();
	<span style="white-space:pre">	</span>Integer value = me.getValue();
	<span style="white-space:pre">	</span>System.out.println(key+"..."+value);
		}
	}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值