1.函数式接口
消费型接口,断言型接口
@FunctionalInterface
public interface MyInterface {
public void show();
}
public class Test {
public static void main(String[] args) {
// 匿名内部类的写法
MyInterface interface1 = new MyInterface() {
@Override
public void show() {
System.out.println("aaa");
}
};
MyInterface interface2 = () -> System.out.println();
}
}
public class Test2 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("aaaa");
list.add("b");
list.add("cc");
list.add("aaaaddd");
// 利用断言型接口实现
list.removeIf(t-> t.length()>3);
// 利用消费型接口实现
list.forEach(t->System.out.println(t));
list.forEach(System.out::println);
for (String string : list) {
System.out.println(string);
}
String upper = toUpper("aaaaa", t-> t.toUpperCase());
System.out.println(upper);
例:定义一个方法, 此方法的作用就是 创建一个集合 通过supplier 创建10个随机数放入到集合, 并将集合返回
List<Integer> list2 = getList(()->(int)(Math.random()*100));
System.out.println(list2);
}
public static List<Integer> getList(Supplier<Integer> supplier){
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i <10; i++) {
Integer num = supplier.get();
list.add(num);
}
return list;
}
public static String toUpper(String str,Function<String, String> fun){
String str2 = fun.apply(str);
return str2;
}
}
使用方法引用的前提: 接口重写的方法中只能调用一个方法
2.方法
对象::实例方法 : 接口中重写的方法的参数和返回值要跟调用的方法的参数和返回值相同
对象::实例方法 接口中的方法的返回值和参数类型和方法中调用的返回值类型和参数个数类型 一致,才可以使用方法引用
类::静态方法: 接口中重写的方法的参数和返回值要跟调用的方法的参数和返回值相同, 调用的方法是静态方法
类::静态方法 当接口中方法参数返回值和调用的方法的返回值接口个数类型一致,并且方法是静态的
类::实例方法: 接口中的方法 参数有两个, 一个参数调用者, 一个参数是 调用方法的参数, 返回值和 调用方法的返回值相同的
类::new 接口中只做创建对象的操作, 供给型接口
接口的返回值和调用方法返回值要一致,接口中方法有两个参数, 调用的方法有一个参数, 接口中两个参数,一个作为方法的调用者,一个座位方法的参数的这种情况可以使用方法引用
public class Employee {
private String name;
private int age;
private double salary;
private String gender;
public Employee(String name, int age, double salary, String gender) {
super();
this.name = name;
this.age = age;
this.salary = salary;
this.gender = gender;
}
public Employee() {
super();
// TODO Auto-generated constructor stub
}
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 double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Employee [name=" + name + ", age=" + age + ", salary=" + salary + ", gender=" + gender + "]";
}
}
public class Test {
public static void main(String[] args) {
/*
* Supplier<Integer> supplier = new Supplier<Integer>() {
*
* @Override public Integer get() { // TODO Auto-generated method stub return
* (int)Math.random(); }
*
* };
*/
Consumer<String> consumer = new Consumer<String>() {
@Override
public void accept(String t) {
System.out.println(t);
}
};
Consumer<String> consumer2 = t -> System.out.println(t);
Consumer<String> consumer3 = System.out::println;
Employee employee = new Employee("zhangsan", 5, 1000000, "位置");
/*
* Supplier<String> supplier = new Supplier<String>() {
*
* @Override public String get() {
*
* return employee.getName(); } };
*/
show(() -> employee.getName());
// 实例::实例方法
show(employee::getName);
// 类::静态方法
TreeSet<Integer> set = new TreeSet<>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return Integer.compare(o1, o2);
}
});
TreeSet<Integer> set2 = new TreeSet<>((o1, o2) -> Integer.compare(o1, o2));
类::静态方法 当接口中方法参数返回值和调用的方法的返回值接口个数类型一致,并且方法是静态的
TreeSet<Integer> set3 = new TreeSet<>(Integer::compare);
// 类::实例方法
// 想要判断两个字符串内容是否相同
/*
* BiPredicate<String, String> biPredicate = new BiPredicate<String, String>() {
*
* @Override public boolean test(String t, String u) {
*
* return t.equals(u); } };
*/
// tes("hehehe", "hehehe", biPredicate);
tes("a", "a", (t, u) -> t.equals(u));
tes("b", "b", String::equals);// 类::实例方法
// 类::new
Supplier<Employee> supplier = new Supplier<Employee>() {
@Override
public Employee get() {
return new Employee();
}
};
Supplier<Employee> supplier2 = () -> new Employee();
// 类::new 当接口中 真是创建一个 对象
Supplier<Employee> supplier3 = Employee::new;
}
public static void tes(String str1, String str2, BiPredicate<String, String> biPredicate) {
boolean t = biPredicate.test(str1, str2);
System.out.println(t);
}
// 通过supplier 调用get方法
public static void show(Supplier<String> supplier) {
System.out.println(supplier.get());
}
}
3.Stream
public class Test2 {
public static void main(String[] args) {
// 1. Stream.of(1
Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5);
stream.forEach(System.out::println);// 进行遍历
// 2. 利用数组得到一个Stream对象
Integer[] arr = { 1, 2, 3, 8, 9, 0 };
Stream<Integer> stream2 = Arrays.stream(arr);
stream2.forEach(System.out::println);
// 3. 集合 集合.stream()
ArrayList<String> list = new ArrayList<>();
list.add("aaaa");
list.add("bbb");
list.add("cc");
list.add("ddddd");
Stream<String> stream3 = list.stream();
// 4. 无限流(没有限制,无限得到)
// 有参数有返回值
Stream<Integer> stream4 = Stream.iterate(0, x -> x + 2);
stream4.limit(5).forEach(System.out::println);
// 5.generate 生成
Stream<Integer> stream5 = Stream.generate(() -> (int) (Math.random() * 100));
stream5.limit(7).forEach(System.out::println);
}
}
4.中间操作
public class Test3 {
public static void main(String[] args) {
ArrayList<Employee> list = new ArrayList<>();
list.add(new Employee("zhangsan", 5, 5000, "男"));
list.add(new Employee("lsi", 15, 3000, "男"));
list.add(new Employee("wangwu", 3, 50000, "男"));
Employee employee = new Employee("zhaoliu", 12, 600, "男");
list.add(employee);
list.add(employee);
// 1. 先获取stream对象
Stream<Employee> stream = list.stream();
//stream.forEach(System.out::println);
System.out.println("=========中间操作filter过滤====");
list.stream().filter(t->t.getSalary()>3000).forEach(System.out::println);
System.out.println("=========limit-截断=======");
list.stream().limit(2).forEach(System.out::println);
System.out.println("========作filter==limit-=======");
list.stream().filter(t->t.getSalary()>3000).limit(1).forEach(System.out::println);
System.out.println("========-skip==跳过===");
list.stream().filter(t->t.getSalary()>3000).skip(1).limit(1).forEach(System.out::println);
System.out.println("========-distinct==去除重复元素===");
list.stream().distinct().forEach(System.out::println);
System.out.println("=======map 映射=========");
// 吧集合中employee对象的名字取出来
/*Function<Employee, String> function = new Function<Employee, String>() {
@Override
public String apply(Employee t) {
// TODO Auto-generated method stub
return t.getName();
}
};*/
// 获取集合中所有员工的名字
list.stream().map(e->e.getName()).forEach(System.out::println);
// 利用stream 获取集合中员工的年龄, 并且去重复,排序,打印
list.stream().map(e->e.getAge()).distinct().sorted().forEach(System.out::println);
System.out.println("======sorted 排序=============");
// Employee排序
/*Comparator<Employee> comparator = new Comparator<Employee>() {
@Override
public int compare(Employee o1, Employee o2) {
return Double.compare(o2.getSalary(), o1.getSalary());
}
};
*/
list.stream().sorted((o1,o2)->Double.compare(o2.getSalary(), o1.getSalary())).forEach(System.out::println);
list.stream().map(e->e.getSalary()).sorted().forEach(System.out::println);
}
}
5.终止操作
allMatch――检查是否匹配所有元素
anyMatch――检查是否至少匹配一个元素
noneMatch――检查是否没有匹配的元素
findFirst――返回第一个元素
findAny――返回当前流中的任意元素
count――返回流中元素的总个数
max――返回流中最大值
min――返回流中最小值
public class Test4 {
public static void main(String[] args) {
ArrayList<Employee> list = new ArrayList<>();
list.add(new Employee("zhangsan", 5, 5000, "男"));
list.add(new Employee("lsi", 15, 3000, "男"));
list.add(new Employee("wangwu", 3, 50000, "男"));
Employee employee = new Employee("zhaoliu", 12, 600, "女");
list.add(employee);
list.add(employee);
System.out.println("========allMatch――检查是否匹配所有元素========");
// 判断所有员工性别是否都为男
boolean match = list.stream().allMatch(e->e.getGender().equals("男"));
System.out.println("员工性别是否都为男:"+match);
System.out.println("========anyMatch――检查是否至少匹配一个元素========");
boolean anyMatch = list.stream().anyMatch(e->e.getGender().equals("男"));
System.out.println("员工性别是否至少有一个为男"+anyMatch);
System.out.println("========noneMatch――检查是否没有匹配的元素========");
boolean noneMatch = list.stream().noneMatch(e->e.getGender().equals("男"));
System.out.println("集合中是否所有员工都不为男"+noneMatch);
System.out.println("========findFirst――返回第一个元素========");
Optional<Employee> findFirst = list.stream().findFirst();
System.out.println(findFirst.get());
System.out.println("========findAny――返回任意一个========");
Optional<Employee> findAny = list.stream().findAny();
System.out.println(findAny.get());
System.out.println("========count――返回流中元素的总个数========");
long count = list.stream().filter(e->e.getSalary()>3000).count();
System.out.println(count);
//max――返回流中最大值
//min――返回流中最小值
System.out.println("========max――返回流中最大值========");
Optional<Employee> max = list.stream().max((o1,o2)->Double.compare(o1.getAge(), o2.getAge()));
System.out.println(max.get());
System.out.println("========min――返回流中最小值========");
Optional<Employee> min = list.stream().min((o1,o2)->Double.compare(o1.getAge(), o2.getAge()));
System.out.println(min.get());
}
}
6.规约,收集
public class Test5 {
public static void main(String[] args) {
ArrayList<Employee> list = new ArrayList<>();
list.add(new Employee("zhangsan", 5, 5000, "男"));
list.add(new Employee("lsi", 15, 3000, "男"));
list.add(new Employee("wangwu", 3, 50000, "男"));
Employee employee = new Employee("zhaoliu", 12, 600, "女");
list.add(employee);
list.add(employee);
// 归约 第二个需求 将所有的工资 都求和
Optional<Double> reduce = list.stream().map(e->e.getSalary()).reduce((t,u)->t+u);
System.out.println(reduce.get());
List<Integer> list2 = new ArrayList<>();
list2.add(3);
list2.add(13);
list2.add(23);
list2.add(33);
// 归约
Optional<Integer> reduce2 = list2.stream().reduce((t,u)->t+u);
System.out.println(reduce2.get());
// 收集
List<String> list3 = list.stream().map(e->e.getName()).collect(Collectors.toList());
list3.forEach(System.out::println);
Set<String> set = list.stream().map(e->e.getName()).collect(Collectors.toSet());
set.forEach(System.out::println);
}
}
7.jdbc
jar包:java—>编译—>.class—>运行的是字节码文件
从数据库中查询数据
1.加载驱动
2.建立连接
3.sql语句
4. 创建statment对象 执行sql语句的对象
5. 执行查询操作
6. ResultSet 从resultset中获取数据,遍历集合
7. 关闭
public class Emp {
private int empno;
private String ename;
private String job;
private int mgr;
private String hiredate;
private double sal;
private double comm;
private int deptno;
public Emp(int empno, String ename, String job, int mgr, String hiredate, double sal, double comm, int deptno) {
super();
this.empno = empno;
this.ename = ename;
this.job = job;
this.mgr = mgr;
this.hiredate = hiredate;
this.sal = sal;
this.comm = comm;
this.deptno = deptno;
}
public Emp() {
super();
// TODO Auto-generated constructor stub
}
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public int getMgr() {
return mgr;
}
public void setMgr(int mgr) {
this.mgr = mgr;
}
public String getHiredate() {
return hiredate;
}
public void setHiredate(String hiredate) {
this.hiredate = hiredate;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}
public double getComm() {
return comm;
}
public void setComm(double comm) {
this.comm = comm;
}
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
@Override
public String toString() {
return "Emp [empno=" + empno + ", ename=" + ename + ", job=" + job + ", mgr=" + mgr + ", hiredate=" + hiredate
+ ", sal=" + sal + ", comm=" + comm + ", deptno=" + deptno + "]";
}
}
public class Test {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//1.加载驱动
Class.forName("oracle.jdbc.OracleDriver");
//2.建立连接
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "SCOTT", "tiger");
//3.sql
String sql="select * from emp";
//4. 创建statment对象 执行sql语句的对象
Statement statement = conn.createStatement();
//5. 执行查询操作
ResultSet rs = statement.executeQuery(sql);
//6. ResultSet 从resultset中获取数据
ArrayList<Emp> list = new ArrayList<>();
// 先判断是否有下一个元素, 如果有, 则获取,如果没有循环结束
while(rs.next()){// 判断是否有下一个元素
//next()判断是否有下一条元素
// 获取每一列数据
int empno = rs.getInt("empno");
String ename = rs.getString("ename");
String job = rs.getString("job");
int mgr = rs.getInt("mgr");
String hiredate = rs.getString("hiredate");
double sal = rs.getDouble("sal");
double comm = rs.getDouble("comm");
int deptno = rs.getInt("deptno");
Emp emp = new Emp(empno, ename, job, mgr, hiredate, sal, comm, deptno);
list.add(emp);
}
// 遍历集合
list.forEach(System.out::println);
//7. 关闭
rs.close();
statement.close();
conn.close();
}
}