package com.edu_01;
import java.util.HashSet;
import java.util.Set;
/**
* 2.HashSet(查询api看说明进行讲解)
2.1 元素顺序:元素唯一,但是无序(它不保证set 的迭代顺序;特别是它不保证该顺序恒久不变)
案例:创建一个HashSet集合,添加元素(String元素),测试唯一性,无序性
e.hash == hash && ((k = e.key) == key|| key.equals(k))
左边:e.hash == hash -- 和对象的hashCode()方法相关
将你即将添加进去的元素的哈希吗值和哈希表中已经存在的元素的哈希吗值进行比较,
如果相等:进行后面的判断
如果不等:将元素直接添加到哈希表中,说明这个元素在表中不存在
右边:((k = e.key) == key || key.equals(k)
左边:(k = e.key) == key
在这里比较对象的地址值
右边:key.equals(k)
默认情况下,比较的是地址值,在这里比较的是字符串的内容
保证元素唯一性的依赖的方法:hashCode(),equals()
执行流程:
首先判断元素的哈希吗值是否相同,如果元素的哈希吗值不同的话,直接添加,
如果元素的哈希吗值相同的话,判断元素的地址值或者元素的内容,
只要这两样有一个相同就说明是用同一个元素,不添加到集合
*/
public class HashSetDemo {
publicstatic void main(String[] args) {
//创建一个HashSet集合
HashSet<String>set = new HashSet<String>();
//给集合中添加元素
set.add("hello");
set.add("java");
set.add("world");
set.add("c++");
set.add("php");
set.add("hello");
//遍历集合
/**
* HashSet集合特点:
* 1.元素无序
* 2.元素唯一
*/
for(String string : set) {
System.out.println(string);
}
}
}
packagecom.edu_02;
importjava.util.HashSet;
public classHashSetDemo {
//3.存储自定义对象,如何保证元素的唯一性?
/**
*hashSet集合保证元素的唯一性依赖两个方法:hashCode()--为了让元素的哈希吗值相同,
* equals()--比较元素的成员变量值是否相同
* 在hashset集合中为了保证元素的唯一性,就必须重写存入hashset集合中元素所对应的类中hashCode(),equals()
*/
public static voidmain(String[] args) {
//创建集合
HashSet<Student> hs = newHashSet<Student>();
//创建学生对象
Student s1 = new Student("郭德纲", 50);
Student s2 = new Student("刘德华", 55);
Student s3 = new Student("张学友", 56);
Student s4 = new Student("黎明", 57);
Student s5 = new Student("郭德纲", 50);
//给集合中添加元素
hs.add(s1);
hs.add(s2);
hs.add(s3);
hs.add(s4);
hs.add(s5);
//遍历集合
for (Studentstudent : hs) {
System.out.println(student);
}
}
}
packagecom.edu_02;
public class Student {
private String name;
private int age;
public Student(String name,int age) {
super();
this.name = name;
this.age = age;
}
public Student() {
super();
// TODOAuto-generated constructor stub
}
public String getName() {
return name;
}
public voidsetName(String name) {
this.name = name;
}
public int getAge(){
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student[name=" + name + ", age=" + age + "]";
}
//重写hashCode(),equals()方法
@Override
public inthashCode() {
final int prime =31;
int result =1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public booleanequals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if(getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
packagecom.edu_03;
importjava.util.TreeSet;
public classTreeSetDemo {
public static voidmain(String[] args) {
/**
*1.创建集合存储Integer类型的元素(20,18,23,22,17,24,19,18,24),
* 分析如何保证元素唯一性,以及排序的规则。二叉树
*(首先讲解二叉树数据结构是怎么存入元素的,根据compareTo()方法的返回值来确定要存入元素的安放位置)
*
*/
//创建一个TreeSet集合
/**
*TreeSet集合特点:
*1.元素唯一
*2.元素有序
*/
TreeSet<Integer> ts = newTreeSet<Integer>();
//给集合中存储元素
//(20,18,23,22,17,24,19,18,24)
ts.add(20);
ts.add(18);
ts.add(23);
ts.add(22);
ts.add(17);
ts.add(24);
ts.add(19);
ts.add(18);
ts.add(24);
//遍历集合
for (Integerinteger : ts) {
System.out.println(integer);
}
}
}
packagecom.edu_03;
importjava.util.TreeSet;
public classTreeSetDemo2 {
public static voidmain(String[] args) {
/**
* 2.存储字符串并遍历(字母a-z排序)
*/
//创建一个TreeSet集合
//当存入字符串的时候,按照字典顺序进行排序
TreeSet<String> ts = newTreeSet<String>();
//存入字符串
ts.add("linzhiling");
ts.add("amu");
ts.add("bigbang");
ts.add("guodegang");
//遍历集合
for (Stringstring : ts) {
System.out.println(string);
}
}
}
packagecom.edu_04;
public class Student implementsComparable<Student>{
private String name;
private int age;
public Student(String name,int age) {
super();
this.name = name;
this.age = age;
}
public Student() {
super();
// TODOAuto-generated constructor stub
}
public String getName() {
return name;
}
public voidsetName(String name) {
this.name = name;
}
public int getAge(){
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student[name=" + name + ", age=" + age + "]";
}
//实现Comparable接口的同时必须实现这个比较法
@Override
public intcompareTo(Student s) {
//就是写的是元素的比较规则,由你自己去动手写出
//按照学生的年龄进行排序
/**
* 两个对象进行比较:
* s
*this
*/
int num = this.age - s.age;
//判断年龄是否相同,如果相同比较姓名
/**
* 写这个比较规则的时候注意两点:
*1.他有主要条件,先按照主要条件进行排序
*2.如果主要条件相同,就需要你自己分析出来他的次要条件,再去按照次要条件进行比较
*/
int num2 =num==0?this.name.compareTo(s.name):num;
return num2;
}
}
packagecom.edu_04;
importjava.util.TreeSet;
/**
* java.lang.ClassCastException:类型转换异常
* TreeSet集合有两种排序方式:至于哪两种排序方式,取决于他的构造器
* 自然排序:无参构造public TreeSet()
* 比较器排序(Comparator):
*/
public classTreeSetDemo {
public static voidmain(String[] args) {
//创建TreeSet集合,存储自定义对象
TreeSet<Student> ts = newTreeSet<Student>();
//给集合中添加Student对象
Student s = new Student("guodegang", 50);
Student s6 = new Student("liuyifei", 50);
Student s2 = new Student("zhangxueyou", 55);
Student s3 = new Student("amu", 45);
Student s4 = new Student("tf-boys", 18);
Student s5 = new Student("wangfeng", 49);
ts.add(s);
ts.add(s2);
ts.add(s3);
ts.add(s4);
ts.add(s5);
ts.add(s6);
//遍历集合
for (Studentstudent : ts) {
System.out.println(student);
}
}
}
packagecom.edu_05;
importjava.util.Comparator;
public classComparatorImpl implements Comparator<Student>{
@Override
public intcompare(Student s1, Student s2) {
/**
*s1--this
*s2 -- s
* 按照年龄进行排序,年龄相同按照姓名排序
*
*/
int num =s1.getAge() - s2.getAge();
int num2 =num==0?s1.getName().compareTo(s2.getName()):num;
return num2;
}
}
packagecom.edu_05;
public class Student{
private String name;
private int age;
public Student(String name,int age) {
super();
this.name = name;
this.age = age;
}
public Student() {
super();
// TODOAuto-generated constructor stub
}
public String getName() {
return name;
}
public voidsetName(String name) {
this.name = name;
}
public int getAge(){
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student[name=" + name + ", age=" + age + "]";
}
}
package com.edu_05;
import java.util.Comparator;
import java.util.TreeSet;
public class TreeSetDemo {
publicstatic void main(String[] args) {
//创建集合,参数是一个接口,实际需要的是接口的实现类对象
//TreeSet<Student>ts = new TreeSet<Student>(new ComparatorImpl());
//使用匿名内部类来进行改进
TreeSet<Student>ts = new TreeSet<Student>(new Comparator<Student>() {
@Override
publicint compare(Student s1, Student s2) {
intnum = s1.getAge() - s2.getAge();
intnum2 = num==0?s1.getName().compareTo(s2.getName()):num;
returnnum2;
}
});
//创建对象存入集合
Students = new Student("guodegang", 50);
Students6 = new Student("liuyifei", 50);
Students2 = new Student("zhangxueyou", 55);
Students3 = new Student("amu", 45);
Students4 = new Student("tf-boys", 18);
Students5 = new Student("wangfeng", 49);
ts.add(s);
ts.add(s2);
ts.add(s3);
ts.add(s4);
ts.add(s5);
ts.add(s6);
//遍历集合
for(Student student : ts) {
System.out.println(student);
}
}
}
packagecom.edu_06;
public class Car implementsComparable<Car>{
private String brand;
private int price;
public Car(String brand, int price) {
super();
this.brand = brand;
this.price = price;
}
public Car() {
super();
// TODOAuto-generated constructor stub
}
public String getBrand() {
return brand;
}
public voidsetBrand(String brand) {
this.brand = brand;
}
public intgetPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public intcompareTo(Car c) {
int num = this.price - c.price;
int num2 =num==0?this.brand.compareTo(c.brand):num;
return num2;
}
@Override
public String toString() {
return "Car[brand=" + brand + ", price=" + price + "]";
}
}
package com.edu_06;
import java.util.Iterator;
import java.util.TreeSet;
public class TreeSetDemo {
publicstatic void main(String[] args) {
//使用自然排序对存储进来的Car对象进行排序
TreeSet<Car>ts = new TreeSet<Car>();
//创建汽车对象
Carc1 = new Car("lanbojini", 1000);
Carc2 = new Car("tesila", 1000);
Carc3 = new Car("mashaladi", 2000);
//存入集合
ts.add(c1);
ts.add(c2);
ts.add(c3);
//遍历集合
Iterator<Car>it = ts.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}
}
packagecom.edu_07;
public class Car{
private String brand;
private int price;
public Car(String brand, int price) {
super();
this.brand = brand;
this.price = price;
}
public Car() {
super();
// TODOAuto-generated constructor stub
}
public String getBrand() {
return brand;
}
public voidsetBrand(String brand) {
this.brand = brand;
}
public intgetPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return "Car[brand=" + brand + ", price=" + price + "]";
}
}
package com.edu_07;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
* * Map接口中的方法概述(创建集合测试方法):
* A:删除功能
* voidclear():移除集合中的所有键值对元素
* Vremove(Object key):根据键移除键值对元素,并返回值
* B:判断功能
* booleancontainsKey(Object key):判断集合中是否包含指定的键
* booleancontainsValue(Object value):判断集合中是否包含指定的值
* booleanisEmpty():判断集合是否为空
* C:获取功能
* Set<Map.Entry<K,V>>entrySet():获取键值对对象的集合,遍历键值对对象,
利用getKey(),getValue()取出键和值(理解即可)
*
Vget(Object key):根据键获取值
* Set<K>keySet():获取所有的键
* Collection<V>values():获取所有的值
* D:添加功能
* Vput(K key,V value):集合添加键值对
* E:长度功能
* intsize():键值对对数。
public class MapDemo {
publicstatic void main(String[] args) {
//创建一个Map集合
//键是学号--值是姓名
Map<Integer,String>map = new HashMap<Integer,String>();
//Vput(K key,V value):集合添加键值对
map.put(1,"周杰伦");
map.put(2,"郭德纲");
map.put(3,"刘德华");
map.put(4,"张学友");
//voidclear():移除集合中的所有键值对元素
//map.clear();
//Vremove(Object key):根据键移除键值对元素,并返回值
//Stringremove = map.remove(1);
//System.out.println(remove);
//booleancontainsKey(Object key):判断集合中是否包含指定的键
//booleancontainsKey = map.containsKey(2);
//System.out.println(containsKey);
//booleancontainsValue(Object value):判断集合中是否包含指定的值
//booleancontainsValue = map.containsValue("周杰伦");
//System.out.println(containsValue);
//booleanisEmpty():判断集合是否为空
//System.out.println(map.isEmpty());
//intsize():键值对对数。
//System.out.println(map.size());
//Collection<V>values():获取所有的值
/*
Collection<String>values = map.values();
Iterator<String>it = values.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}*/
//Set<Map.Entry<K,V>>entrySet():获取键值对对象的集合,遍历键值对对象,
//利用getKey(),getValue()取出键和值(理解即可)
Set<Entry<Integer,String>>entrySet = map.entrySet();
for(Entry<Integer, String> entry : entrySet) {
System.out.println(entry.getKey()+" "+entry.getValue());
}
System.out.println("--------------------");
//Set<K>keySet():获取所有的键
Set<Integer>keys = map.keySet();
for(Integer key : keys) {
//Vget(Object key):根据键获取值
System.out.println(key+" "+map.get(key));
}
}
}
package com.edu_07;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
public class TreeSetDemo {
publicstatic void main(String[] args) {
//创建一个TreeSet集合,使用比较器排序
TreeSet<Car>ts = new TreeSet<>(new Comparator<Car>() {
@Override
publicint compare(Car c1, Car c2) {
intnum = c1.getPrice() - c2.getPrice();
intnum2 = num==0?c1.getBrand().compareTo(c2.getBrand()):num;
returnnum2;
}
});
//创建汽车对象
Carc1 = new Car("lanbojini", 1000);
Carc2 = new Car("tesila", 1000);
Carc3 = new Car("mashaladi", 2000);
//存入集合
ts.add(c1);
ts.add(c2);
ts.add(c3);
//遍历集合
Iterator<Car>it = ts.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}
}
package com.edu_08;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
/**
* 2.HashMap
2.1元素顺序:元素顺序不可预测
2.2底层算法:哈希算法
2.3对键没有要求(仅仅相对于TreeMap来说)
*/
public class HashMapDemo {
publicstatic void main(String[] args) {
//1.存入(String,String)主要讲解遍历方式,键:丈夫 值:妻子
HashMap<String,String> hm = new HashMap<String,String>();
//给键值对集合存入元素
hm.put("文章", "马伊琍");
hm.put("黄晓明","baby");
hm.put("汪老师", "章子怡");
hm.put("周杰伦", "昆凌");
//hm.put("文章", "姚笛");,当后面存入的元素和前面的键的值相同的时候,前面的元素的值会被后面的元素的值代替
//遍历,通过建找值
Set<String>keys = hm.keySet();
for(String key : keys) {
System.out.println(key+" "+hm.get(key));
}
System.out.println("-------------");
//通过找到结婚证,来分别找到丈夫和妻子
Set<Entry<String,String>>entrySet = hm.entrySet();
for(Entry<String, String> entry : entrySet) {
System.out.println(entry.getKey()+" "+entry.getValue());
}
}
}
package com.edu_09;
import java.util.HashMap;
import java.util.Set;
public class HashMapDemo {
publicstatic void main(String[] args) {
//2.存入(String,Student)键:String(国籍) 值:Student
HashMap<String,Student> hm = new HashMap<String, Student>();
//创建学生对象
Students1 = new Student("杰克逊",60);
Students2 = new Student("孙楠",50);
Students3 = new Student("权志龙",30);
//将对象存入集合
hm.put("美国", s1);
hm.put("中国", s2);
hm.put("韩国", s3);
//同过键找值
Set<String>keys = hm.keySet();
for(String key : keys) {
System.out.println(key+" "+hm.get(key));
}
}
}
packagecom.edu_09;
public class Student{
private String name;
private int age;
public Student(String name,int age) {
super();
this.name = name;
this.age = age;
}
public Student() {
super();
// TODOAuto-generated constructor stub
}
public String getName() {
return name;
}
public voidsetName(String name) {
this.name = name;
}
public int getAge(){
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student[name=" + name + ", age=" + age + "]";
}
}
package com.edu_10;
import java.util.HashMap;
import java.util.Set;
public class HashMapDemo {
publicstatic void main(String[] args) {
//3.存入(Student,String)
//创建一个Map集合
HashMap<Student,String> hm = new HashMap<Student, String>();
//创建学生对象
Students1 = new Student("杰克逊",60);
Students2 = new Student("孙楠",50);
Students3 = new Student("权志龙",30);
Students4 = new Student("权志龙",30);
//将对象存入集合
hm.put(s1,"美国");
hm.put(s2,"中国");
hm.put(s3,"韩国");
hm.put(s4,"中国");
//遍历集合
Set<Student>keys = hm.keySet();
for(Student s : keys) {
System.out.println(s+" "+hm.get(s));
}
}
}
packagecom.edu_10;
public class Student{
private String name;
private int age;
public Student(String name,int age) {
super();
this.name = name;
this.age = age;
}
public Student() {
super();
// TODOAuto-generated constructor stub
}
public String getName() {
return name;
}
public voidsetName(String name) {
this.name = name;
}
public int getAge(){
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student[name=" + name + ", age=" + age + "]";
}
@Override
public inthashCode() {
final int prime =31;
int result =1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public booleanequals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if(getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}