一、集合框架Set(HashSet哈希表存储、重复元素存储底层探究)
- 1.set集合不能存放重复元素的问题
- 不能存放元素 字符串 八大基本数据类型
- set集合中的元素是无序的
- 2.HashSet哈希表存储,重复元素存储底层探究
-
list.contains 底层调用了equals方法
-
set.add 底层调用hashCode/equals
public static void main(String[] args) {
Set set=new HashSet<>();
set.add(new Person("张三",18,1600));
set.add(new Person("李四",28,1500));
set.add(new Person("哈哈",19,1800));
set.add(new Person("嗯嗯",20,1000));
set.add(new Person("张三",18,1600));
System.out.println(set.size());
Iterator it=set.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}
}
class Person{
private String name;
private int age;
private int money;
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;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", money=" + money + "]";
}
public Person(String name, int age, int money) {
super();
this.name = name;
this.age = age;
this.money = money;
}
public Person() {
super();
}
@Override
public int hashCode() {
System.out.println("hashCode----------"+this.name);
int code=this.name.hashCode()+this.age;
System.out.println(code);
return code;
}
@Override
public boolean equals(Object obj) {
Person p=(Person) obj;
return this.name.equals(p.name) && this.age==p.age;
}
输出结果
二、集合框架TreeSet(自然排序、数据结构二叉树、比较器排序)
1.TreeSet可以对set集合中元素进行排序
public static void main(String[] args) {
TreeSet set=new TreeSet<>();
set.add(22);
set.add(24);
set.add(26);
set.add(21);
set.add(30);
System.out.println(set);
}
在这里插入代码片
输出
2.自然排序
引用数据类型想要排序,必须实现Comparable接口
注意:排序时,当主要条件相同时,一定要判断次要条件
TreeSet set=new TreeSet<>();
set.add(new Person("张三",18,1600));
set.add(new Person("李四",28,1500));
set.add(new Person("哈哈",19,2800));
set.add(new Person("嗯嗯",20,1000));
set.add(new Person("张三",38,1600));
System.out.println(set);
Iterator it=set.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}
}
class Person implements Comparable<Person>{
private String name;
private int age;
private int money;
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;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", money=" + money + "]";
}
public Person(String name, int age, int money) {
super();
this.name = name;
this.age = age;
this.money = money;
}
public Person() {
super();
}
@Override
public int hashCode() {
System.out.println("hashCode----------"+this.name);
int code=this.name.hashCode()+this.age;
System.out.println(code);
return code;
}
@Override
public boolean equals(Object obj) {
Person p=(Person) obj;
return this.name.equals(p.name) && this.age==p.age;
}
/**
* 让元素具备比较器
* 注意:
* 在做自然排序方法重写的时候,一定要先判断主要条件然后还要判断次要条件
*/
@Override
public int compareTo(Person o) {
int num=o.money-this.money;
if(num==0) {
return o.age-this.age;
}
return num;
}
在这里插入代码片
2.比较器排序
当元素自身不具备比较性时,或者具备的比较性不是所需要的;
注意:这时需要让集合自身具备比较性
在集合初始化时,就有了比较方式;
在这里插入代码片
public static void main(String[] args) {
// TreeSet<Object> set = new TreeSet<>();
// TreeSet<Person> set = new TreeSet<>(new PersonAgaMoneyComp());
TreeSet<Person> set = new TreeSet<>(new PersonMoneyAgeComp());
set.add(new Person("张三",18,1600));
set.add(new Person("李四",28,2500));
set.add(new Person("2四",28,500));
set.add(new Person("哈哈",19,1800));
set.add(new Person("嗯嗯",20,1000));
set.add(new Person("张三",18,1600));
// abc/bc/de/dea/def
// Student extends/implements Person
// Student stu = (Student)p;
// System.out.println(set);
Iterator it = set.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}
}
/**
* 年轻有钱
* @author Administrator
*
*/
class PersonAgaMoneyComp implements Comparator<Person>{
@Override
public int compare(Person o1, Person o2) {
int num = o1.getAge() - o2.getAge();
if (num == 0) {
return o2.getMoney() - o1.getMoney();
}
return num;
}
}
/**
* 有钱年轻
* @author Administrator
*
*/
class PersonMoneyAgeComp implements Comparator<Person>{
@Override
public int compare(Person o1, Person o2) {
int num = o2.getMoney() - o1.getMoney();
if (num == 0) {
return o1.getAge() - o2.getAge();
}
return num;
}
}
class Person implements Comparable<Person>{
private String name;
private int age;
private int money;
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;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", money=" + money + "]";
}
public Person(String name, int age, int money) {
super();
this.name = name;
this.age = age;
this.money = money;
}
public Person() {
super();
}
@Override
public int hashCode() {
System.out.println("hashCode---------"+this.name);
int code=this.name.hashCode() + this.age;
System.out.println(code);
return code;
}
@Override
public boolean equals(Object obj) {
Person p = (Person) obj;
return this.name.equals(p.name) && this.age == p.age;
}
/**
* 让元素具备比较性
* 注意:
* 在做自然排序方法重写的时候,一定先判断主要条件、还要判断次要条件
*/
@Override
public int compareTo(Person o) {
int num = o.money-this.money;
if (num == 0) {
return o.age - this.age;
}
return num;
}
输出
3.泛型
1.不使用泛型的情况下,会将未知的错误表现在运行时期,
List c=new ArrayList<>();
c.add(23);
c.add(25);
c.add(27);
c.add(28);
c.add(30);
c.add("s");
Iterator it=c.iterator();
while(it.hasNext()) {
Object obj=it.next();
int num=(int) obj;
if(num%2==0) {
System.out.println(num);
}
}
}
在这里插入代码片
2.如果说用代码去处理了,那么这个可能发现的错误,运行的时候错误就不会暴露出来,
List c=new ArrayList<>();
c.add(23);
c.add(25);
c.add(27);
c.add(28);
c.add(30);
c.add("s");
Iterator it=c.iterator();
while(it.hasNext()) {
Object obj=it.next();
if(obj instanceof Integer) {
int num=(int) obj;
if(num%2==0) {
System.out.println(num);
}
}
}
}
在这里插入代码片
泛型类
泛型接口
泛型方法
对于编码而言有什么更深层次的好处呢?
/**
- 购物车项目
- 订单模块,用户模块、商品模块等。
- Class OrderDao{
- public List list(Order order){
- }
- public int add(Order order){}
- public int edit(Order order){}
- public int del(Order order){}
- }
- Class UserDao{
- public List list(User User){}
- public int add(User User){}
- public int edit(User User){}
- public int del(User User){}
- }
- Class ProductDao{
- }
- --------不使用泛型的情况-----------------
- --------使用泛型的情况-----------------
- Class BaseDao{
- public List list(T t){}
- public int add(T t){}
- public int edit(T t){}
- public int del(T t){}
- }
- Class OrderDao extends BaseDao{}
- Class UserDao extends BaseDao{}
- Class ProductDao extends BaseDao{}
*/ 在这里插入代码片