文章目录
- 1、Arrays
- 1.1 简述
- 1.2 如何比较对象数组
- 1.2.1 方法1.
- 1.2.2 方式2
- 2、Lambda
- 3、方法引用
- 3.1 静态方法的引用
- 3.2 实例方法引用
- 3.3 特定类型方法引用
- 3.4 构造器引用
1、Arrays
1.1 简述
操作数组的一个工具类
常见方法
int[] arr={10,20,30,50,40};//将 数组的内容 组装成一个String对象 格式[,,,]String s = Arrays.toString(arr);
System.out.println(s);//[10, 20, 30, 50, 40]
//将 [Start,End] 范围内的数组复制给一个新的数组
int[] newarr = Arrays.copyOfRange(arr, 1, 4); System.out.println(Arrays.toString(newarr));//[20, 30, 50]
//将全部数组copy给一个新数组,并且能重新定义数组长度
int[] newarr1 = Arrays.copyOf(arr, 8); System.out.println(Arrays.toString(newarr1));//[10, 20, 30, 50, 40, 0, 0, 0]
//比较 sort 默认升序
Arrays.sort(arr); System.out.println(Arrays.toString(arr));//[10, 20, 30, 40, 50]
//把数组中的数据改为新数据再存进去
double[] arr1={100,100.8,50};
Arrays.setAll(arr1,new IntToDoubleFunction(){@Overridepublic double applyAsDouble(int value) {return arr1[value]*0.8; //每个*0.8}
}); System.out.println(Arrays.toString(arr1)); //[80.0, 80.64, 40.0]
1.2 如何比较对象数组
比如 有一个学生类 包含姓名,身高,年龄,如果我们直接使用sort 对该学生数组排序 会出现错误。
两种方法
1.2.1 方法1.
让类的对象实现Comparable接口 重写compareTo方法
Student 类
package com.cky.mathclass;public class Student implements Comparable<Student>{private String name;private double height;private int age;public Student(String name, double height, int age) {this.name = name;this.height = height;this.age = age;}public Student() {}public String getName() {return name;}public void setName(String name) {this.name = name;}public double getHeight() {return height;}public void setHeight(double height) {this.height = height;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic int compareTo(Student o) {//这里根据年龄排序//this 0 this>0 正整数 this<0 负整数 this=o 0return this.age-o.age; //升序//return o.age-this.age//降序}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", height=" + height +", age=" + age +'}';}
}
System.out.println(Arrays.toString(arr1)); //[80.0, 80.64, 40.0]
Student[] students=new Student[3];
students[0]=new Student("cui",165,24);
students[1]=new Student("jiang",182,24);
students[2]=new Student("chu",167,23);
//方式1 在类中实现Comparable接口 重写compareTo方法
Arrays.sort(students);
System.out.println(Arrays.toString(students));
//[Student{name='chu', height=167.0, age=23}, Student{name='cui', height=165.0, age=24}, Student{name='jiang', height=182.0, age=24}]
1.2.2 方式2
//方式2 Comparator匿名内部类
Arrays.sort(students, new Comparator<Student>() {@Overridepublic int compare(Student o1, Student o2) {//这里根据身高排序//注意 我们返回的需要是整数 不能直接 //return o1.getHeight()-o2.getHeight();
//if(o1.getHeight()>o2.getHeight())
// return 1;
// else if(o1.getHeight()<o2.getHeight())
// return -1;
// return 0;//也可以 与上边的if语句一样 内部实现了
return Double.compare(o1.getHeight(),o2.getHeight());
}});
2、Lambda
jak8新特性
lambda 用于简化匿名内部类的书写
重点:
只能简化函数式接口匿名内部类的书写
函数式接口
:只有一个方法的接口
格式
:
(被重写方法的形参)->{方法体};
如上边的setall
IntToDoubleFunction()是一个函数式接口
函数式接口 我们可以在其上边添加上
@FunctionalInterface
注解
Arrays.setAll(arr1,new IntToDoubleFunction(){@Overridepublic double applyAsDouble(int value) {return arr1[value]*0.8; //每个*0.8}
});
Arrays.setAll(arr1,(int value) ->{return arr1[value]*0.8; //每个*0.8});
其他简化形式:
Arrays.setAll(arr1, value->{return arr1[value]*0.8; //每个*0.8});
最简格式:
Arrays.setAll(arr1,value-> arr1[value]*0.8); //每个*0.8
3、方法引用
package com.cky.mathclass;public class Compareclass {public static int Comarebydate(Student o1,Student o2){return o1.getAge()-o2.getAge();}public int Comarebydatedecs(Student o1,Student o2){return o2.getAge()-o1.getAge();}
}
3.1 静态方法的引用
Arrays.sort(students, new Comparator<Student>() {@Overridepublic int compare(Student o1, Student o2) {return o1.getAge()-o2.getAge();}});
//Arrays.sort(students, (o1, o2) ->Compareclass.Comarebydate(o1,o2));
Arrays.sort(students, Compareclass::Comarebydate);}
3.2 实例方法引用
Arrays.sort(students, new Comparator<Student>() {@Overridepublic int compare(Student o1, Student o2) {return o2.getAge()-o1.getAge();}});Compareclass compareclass=new Compareclass();
Arrays.sort(students, (o1, o2) -> compareclass.Comarebydatedecs(o1,o2));
//最终简化
Arrays.sort(students, compareclass::Comarebydatedecs);
3.3 特定类型方法引用
String[] names={"Anny","anbs","Cabd","Bsad","bsa"};Arrays.sort(names); System.out.println(Arrays.toString(names));//[Anny, Bsad, Cabd, anbs, bsa]
//我们想要的是 忽略大小写
Arrays.sort(names, new Comparator<String>() {@Overridepublic int compare(String o1, String o2) {
return o1.compareToIgnoreCase(o2);}});
System.out.println(Arrays.toString(names));//[anbs, Anny, bsa, Bsad, Cabd]
//简化 Arrays.sort(names,String::compareToIgnoreCase);
System.out.println(Arrays.toString(names));
3.4 构造器引用
package com.cky.mathclass;public class Car {private String name;private double price;public Car(String name, double price) {this.name = name;this.price = price;}public String getName() {return name;}public void setName(String name) {this.name = name;}public double getPrice() {return price;}@Overridepublic String toString() {return "Car{" +"name='" + name + '\'' +", price=" + price +'}';}public void setPrice(double price) {this.price = price;}public Car() {}
}
public class Dateclass {public static void main(String[] args) {Carable c=new Carable(){@Overridepublic Car ceateCar(String name, double price) {return new Car(name,price);}};Carable c1=(name, price)-> new Car(name,price);Carable c2=Car::new;System.out.println(c2.ceateCar("ni", 1000000));}}
@FunctionalInterface
interface Carable{Car ceateCar(String name,double price);
}