------- 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培训、期待与您交流!
----------

黑马程序员——JAVA基础之set集合的更多相关文章

  1. 黑马程序员——JAVA基础之Map集合

    ------- android培训.java培训.期待与您交流! ---------- Map集合: 该集合存储键值对.一对一对往里存.而且要保证键的唯一性. 和Set很像,其实Set底层就是使用了M ...

  2. 黑马程序员——JAVA基础之List集合

    ------- android培训.java培训.期待与您交流! ---------- Collection : |--List:元素是有序的,元素可以重复.因为该集合体系有索引.         | ...

  3. 黑马程序员——JAVA基础之简述集合collection

    ------- android培训.java培训.期待与您交流! ---------- 集合: 为什么出现集合类? •  面向对象语言对事物的体现都是以对象的形式,所以为了方便对多个对象的操作,就对对 ...

  4. 黑马程序员——JAVA基础之Vector集合

    ------- android培训.java培训.期待与您交流! ---------- 基本已被淘汰 枚举就是Vector特有的取出方式.枚举和迭代器很像.其实枚举和迭代是一样的.   因为枚举的名称 ...

  5. 黑马程序员----java基础笔记中(毕向东)

    <p>------<a href="http://www.itheima.com" target="blank">Java培训.Andr ...

  6. 黑马程序员Java基础班+就业班课程笔记全发布(持续更新)

    正在黑马学习,整理了一些课程知识点和比较重要的内容分享给大家,也是给自己拓宽一些视野,仅供大家交流学习,大家有什么更好的内容可以发给我 ,现有黑马教程2000G  QQ 1481135711 这是我总 ...

  7. 黑马程序员----java基础笔记上(毕向东)

    ------Java培训.Android培训.iOS培训..Net培训.期待与您交流! ------- 笔记一共记录了毕向东的java基础的25天课程,分上.中.下 本片为上篇,涵盖前10天课程 1. ...

  8. 黑马程序员——JAVA基础之泛型和通配符

    ------- android培训.java培训.期待与您交流! ---------- 泛型:            JDK1.5版本以后出现新特性.用于解决安全问题,是一个类型安全机制. 泛型好处: ...

  9. 黑马程序员——JAVA基础之简述面向对象,类,变量,匿名对象

    ------- android培训.java培训.期待与您交流! ---------- 面向对象: 面向对象是相对面向过程而言 面向对象和面向过程都是一种思想 面向过程 强调的是功能行为 面向对象 将 ...

随机推荐

  1. JAVA 打印九九乘法表

    /** *  * @author liangxiaoyu * @version 1.0 *2015-09-18 */public class JJ { public static void main( ...

  2. 详解模块定义(.def)文件

    一个完整的Windows应用程序(C++程序)通常由五种类型的文件组成:源程序文件,头文件,资源描述文件,项目文件,模块定义文件.本文主要讲解模块定义文件. 模块定义 (.def)文件为链接器提供有关 ...

  3. Java 嵌套作用域

    在C/C++中,当一个块处于另一个块作用域内的时候,内层定义的变量会把外层的变量隐藏, 遵循所谓的就近原则. 在Java中,在内层定义与外层同名的变量是禁止的! 如下: int i = 0; for( ...

  4. Java Proxy

    Client---->Interface A --        -- 代理类     Class AImpl 代理类是动态生成的,借助Proxy类和InvocationHandler接口进行实 ...

  5. 【LeetCode OJ】Word Break II

    Problem link: http://oj.leetcode.com/problems/word-break-ii/ This problem is some extension of the w ...

  6. Object-C 基础笔记3---属性

    一,区别属性和实例变量 实例变量就是声明在接口大括号里面的变量.@public类型的实例变量直接使用->访问, property 属性是一组设置器和访问器,属性是方法不是变量. 与类相似,属性需 ...

  7. 7、SQL基础整理(子查询)

    子查询 (用来进行两个或以上表之间的查询) 1.首先新建一个bumen表和一个haha表,填充数据 2.利用两表进行子查询: --部门人数大于5的部门中最大年龄的人的信息--- select MAX( ...

  8. [转Go-简洁的并发 ]

    http://www.yankay.com/go-clear-concurreny/ Posted on 2012-11-28by yankay 多核处理器越来越普及.有没有一种简单的办法,能够让我们 ...

  9. 第一个Sprint冲刺第十天

    讨论成员:邵家文.李新.朱浩龙.陈俊金 工作:第一个计时功能完成,还有一些复杂的公式已完成.          关于github其实我们团队,还没有搞清楚github的真正用途,我们尚要花时间去学习如 ...

  10. NLP(Natural Language Processing)

    https://github.com/kjw0612/awesome-rnn#natural-language-processing 通常有: (1)Object Recognition (2)Vis ...