1 Set集合
1.1Set结合特点
元素存取无序
没有索引、只能通过迭代器或增强for循环遍历
不能存储重复元素
1.2 哈希值
哈希值简介:是JDK根据对象的地址或者字符串或者数字算出来的int类型的数值。
如何获取哈希值:Object类中的public int hashCode():返回对象的哈希码值。
哈希值的特点:
同一个对象多次调用hashCode()方法返回的哈希值是相同的。
默认情况下,不同对象的哈希值是不同的。而重写hashCode()方法,可以实现让不同对象的哈希值相同。
1.3 HashSet集合
特点:
底层数据结构是哈希表。
对集合的迭代顺序不作任何保证,也就是说不保证存储和取出的元素顺序一致。
没有带索引的方法,所以不能使用普通for循环遍历。
由于是Set集合,所以是不包含重复元素的集合。
1.4 HashSet集合对存储学生遍历
学生对象的成员变量值相同,我们就认为是同一个对象,我们需要在Student对象中重写equals和hashCode方法
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
if (age != student.age) return false;
return name != null ? name.equals(student.name) : student.name == null;
}
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + age;
return result;
}
1.5 LinkedHashSet集合
特点:
哈希表和链表实现的Set接口,具有可预测的迭代次序。
由链表保证元素有序,也就是说元素的存储和取出顺序是一致的。
由哈希表保证元素唯一,也就是说没有重复的元素。
2 Set集合排序
2.1 TreeSet集合
TreeSet集合概述:
元素有序,可以按照一定的规则进行排序,具体排序方式取决于构造方法。
TreeSet():根据其元素的自然排序进行排序。
TreeSet(Comparator comparator) :根据指定的比较器进行排序。
· 没有带索引的方法,所以不能使用普通for循环遍历。
由于是Set集合,所以不包含重复元素的集合。
2.2 自然排序Comparable的使用
例子:
public class Student implements Comparable<Student> {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
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;
}
@Override
public int compareTo(Student s) {
// return 0;
// return 1;
// return -1;
//按照年龄从小到大排序
int num = this.age - s.age;
// int num = s.age - this.age;
//年龄相同时,按照姓名的字母顺序排序
int num2 = num==0?this.name.compareTo(s.name):num;
return num2;
}
}
public class TreeSetDemo02 {
public static void main(String[] args) {
//创建集合对象
TreeSet<Student> ts = new TreeSet<Student>();
//创建学生对象
Student s1 = new Student("xishi", 29);
Student s2 = new Student("wangzhaojun", 28);
Student s3 = new Student("diaochan", 30);
Student s4 = new Student("yangyuhuan", 33);
Student s5 = new Student("linqingxia",33);
Student s6 = new Student("linqingxia",33);
//把学生添加到集合
ts.add(s1);
ts.add(s2);
ts.add(s3);
ts.add(s4);
ts.add(s5);
ts.add(s6);
//遍历集合
for (Student s : ts) {
System.out.println(s.getName() + "," + s.getAge());
}
}
}
2.3 比较器排序Comparator的使用
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
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 class TreeSetDemo {
public static void main(String[] args) {
//创建集合对象
TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
//this.age - s.age
//s1,s2
int num = s1.getAge() - s2.getAge();
int num2 = num == 0 ? s1.getName().compareTo(s2.getName()) : num;
return num2;
}
});
//创建学生对象
Student s1 = new Student("xishi", 29);
Student s2 = new Student("wangzhaojun", 28);
Student s3 = new Student("diaochan", 30);
Student s4 = new Student("yangyuhuan", 33);
Student s5 = new Student("linqingxia",33);
Student s6 = new Student("linqingxia",33);
//把学生添加到集合
ts.add(s1);
ts.add(s2);
ts.add(s3);
ts.add(s4);
ts.add(s5);
ts.add(s6);
//遍历集合
for (Student s : ts) {
System.out.println(s.getName() + "," + s.getAge());
}
}
}
3 泛型
3.1 泛型定义格式:
<类型>:指定一种类型的格式。这里的类型可以看成是形参。
<类型1,类型2…>:指定多种类型的格式,多种类型之间用逗号隔开。这里的类型可以看成是形参。
将来具体调用时候给定的类型可以看成是实参,并且实参的类型只能是引用数据类型。
泛型的好处:
把运行时期的问题提前到了编译期间。
避免了强制类型转换。
3.2 泛型类
public class Generic<T> { //泛型类
private T t;
public T getT() {
return t;
}
public void setT(T t) {
this.t = t;
}
}
public class GenericDemo {
public static void main(String[] args) { //测试类
Generic<String> g1 = new Generic<String>();
g1.setT("林青霞");
System.out.println(g1.getT());
Generic<Integer> g2 = new Generic<Integer>();
g2.setT(30);
System.out.println(g2.getT());
Generic<Boolean> g3 = new Generic<Boolean>();
g3.setT(true);
System.out.println(g3.getT());
}
}
3.3 泛型方法
定义格式:修饰符 <类型> 返回值类型 方法名(类型 变量名) { }
带有泛型方法的类:
public class Generic {
public <T> void show(T t) {
System.out.println(t);
}
}
测试类:
public class GenericDemo {
public static void main(String[] args) {
Generic g = new Generic();
g.show("林青霞");
g.show(30);
g.show(true);
g.show(12.34);
}
}
3.4 泛型接口
定义格式:修饰符 interface 接口名<类型> { }
泛型接口:
public interface Generic<T> {
void show(T t);
}
泛型接口实现类:
public class GenericImpl<T> implements Generic<T> {
@Override
public void show(T t) {
System.out.println(t);
}
}
测试类
public class GenericDemo {
public static void main(String[] args) {
Generic<String> g1 = new GenericImpl<String>();
g1.show("林青霞");
Generic<Integer> g2 = new GenericImpl<Integer>();
g2.show(30);
}
}
3.5 类型通配符
类型通配符的作用:为了表示各种泛型List的父类,可以使用类型通配符。
类型通配符的分类:
类型通配符:<?>:
List<?>:表示元素类型未知的List,它的元素可以匹配任何的类型。
这种带通配符的List仅表示它是各种泛型List的父类,并不能把元素添加到其中。
类型通配符上限:<? extends 类型>:
List<? extends Number>:它表示的类型是Number或者其子类型。
类型通配符下限:<? super 类型>:
List<? super Number>:它表示的类型是Number或者其父类型。
类型通配符的基本使用:
public class GenericDemo {
public static void main(String[] args) {
//类型通配符:<?>
List<?> list1 = new ArrayList<Object>();
List<?> list2 = new ArrayList<Number>();
List<?> list3 = new ArrayList<Integer>();
System.out.println("--------");
//类型通配符上限:<? extends 类型>
// List<? extends Number> list4 = new ArrayList<Object>();
List<? extends Number> list5 = new ArrayList<Number>();
List<? extends Number> list6 = new ArrayList<Integer>();
System.out.println("--------");
//类型通配符下限:<? super 类型>
List<? super Number> list7 = new ArrayList<Object>();
List<? super Number> list8 = new ArrayList<Number>();
// List<? super Number> list9 = new ArrayList<Integer>();
}
}
4 可变参数
4.1 可变参数
可变参数介绍:可变参数又称参数个数可变,用作方法的形参出现,那么方法参数个数就是可变的了。
可变参数定义格式:修饰符 返回值类型 方法名(数据类型… 变量名) { }
可变参数的注意事项:
这里的变量其实是一个数组。
如果一个方法有多个参数,包含可变参数,可变参数要放在最后。
可变参数的基本使用:
public class ArgsDemo01 {
public static void main(String[] args) {
System.out.println(sum(10, 20));
System.out.println(sum(10, 20, 30));
System.out.println(sum(10, 20, 30, 40));
System.out.println(sum(10,20,30,40,50));
System.out.println(sum(10,20,30,40,50,60));
System.out.println(sum(10,20,30,40,50,60,70));
System.out.println(sum(10,20,30,40,50,60,70,80,90,100));
}
// public static int sum(int b,int... a) {
// return 0;
// }
public static int sum(int... a) {
int sum = 0;
for(int i : a) {
sum += i;
}
return sum;
}
}
4.2可变参数的使用
Arrays工具类中有一个静态方法:
public static <T> List<T> asList(T... a):返回由指定数组支持的固定大小的列表。
返回的集合不能做增删操作,可以做修改操作。
List接口中有一个静态方法:
public static <E> List<E> of(E... elements):返回包含任意数量元素的不可变列表。
返回的集合不能做增删改操作 。
Set接口中有一个静态方法:
public static <E> Set<E> of(E... elements) :返回一个包含任意数量元素的不可变集合。
在给元素的时候,不能给重复的元素。
返回的集合不能做增删操作,没有修改的方法。
例子:
public class ArgsDemo02 {
public static void main(String[] args) {
//public static <T> List<T> asList(T... a):返回由指定数组支持的固定大小的列表
// List<String> list = Arrays.asList("hello", "world", "java");
//
list.add("javaee"); //UnsupportedOperationException
list.remove("world"); //UnsupportedOperationException
// list.set(1,"javaee");
//
// System.out.println(list);
//public static <E> List<E> of(E... elements):返回包含任意数量元素的不可变列表
// List<String> list = List.of("hello", "world", "java", "world");
//
list.add("javaee");//UnsupportedOperationException
list.remove("java");//UnsupportedOperationException
list.set(1,"javaee");//UnsupportedOperationException
//
// System.out.println(list);
//public static <E> Set<E> of(E... elements) :返回一个包含任意数量元素的不可变集合
// Set<String> set = Set.of("hello", "world", "java","world"); //IllegalArgumentException
//Set<String> set = Set.of("hello", "world", "java");
// set.add("javaee");//UnsupportedOperationException
// set.remove("world");//UnsupportedOperationException
//System.out.println(set);
}
}