黑马程序员——JAVA基础之set集合
------- android培训、java培训、期待与您交流!
----------
元素是无序(存入和取出的顺序不一定一致),元素不可以重复。
HashSet:底层数据结构是哈希表。是线程不安全的,不同步。存取速度快。
TreeSet: 线程不安全,可以对Set集合中的元素进行排序。
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集合的更多相关文章
- 黑马程序员——JAVA基础之Map集合
------- android培训.java培训.期待与您交流! ---------- Map集合: 该集合存储键值对.一对一对往里存.而且要保证键的唯一性. 和Set很像,其实Set底层就是使用了M ...
- 黑马程序员——JAVA基础之List集合
------- android培训.java培训.期待与您交流! ---------- Collection : |--List:元素是有序的,元素可以重复.因为该集合体系有索引. | ...
- 黑马程序员——JAVA基础之简述集合collection
------- android培训.java培训.期待与您交流! ---------- 集合: 为什么出现集合类? • 面向对象语言对事物的体现都是以对象的形式,所以为了方便对多个对象的操作,就对对 ...
- 黑马程序员——JAVA基础之Vector集合
------- android培训.java培训.期待与您交流! ---------- 基本已被淘汰 枚举就是Vector特有的取出方式.枚举和迭代器很像.其实枚举和迭代是一样的. 因为枚举的名称 ...
- 黑马程序员----java基础笔记中(毕向东)
<p>------<a href="http://www.itheima.com" target="blank">Java培训.Andr ...
- 黑马程序员Java基础班+就业班课程笔记全发布(持续更新)
正在黑马学习,整理了一些课程知识点和比较重要的内容分享给大家,也是给自己拓宽一些视野,仅供大家交流学习,大家有什么更好的内容可以发给我 ,现有黑马教程2000G QQ 1481135711 这是我总 ...
- 黑马程序员----java基础笔记上(毕向东)
------Java培训.Android培训.iOS培训..Net培训.期待与您交流! ------- 笔记一共记录了毕向东的java基础的25天课程,分上.中.下 本片为上篇,涵盖前10天课程 1. ...
- 黑马程序员——JAVA基础之泛型和通配符
------- android培训.java培训.期待与您交流! ---------- 泛型: JDK1.5版本以后出现新特性.用于解决安全问题,是一个类型安全机制. 泛型好处: ...
- 黑马程序员——JAVA基础之简述面向对象,类,变量,匿名对象
------- android培训.java培训.期待与您交流! ---------- 面向对象: 面向对象是相对面向过程而言 面向对象和面向过程都是一种思想 面向过程 强调的是功能行为 面向对象 将 ...
随机推荐
- windows操作系统日常使用
快捷键使用: 看实例,学经验,我看行. 1.人体学输入设备被禁用怎么办(鼠标被禁用.键盘被禁用) 有一天脑子抽风,把鼠标给禁用了.以前不常用快捷键,这下必须学学怎么使用快捷键了,现在记下来,防止以后脑 ...
- Linux下GCC的使用
1简介 GCC 的意思也只是 GNU C Compiler 而已.经过了这么多年的发展,GCC 已经不仅仅能支持 C 语言:它现在还支持 Ada 语言.C++ 语言.Java 语言.Objective ...
- poj1458
//Accepted 4112 KB 16 ms //最长公共子串 #include <cstdio> #include <cstring> #include <iost ...
- Android 对 properties文件的读写操作
-. 放在res中的properties文件的读取,例如对放在assets目录中的setting.properties的读取:PS:之所以这里只是有读取操作,而没有写的操作,是因为我发现不能对res下 ...
- cometd的服务器配置
CometDServlet必须在web.xml中进行配置,如下: <servlet> <servlet-name>cometd</servlet-name& ...
- Get start with Android development
Firstly we should install the right version of JDK and JRE, there are two version of ADK for differe ...
- js 微信分享
一. //js接口 var shareme; var urls = window.location.href; if(isWeiXin()){ var weifileref=document.cr ...
- 64位Ubuntu下的Eclipse、ADT终于可以生成R.java了,虚机也可以正常建立
64位Ubuntu12下的Eclipse总也不能自动生成R.java,导致无法正常编译程序,建虚拟器的时候总是提示少文件....三天下班机器没关(无奈公司网络不给力)来安装lib包. 各种加载,总结一 ...
- 文件IO和标准IO
2015.2.26 星期四,阴天 今天的内容主要是文件IO man 手册的分册: man -f open 查看那些分册中有openman 1 -- 普通的命令程序man 2 -- 系统调用man 3 ...
- JQuery源码解析(九)
jQuery回调对象 jQuery.Callbacks一般开发者接触的很少,虽然jQuery向开发者提供了外部接口调用,但是$.Callbacks()模块的开发目的是为了给内部$.ajax() 和 $ ...