run_wind

导航

黑马程序员——JAVA基础之set集合

------- android培训java培训、期待与您交流! ----------

 

Set:
      元素是无序(存入和取出的顺序不一定一致),元素不可以重复。 
 
Set接口中常用的类:
HashSet:底层数据结构是哈希表。是线程不安全的,不同步。存取速度快。
TreeSet: 线程不安全,可以对Set集合中的元素进行排序。
 
Set集合元素唯一性原因:
HashSet:通过equals方法和hashCode方法来保证元素的唯一性。
TreeSet:通过compareTo或者compare方法中的来保证元素的唯一性。元素是以二叉树的形式存放的。

 


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

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

如果元素的hashCode值相同,才会判断equals是否为true。 

如果元素的hashcode值不同,不会调用equals。 
 
注意,对于判断元素是否存在,以及删除等操作,依赖的方法是元素的hashcode和equals方法。

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

 

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

/**
 * 
 * HashSet简单演示:
 *
 */
public class HashSetDemo 
{
	public static void main(String[] args)
	{
		HashSet hs = new HashSet();
		
		hs.add("01");
		hs.add("02");
		hs.add("03");
		hs.add("04");
		hs.add("05");
		
		for (Iterator it = hs.iterator();it.hasNext(); )
		{
			System.out.println(it.next());
		}
		
	}
}


 

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

/**
 * 将自定义元素存到ArrayList结构中去,并去掉重复元素
 * 	比如:存人对象。同姓名同年龄,视为同一个人。为重复元素。
 * 
 * 思路: 
 *		对人描述,将数据封装进人对象。 
 *		定义容器,将人存入。 
 *		取出。 
 *   List集合判断元素是否相同,依据是元素的equals方法。 	
 */
public class HashSetDemo
{
	public static void main(String[] args)
	{
		HashSet hs = new HashSet();
		
		hs.add(new Person("zhangsan",12));
		hs.add(new Person("zhangsan",22));
		hs.add(new Person("lisi",12));
		hs.add(new Person("wangwu",12));
		hs.add(new Person("wangwu",12));
		
		for (Iterator it = hs.iterator();it.hasNext(); )
		{
			Person p = (Person) it.next();
			System.out.println(p.getName()+"------"+p.getAge());
		}
	}
}

//声明一个Person对象,具有年龄和名字的属性
class Person
{
	private String name;
	private int age;
	
	Person(String name,int age)
	{
		this.name = name;
		this.age = age;
	}

	 public boolean equals(Object obj)  
	 {  
	     if(!(obj instanceof Person))  
	         return false;  
	     Person p = (Person)obj;  
	     return this.name.equals(p.name) && this.age == p.age;  
	 }  
	 
	 public int hashCode()
	 {
		 return name.hashCode()+age*37;
	 }
	
	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:

    可以对Set集合中的元素进行排序。 
    底层数据结构是二叉树。 


               

TreeSet保证元素唯一性的依据:

    compareTo方法return 0. 


 
TreeSet排序的第一种方式:

让元素自身具备比较性。
元素需要实现Comparable接口,覆盖compareTo方法。 这种方式也称为元素的自然顺序,或者叫做默认顺序。 

 

 

import java.util.Iterator;
import java.util.TreeSet;

/**
 * TreeSet演示:
 * 需求:用TreeSet方法储存自定义学生对象
 * 		相同名字年龄视为同一学生
 * 		按照年龄来排序
 * 
 * 当主要条件相同时,记得判断次要条件
 */
public class TreeSetDemo 
{
	public static void main(String[] args)
	{
		TreeSet ts = new TreeSet();
		
		ts.add(new Student("zhangsan",19));
		ts.add(new Student("lisi",19));
		ts.add(new Student("wangwu",14));
		ts.add(new Student("lisi",19));
		ts.add(new Student("lisi",15));
		
		for (Iterator it = ts.iterator();it.hasNext(); )
		{
			Student s = (Student) it.next();
			System.out.println(s.getName()+"-----"+s.getAge());
		}
	}
}

//声明一个学生类实现comparable接口,属性年龄,名字
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)
	{
		if (!(obj instanceof Student))
			throw new RuntimeException("isn't student!");
		Student s = (Student) obj;
		
		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 void setName(String name) 
	{
		this.name = name;
	}

	public int getAge() 
	{
		return age;
	}

	public void setAge(int age) 
	{
		this.age = age;
	}	
}


 

 

TreeSet的第二种排序方式: 

当元素自身不具备比较性时,或者具备的比较性不是所需要的。

这时就需要让集合自身具备比较性。 在集合初始化时,就有了比较方式。

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

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

 

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

 

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

public class TreeSetDemo 
{
	public static void main(String[] args)
	{
		TreeSet ts = new TreeSet(new MyCompare());
		
		ts.add(new Student("zhangsan",19));
		ts.add(new Student("lisi",19));
		ts.add(new Student("wangwu",14));
		ts.add(new Student("lisi",19));
		ts.add(new Student("lisi",15));
		
		for (Iterator it = ts.iterator();it.hasNext(); )
		{
			Student s = (Student) it.next();
			System.out.println(s.getName()+"-----"+s.getAge());
		}
	}
}

//声明一个学生类实现comparator接口,属性年龄,名字
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)
	{
		if (!(obj instanceof Student))
			throw new RuntimeException("isn't student!");
		Student s = (Student) obj;
		
		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 void setName(String name) 
	{
		this.name = name;
	}

	public int getAge() 
	{
		return age;
	}

	public void setAge(int age) 
	{
		this.age = age;
	}	
}

//定义一个比较器实现comparable接口,按名字排序
class MyCompare implements Comparator
{
	public int compare(Object o1,Object o2)
	{
		Student s1 = (Student) o1;
		Student s2 = (Student) o2;
		
		int num = s1.getName().compareTo(s2.getName());
		if (num == 0)
		{
			return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
		}
		return num;
	}
}


 

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

/**
 * 练习:按照字符串长度排序。 
 * 		字符串本身具备比较性。但是它的比较方式不是所需要的。 
 *		 这时就只能使用比较器。 
 */
public class TreeSetTest 
{
	public static void main(String[] args)
	{
		TreeSet ts = new TreeSet(new Mycompare());
		
		ts.add("asdasd");
		ts.add("rgeri");
		ts.add("ididi");
		ts.add("if");
		
		for (Iterator it = ts.iterator();it.hasNext(); )
		{
			System.out.println(it.next());		
		}
	}
}

//定义一个比较器,按长度排序
class Mycompare implements Comparator
{
	public int compare(Object o1,Object o2)
	{
		String s1 = (String) o1;
		String s2 = (String) o2;
		
		int num = new Integer(s1.length()).compareTo(new Integer(s2.length()));
		if (num == 0)
		{
			return s1.compareTo(s2);
		}
		return num;
	}
}


 

------- android培训java培训、期待与您交流! ----------

 

 

 

 

posted on 2014-11-29 23:23  run_wind  阅读(168)  评论(0编辑  收藏  举报