您的位置:首页 > 其它

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

2014-08-11 00:22 603 查看
<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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐