java基础-- 集合框架 之 Set集合

一、HashSet与TreeSet比较


Set:元素是无序(存入和取出的顺序不一定一致),元素不可以重复。

Set集合的功能和Collection是一致的。


1、hashSet与TreeSet比较

HashSet

|--HashSet: 底层数据结构是哈希表、是线程不安全的、不同步。

 

TreeSet

|--TreeSet:可以对Set集合中的元素进行排序,底层数据结构是二叉树(有一定的顺序)。

而且往里存入的对象必须具备比较性,进而进行排序,最后存入


 二、hashSet集合

1、HashSet是如何保证元素唯一性的呢?

   是通过元素的两个方法,hashCode和equals来完成。

   如果元素的HashCode值相同,才会判断equals是否为true,如果元素的hashcode值不同,不会调用equals。

   我们一般存储对象的时候,都要复写这两个方法。因为原有的hashCode和equals方法都是根据地址或根据地址算出来的值来进行比较,

 (hashCode内部调用的也是equals方法)没意义,所以复写hashCode和equals方法,按照元素自身条件特点来判断元素的唯一性。

 

注意: 对于判断元素是否存在,以及删除等操作,依赖的方法也是元素的hashcode 和equals方法。

 

使用HashSet集合存储自定义对象示例代码:

 

要求:如果姓名和年龄相同视为同一个人,重复元素。

说明:主要是hashCode与equals方法的使用


import java.util.HashSet;
import java.util.Iterator;

class HashSetTest {
	public static void sop(Object obj) {
		System.out.println(obj);
	}

	public static void main(String[] args) {
		HashSet hs = new HashSet();

		hs.add(new Person2("a1", 11));
		hs.add(new Person2("a2", 12));
		hs.add(new Person2("a3", 13));
		// hs.add(new Person2("a2",12));
		// hs.add(new Person2("a4",14));

		// sop("a1:"+hs.contains(new Person("a2",12)));

		// hs.remove(new Person("a4",13));

		Iterator it = hs.iterator();

		while (it.hasNext()) {
			Person2 p = (Person2) it.next();
			sop(p.getName() + "::" + p.getAge());
		}
	}
}

class Person2 {
	private String name;
	private int age;

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

	public String getName() {
		return name;
	}

	public int getAge() {
		return age;
	}

	public int hashCode() {
		
		System.out.println(this.name + "....hashCode");//判断何时调用hashCode方法
		return name.hashCode() + age * 37;
	}

	public boolean equals(Object obj) {

		if (!(obj instanceof Person))
			return false;

		Person2 p = (Person2) obj;
		
		System.out.println(this.name + "...equals.." + p.name);//判断何时调用equals方法

		return this.name.equals(p.name) && this.age == p.age;
	}

}

三、TreeSet集合

|--TreeSet:可以对Set集合中的元素进行排序,底层数据结构是二叉树(有一定的顺序)。

而且往里存入的对象必须具备比较性,进而进行排序,最后存入

 

1、保证元素唯一性的依据:compareTo方法和return  0;

 

2、TreeSet两种排序方式

    1> TreeSet排序的第一种方式:让元素自身具备比较性

      元素需要实现Comparable接口,覆盖compareTo方法。

      因为对象一存在就具备了比较性所以也称种方式为元素的自然顺序,或者叫做默认顺序。

注意:当主要条件相同时,判断次要条件(对于字符串可直使用接用compareTo())

 

让元素自身具备比较性的示例代码:

需求:

往TreeSet集合中存储自定义对象学生。

将学生对象进行排序:学生年龄为主要条件,姓名为次要条件

 

class Student implements Comparable {// 该接口强制让学生具备比较性。

	private String name;
	private int age;

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

	public int compareTo(Object obj) {
	
		//return 1;		实现按照存入顺序取出
		//return -1;		实现按照存入倒序取出

		if (!(obj instanceof Student))
			throw new RuntimeException("不是学生对象");
		Student s = (Student) obj;

		System.out.println(this.name + "....compareto....." + s.name);

		if (this.age > s.age)
			return 1;
		if (this.age == s.age) {	// 主要条件相同,比较次要条件
		
			return this.name.compareTo(s.name); // String已经实现了compareTo方法(按照字典方式排序)
		}
		return -1;
	}

	public String getName() {
		return name;

	}

	public int getAge() {
		return age;
	}
}

class TreeSetDemo {
	public static void main(String[] args) {
		TreeSet ts = new TreeSet();

		ts.add(new Student("lisi02", 22));
		ts.add(new Student("lisi007", 20));
		ts.add(new Student("lisi09", 19));
		ts.add(new Student("lisi08", 19));
		// ts.add(new Student("lisi007",20));
		// ts.add(new Student("lisi01",40));

		Iterator it = ts.iterator();
		while (it.hasNext()) {
			Student stu = (Student) it.next();
			System.out.println(stu.getName() + "..." + stu.getAge());
		}
	}
}

2>    TreeSet的第二种排序方式:让容器自身具备比较性

  当“元素”自身不具备比较性时,没有实现comparable,或者具备的比较性不是所需要的,这时就需要让“容器”自身具备比较性,

  定义比较器,将比较器对象作为参数传递给TreeSet集合的构造函数。

 

实现方式:定义一个类,实现Comparator接口,覆盖compare方法。

将比较器在集合初始化的时候传入

 

注意:当两种排序都存在时,以比较器为主。

 

让容器自身具备比较性的示例代码:

 

需求:往TreeSet集合中存储自定义对象学生。

将对象进行排序:学生姓名为主要条件,年龄为次要条件


//自定义比较器:姓名为主要条件,年龄为次要条件
class MyCompare implements Comparator {
	public int compare(Object o1, Object o2) {
		Student2 s1 = (Student2) o1;
		Student2 s2 = (Student2) o2;

		int num = s1.getName().compareTo(s2.getName());
		if (num == 0) {
			
			//使用基本类型的封装类创建对象:因为Integer已经实现了comparTo方法
			return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
			
			/*
			 if(s1.getAge()>s2.getAge()) return 1;
			 if(s1.getAge()==s2.getAge()) return 0; return -1;
			 */
		}
		return num;

	}
}

class Student2 implements Comparable// 该接口强制让学生具备比较性。
{
	private String name;
	private int age;

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

	public int compareTo(Object obj) {

		// return 0;

		if (!(obj instanceof Student2))
			throw new RuntimeException("不是学生对象");
		Student2 s = (Student2) obj;

		// System.out.println(this.name+"....compareto....."+s.name);
		if (this.age > s.age)
			return 1;
		if (this.age == s.age) {
			return this.name.compareTo(s.name);
		}
		return -1;

	}

	public String getName() {
		return name;

	}

	public int getAge() {
		return age;
	}
}



class TreeSetDemo2 {
	public static void main(String[] args) {
		TreeSet ts = new TreeSet(new MyCompare());

		ts.add(new Student2("lisi", 22));
		ts.add(new Student2("lisi", 21));
		ts.add(new Student2("lisi7", 20));
		ts.add(new Student2("lisi9", 19));
		ts.add(new Student2("lisi06", 18));
		ts.add(new Student2("lisi06", 18));
		ts.add(new Student2("lisi007", 29));
		// ts.add(new Student("lisi007",20));
		// ts.add(new Student("lisi01",40));

		Iterator it = ts.iterator();
		while (it.hasNext()) {
			Student2 stu = (Student2) it.next();
			System.out.println(stu.getName() + "..." + stu.getAge());
		}
	}
}

二、练习

TreeSet练习:按照字符串长度排序

 

说明:字符串本身具备比较性。但是它的比较方式不是所需要的。

这时就只能使用比较器。

 

注意问题:如果主要条件相同,进行次要条件比较,否则无法存入


import java.util.*;

class StrLenComparator implements Comparator{
	public int compare(Object o1,Object o2){
		
		String s1 = (String)o1;
		String s2 = (String)o2;

		/*
		if(s1.length()>s2.length())
			return 1;
		if(s1.length()==s2.length())
			return 0;
			*/

		int num = new Integer(s1.length()).compareTo(new Integer(s2.length()));
		if(num==0)
			return s1.compareTo(s2);

		return num;
	}
}

class  TreeSetTest{	
	public static void main(String[] args){
		
		TreeSet ts = new TreeSet(new StrLenComparator());

		ts.add("dfsd");
		ts.add("cc");
		ts.add("cba");
		ts.add("aaa");
		ts.add("gs");
		ts.add("fsfef");

		for(Iterator it = ts.iterator();it.hasNext()){
			System.out.println(it.next());
		}
	}
}

TreeSet练习二:将字符串中的数值进行排序,使用TreeSet完成。

 

思路

1,将字符串切割。

2,可以将这些对象存入TreeSet集合。因为TreeSet自身具备排序功能。


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

		ArrayList al = new ArrayList();

		String str = "90 -7 0 18 2 45 4";

		String[] arr = str.split(" ");

		TreeSet ts = new TreeSet();

		for(int x=0; x<arr.length; x++){
			//ts.add(new Integer(arr[x]));
			ts.add(Integer.parseInt(arr[x]));//
		}
		System.out.println(ts);
	}
}


posted @ 2012-10-07 21:43  积小流,成江海  阅读(197)  评论(0编辑  收藏  举报