JAVASE入门十脚-红黑树,比较器,泛型
红黑树Set,ArrayList数组,LinkedList链表
AVL二叉树
红黑树
插入节点一般是红色
treeSet()底层是treeMap,利用红黑树来实现
package ContainerDemo1;
import java.util.Objects;
public class PersonDemo {
private String name;
private int age;
public PersonDemo(){
}
public PersonDemo(int age, String name) {
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PersonDemo that = (PersonDemo) o;
return age == that.age && Objects.equals(name, that.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
@Override
public String toString() {
return "PersonDemo{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
package ContainerDemo1;
import java.util.HashSet;
public class TestDemoo {
public static void main(String[] args) {
HashSet hashSet=new HashSet();
PersonDemo personDemo=new PersonDemo(15,"张三");
PersonDemo personDemo1=new PersonDemo(16,"li三");
PersonDemo personDemo2=new PersonDemo(15,"张三");
hashSet.add(personDemo);
hashSet.add(personDemo1);
hashSet.add(personDemo2);
System.out.println(hashSet);
}
}
1、set中存放的是无序,唯一的数据
2.set不可以通过下标获取对应位置的元素的值,因为无序的特点
3、使用treeset底层的实现是treemap,利用红黑树来进行实现
4、设置元素的时候,如果是自定义对象,会直找对象中的equals和hashcode的方法,如果没有,比较的
5、树中的元素是要默认进行排序操作的,如果是基本数据类型,下动比较,如果是引用类型的话,需要
比较器分类:
内部比较器
定义在元素的类中,通过实现comparable接口来进行实现
外部比较器
定义在当前类中,通过实现comparator接口来实现,但是要将该比较器传递到集合中
注意:外部比较器可以定义建一个工具类。此时所有而要比较的规则如果致的话,可以复用,
内部比较器只有在存储当前对象的时候才可以使用
当两者同时用的时候使用外部比较器
TreeSet定义内部比较器;
package ContainerDemo1;
import java.util.Objects;
public class PersonDemo implements Comparable{
private String name;
private int age;
public PersonDemo(){
}
public PersonDemo(int age, String name) {
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PersonDemo that = (PersonDemo) o;
return age == that.age && Objects.equals(name, that.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
@Override
public String toString() {
return "PersonDemo{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
/*
此比较器按照name的长度来比较
*/
@Override
public int compareTo(Object o) {
PersonDemo personDemo=(PersonDemo)o;
if(personDemo.name.length()>this.name.length()){
return 1;
}
else if(personDemo.name.length()<this.name.length()){
return -1;
}
else {
return 0;}
}
}
package ContainerDemo1;
import java.util.HashSet;
import java.util.TreeSet;
public class TestDemoo {
public static void main(String[] args) {
TreeSet treeSet=new TreeSet();
PersonDemo personDemo=new PersonDemo(15,"张三");
PersonDemo personDemo1=new PersonDemo(16,"li三");
PersonDemo personDemo2=new PersonDemo(15,"张三");
treeSet.add(personDemo);
treeSet.add(personDemo1);
treeSet.add(personDemo2);
System.out.println(treeSet);
}
}
外部比较器:
package ContainerDemo1;
import java.util.Comparator;
import java.util.HashSet;
import java.util.TreeSet;
public class TestDemoo implements Comparator<PersonDemo> {
public static void main(String[] args) {
TreeSet treeSet=new TreeSet(new TestDemoo());
PersonDemo personDemo=new PersonDemo(15,"张三");
PersonDemo personDemo1=new PersonDemo(16,"li三");
PersonDemo personDemo2=new PersonDemo(15,"张三");
treeSet.add(personDemo);
treeSet.add(personDemo1);
treeSet.add(personDemo2);
System.out.println(treeSet);
}
@Override
public int compare(PersonDemo o1, PersonDemo o2) {
if(o1.getAge()>o2.getAge()){
return -1;
}else if(o1.getAge()<o2.getAge()){
return 1;
}else {
return 0;
}}
}
package ContainerDemo1;
import java.util.Objects;
public class PersonDemo /*implements Comparable*/{
private String name;
private int age;
public PersonDemo(){
}
public PersonDemo(int age, String name) {
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PersonDemo that = (PersonDemo) o;
return age == that.age && Objects.equals(name, that.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
@Override
public String toString() {
return "PersonDemo{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
/*
此比较器按照name的长度来比较
*/
// @Override
// public int compareTo(Object o) {
// PersonDemo personDemo=(PersonDemo)o;
// if(personDemo.name.length()>this.name.length()){
// return 1;
// }
// else if(personDemo.name.length()<this.name.length()){
// return -1;
// }
// else {
// return 0;}
//
// }
}
泛型
package ContainerDemo1;
import java.util.ArrayList;
import java.util.List;
public class ModernDemo {
public static void main(String[] args) {
List<String> list=new ArrayList<String>();
list.add("415");
list.add("撒");
list.add("555");
list.add(new Dog("455","123").toString());
System.out.println(list);
for(String ite:list){
System.out.println(ite);
}
}
}
当做一些集合的统一操作的时候,需要保证集合的类型是统的,此时需要泛型来进行限制
优点:
1、数据安全
2、获取数据时效率比较高
给集合中的元素没置相同的类型就是泛型的基本需求
使用:
在定义对象的时候,通过 中没置合理的类型来进行实现
泛型类
package ContainerDemo1;
public class FanXingClass<A> {
private int id;
private A a;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public A getA() {
return a;
}
public void setA(A a) {
this.a = a;
}
public void show(){
System.out.println(id+","+a);
}
}
package ContainerDemo1;
public class FanXingDemo {
public static void main(String[] args) {
FanXingClass<String> fanXing=new FanXingClass<String>();
fanXing.setA("机");
fanXing.setId(55);
fanXing.show();
}
}