package zz1Hash;
/*
* Set集合:Hashset
*
* 1、Hashset底层实际上是一个HashMap,HashMap底层采用了哈希表数据结构
*
* 2、哈希表又叫做散列表,哈希表底层是一个数组,这个数组中每一个元素
* 是一个单向链表。每个单向链表都有一个独一无二的hash值,代表数组的
* 下标。在某个单向链表中的每一个节点上的hash值是相等的。hash值实际
* 上是key调用hashCode方法,在通过"hash function"转换成的值
*
* 3、如何向哈希表中添加元素:
* 先调用被存储的key的hashCode方法,经过某个算法得出hash值,如果在
* 这个哈希表中不存在这个hash值,则直接加入元素。如果该hash值已经存在,
* 继续调用key之间的equals方法,如果equals方法返回false,则将该元
* 素添加。如果equals方法返回true,则放弃添加该元素
*
* 4、HashSet其实是HashMap中的key部分。
* HashSet有什么特点, HashMap中的key应该具有相同的特点
*
* 5、HashMap和HashSet初始化容量都是16,默认加载因子是0.75(容量到75%自动扩容)
*/
import java.util.*;
public class SetTest01 {
public static void main(String[] args) {
//创建Set集合
Set s = new HashSet();
//无序不可重复
s.add(1);
s.add(1);
s.add(100);
s.add(85);
s.add(88);
//遍历
Iterator it = s.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
package zz1Hash;
/*
* 关于往set集合中存储的元素,该元素的hashCode和equals方法
*
* HashMap中有一个put方法,put(key, value)key是无序不可重复的
*
* 结论:存储在HashSet集合或者HashMap集合key部分的元素,需要同时重写hashCode.equals
*/
import java.util.*;
public class SetTest02 {
public static void main(String[] args) {
//创建集合
Set es = new HashSet();
//e1 e2 e3都是key
//e1 e2的key都一样,但是他们的内存地址不一样,所以不会形成单链
Employee e1 = new Employee("1000", "JACK");
Employee e2 = new Employee("1000", "JACK");
Employee e3 = new Employee("2000", "JACK1");
System.out.println(e1.hashCode()); //1807500377 //1507423
System.out.println(e2.hashCode()); //355165777 //1507423
//添加元素
es.add(e1);
es.add(e2);
es.add(e3);
//查看集合元素个数
System.out.println(es.size()); //2
}
}
//根据现实的业务逻辑得知:该公司员工编号是:1000 - 9999
class Employee{
//编号
String no;
//姓名
String name;
//构造函数
Employee(String no, String name){
this.no = no;
this.name = name;
}
//重写equals方法
public boolean equals(Object o){
if(this == o){
return true;
}
if(o instanceof Employee){
Employee e = (Employee)o;
if(e.no.equals(this.no) && e.name.equals(this.name)){
return true;
}
}
return false;
}
//重写hashCode方法
public int hashCode(){
//以员工编号分组
//返回此字符串的哈希码
return no.hashCode();
}
}
package zz1Hash;
import java.util.*;
import java.text.*;
/*
* java.util.Set
* java.util.SortedSet; 无序不可重复,但是存进去的元素可以按照元素大小顺序自动排列
* java.util.TreeSet;
*/
public class SortedSetTest01 {
public static void main(String[] args) throws Exception {
//创建集合
SortedSet ss = new TreeSet();
//添加元素
ss.add(10); //自动装箱
ss.add(20);
ss.add(15);
ss.add(30);
ss.add(25);
//遍历
Iterator it = ss.iterator();
while(it.hasNext()){
Object element = it.next();
System.out.println(element);
}
/*
* 10
* 15
* 20
* 25
* 30
*/
//String
SortedSet strs = new TreeSet();
strs.add("JACK");
strs.add("SUN");
strs.add("KING");
//遍历
it = strs.iterator();
while(it.hasNext()){
Object element = it.next();
System.out.println(element);
}
/*
* JACK
* KING
* SUN
*/
//日期Date
String st1 = "2008-08-08";
String st2 = "2009-08-08";
String st3 = "2008-09-08";
String st4 = "2008-08-09";
String st5 = "2012-08-08";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date t1 = sdf.parse(st1);
Date t2 = sdf.parse(st2);
Date t3 = sdf.parse(st3);
Date t4 = sdf.parse(st4);
Date t5 = sdf.parse(st5);
//添加
SortedSet times = new TreeSet();
times.add(t1);
times.add(t4);
times.add(t3);
times.add(t2);
times.add(t5);
//遍历
it = times.iterator();
while(it.hasNext()){
Object element = it.next();
if(element instanceof Date){
Date d = (Date)element;
System.out.println(sdf.format(d));
}
}
/*
* 2008-08-08
* 2008-08-09
* 2008-09-08
* 2009-08-08
* 2012-08-08
*/
}
}
package zz1Hash;
import java.util.*;
/*
* SortedSet集合存储元素为什么可以自动排序
* 因为被存储的元素实现了Comparable接口,
* SUN编写TreeSet集合在添加元素的时候,
* 会调用compareTo方法完成比较
*/
public class SortedSetTest02 {
public static void main(String[] args) {
SortedSet users = new TreeSet();
User u1 = new User(15);
User u2 = new User(16);
User u3 = new User(25);
User u4 = new User(13);
User u5 = new User(11);
//添加元素
users.add(u1);
users.add(u2);
users.add(u3);
users.add(u4);
users.add(u5);
//遍历
Iterator it = users.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
/*
* User[age=11]
* User[age=13]
* User[age=15]
* User[age=16]
* User[age=25]
*/
}
}
class User implements Comparable{
int age;
User(int age){
this.age = age;
}
public String toString(){
return "User[age="+age+"]";
}
//实现java.lang.Comparable;接口中的compareTo方法
//该方法程序员负责实现,SUN提供的程序已经调用了该方法
//需求:按照User的age排序
public int compareTo(Object o){
//编写一个比较规则
int age1 = this.age;
int age2 = ((User)o).age;
return age1-age2;
}
}
package zz1Hash;
import java.util.*;
/*
* 让SortedSet集合做到排序还有另一种方式:java.util.Comparator;
*
* 推荐使用这种方法
*
* 单独编写一个比较器
*/
public class SortedSetTest03 {
public static void main(String[] args) {
//创建TreeSet集合的时候提供一个比较器
SortedSet products = new TreeSet(new ProductComparator());
//匿名内部类,不推荐使用,但是这样导致程序无法复用
/*
SortedSet products = new TreeSet(new Comparator(){
public int compare(Object o1, Object o2){
double price1 = ((Product)o1).price;
double price2 = ((Product)o2).price;
if(price1 == price2){
return 0;
}else if(price1 > price2){
return -1;
}else{
return 1;
}
}
});
*/
Product p1 = new Product(3.4);
Product p2 = new Product(4.0);
Product p3 = new Product(3.0);
Product p4 = new Product(5.0);
//添加元素
products.add(p1);
products.add(p2);
products.add(p3);
products.add(p4);
//遍历
Iterator it = products.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
class Product{
double price;
Product(double price){
this.price = price;
}
public String toString(){
return price + "";
}
}
//单独编写一个比较器
class ProductComparator implements Comparator{
//需求:按照商品排序
public int compare(Object o1, Object o2){
double price1 = ((Product)o1).price;
double price2 = ((Product)o2).price;
if(price1 == price2){
return 0;
}else if(price1 > price2){
return -1;
}else{
return 1;
}
}
}