---------------------- ASP.Net+Unity开发.Net培训、期待与您交流! ----------------------

1.Set

Set是Collection接口的子接口,此集合中不能存放重复元素,对于比较方式是根据他们的底层结构决定的,常见的子类有:HashSet和TreeSet

2.HashSet

1. 概述

底层是哈希表:哈希值是Object类中哈希方法返回的值,读取此集合中的元素是必须使用迭代器,比较的时候,先是比较的哈希值,如果哈希值相同那么就比较equals方法

2. 无序性

3.不可重复性

145322775.jpg

4.比较的原理

他们首先比较的哈希值,如果哈希值相同,那么他们就开始比较他们的equals方法,同时也可以自定义比较规则,就是重写HashCode()和equals()方法。

重写的时候一定要和Object类中定义的一样,equals的参数是Object类型,public boolean equals(Object ob){},public int hashCode();

在这里也说一下ArrayList和LinkedList集合,他们的比较方式,他们就是基于equals方法

如果我们hashCode()方法重写的恰到好处的话,那么equals方法减少了比较次数。

如下:

package cn.itcast.CollectionTest;


import java.util.HashSet;

import java.util.Iterator;


class person2{

private String name;

private int age;

public person2(String name,int age){

super();

this.name=name;

this.age=age;

}

public String getName() {

return name;

}

public int getAge() {

return age;

}

/*

*这个重写hashCode()方法,在这里返回的哈希值是一样的,为了测试是不是调用的此方法,若哈希值相同,调用的是否是equals()方法

* */

public int hashCode(){

/*System.out.println(this.getName()+"------hashCode----");

return 20;*/

int code= this.name.hashCode()+this.age*56;

System.out.println(this.name+":"+this.age+code);

return code;

}

/*

* equals()方法的重写

* */

public boolean equals(Object ob){

if((ob instanceof person)){

return false;

}

person2 p=(person2)ob;

System.out.println(this.getName()+"----equals---"+p.getName());

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

}

}


public class HashSetDome2 {


public static void main(String[] args) {

HashSet set=new HashSet();

set.add(new person2("zhangsan",21));

set.add(new person2("lsii",22));

set.add(new person2("zhangsan",21));

System.out.println("读取下集合中的元素:");

Iterator it=set.iterator();

while (it.hasNext()){

person2 ob=(person2)it.next();

System.out.println(ob.getName()+":"+ob.getAge());

}

System.out.println("是否存在此对象:"+set.contains(new person2("zhangsan",21)));

System.out.println("是否存在此对象:"+set.contains(new person2("wangwu",21)));

System.out.println("移除对象:"+set.remove(new person2("lisi",22)));


}


}

运行结果:

zhangsan:21-1432603380

lsii:223332535

zhangsan:21-1432603380

zhangsan----equals---zhangsan

读取下集合中的元素:

lsii:22

zhangsan:21

zhangsan:21-1432603380

zhangsan----equals---zhangsan

是否存在此对象:true

wangwu:21-795135815

是否存在此对象:false

lisi:223323235

移除对象:false

1.TreeSet

1.概述


TreeSet本身对元素记性排序,要是自定的类,那么要是此类对象存数TreeSet中,那么就必须是S自定义的类本身具备比较性,那么据必须实现接口Comparable,并重写方法compareTo()方法,底层数据结构是二叉树

compareTo()返回值是整数,如果小于0,那么此对象就小于比较的对象,等于0,那么此对象就等于比较的对象,如果大于0,那么此对象就大于比较的对象。

2.自然排序规则(Comparable)

有的对象在存储到TreeSet本身就具备可比性,例如:String类,是要找ASCII大小比较的,那么我们就以自定义的类来存储TreeSet中,使其具备可比性。

如下:

package cn.itcast.CollectionTest;

import java.util.Iterator;

import java.util.TreeSet;

//自定义异常

class RuntimeNoStudentExcpetion extends RuntimeException{

public RuntimeNoStudentExcpetion(String message){

super(message);

}

}

class person3 implements Comparable{

private String name;

private int age;

public person3(String name,int age){

super();

this.name=name;

this.age=age;

}

public String getName(){

return name;

}

public int getAge(){

return age;

}

public int compareTo(Object o){

if(!(o instanceof person3)){

throw new RuntimeNoStudentExcpetion("不是person3的对象");

}

person3 p=(person3)o;

if(this.name.equals(p.name))

return this.age-p.age;

return this.name.compareTo(p.name);

}

}

public class TreeSetDome {


public static void main(String[] args) {

TreeSet set=new TreeSet();

set.add(new person3("zhangsan",21));

set.add(new person3("lisi",25));

set.add(new person3("wangwu",36));

set.add(new person3("lisi",25));

Iterator it=set.iterator();

while (it.hasNext()){

person3 p=(person3)it.next();

System.out.println(p.getName()+":"+p.getAge());

}

}


}

运行结果:

lisi:25

wangwu:36

zhangsan:21

3.模拟二叉树:

package cn.itcast.CollectionTest;

class ErChaShu{

class Node{

private Comparable data;

private Node lift;

private Node right;

public Node (Comparable data){//构造方法初始化数据

this.data=data;

}

public void addNode(Node newNode){

if(newNode.data.compareTo(this.data)<0){

if(this.lift==null){

this.lift=newNode;

}else{

this.lift.addNode(newNode);

}

}

if(newNode.data.compareTo(this.data)>=0){

if(this.right==null){

this.right=newNode;

}else{

this.right.addNode(newNode);

}

}

}

public void PrintNode(){

if(this.lift != null){

this.lift.PrintNode();

}

System.out.println(this.data+"\t");

if(this.right !=null){

this.right.PrintNode();

}

}

}

private Node root=null;

public void add(Comparable com){

Node newNode=new Node(com);

if(this.root==null){

this.root=newNode;

}else{

this.root.addNode(newNode);

}

}

public void print(){

this.root.PrintNode();

}

}


public class TreeSetDome2 {


public static void main(String[] args) {

ErChaShu t=new ErChaShu();

t.add(2);

t.add(8);

t.add(400);

t.add(9);

t.add(5);

t.print();


}


}

运行结果:

2

5

8

9

400

练习:

自定义比较器,存储字符串,字符串按照长度大小排序

151107367.jpg

运行结果:

d

ab

ets

abcdg




-------------------- ASP.Net+Unity开发.Net培训、期待与您交流! ----------------------