今日目标
Lambda表达式 Stream流 File类 递归
1 Lambda表达式
1.1 体验Lambda表达式
package com. lystar. lambda_demo ;
public class LambdaDemo {
public static void main ( String [ ] args) {
goSwimming ( new Swimming ( ) {
@Override
public void swim ( ) {
System . out. println ( "铁汁 , 我们去游泳吧...." ) ;
}
} ) ;
goSwimming ( ( ) -> System . out. println ( "铁汁 , 我们去游泳吧...." ) ) ;
}
public static void goSwimming ( Swimming swimming) {
swimming. swim ( ) ;
}
}
interface Swimming {
public abstract void swim ( ) ;
}
lambda表达式可以理解为对匿名内部类的一种简化 , 但是本质是有区别的 面向对象思想 :
函数式编程思想 :
1.2 函数式接口
只有一个抽象方法需要重写的接口,函数式接口。函数式接口是允许有其他的非抽象方法的存在例如静态方法,默认方法,私有方法。 为了标识接口是一个函数式接口,可以在接口之上加上一个注解: @FunctionalInterface 以示区别 在JDK中 java.util.function 包中的所有接口都是函数式接口。我们之前学习线程时学习的Runnable也是函数式接口
1.3 Lambda表达式的使用
使用前提
格式 : ( 形式参数 ) -> { 代码块 }
形式参数:如果有多个参数,参数之间用逗号隔开;如果没有参数,留空即可 ->:由英文中画线和大于符号组成,固定写法。代表指向动作 代码块:是我们具体要做的事情,也就是以前我们写的方法体内容
1.4 Lambda表达式的案例
package com. lystar. lambda_test ;
public class LambdaTest1 {
public static void main ( String [ ] args) {
useShowHandler ( ( ) -> {
System . out. println ( "我是一个lambda表达式...." ) ;
} ) ;
}
public static void useShowHandler ( ShowHandler showHandler) {
showHandler. show ( ) ;
}
}
interface ShowHandler {
public abstract void show ( ) ;
}
package com. lystar. lambda_test ;
public class LambdaTest2 {
public static void main ( String [ ] args) {
useStringHandler ( ( String msg) -> {
System . out. println ( msg) ;
} ) ;
}
public static void useStringHandler ( StringHandler stringHandler) {
stringHandler. printMessage ( "今天天气不错 , 挺风和日丽的..." ) ;
}
}
@FunctionalInterface
interface StringHandler {
public abstract void printMessage ( String msg) ;
}
package com. lystar. lambda_test ;
import java. util. Random ;
public class LambdaTest3 {
public static void main ( String [ ] args) {
useRandomNumHandler ( ( ) -> {
return new Random ( ) . nextInt ( 10 ) + 1 ;
} ) ;
}
public static void useRandomNumHandler ( RandomNumHandler randomNumHandler) {
int number = randomNumHandler. getNumber ( ) ;
System . out. println ( number) ;
}
}
interface RandomNumHandler {
public abstract int getNumber ( ) ;
}
package com. lystar. lambda_test ;
public class LambdaTest4 {
public static void main ( String [ ] args) {
useCalculator ( ( int a , int b) -> { return a + b; } ) ;
}
public static void useCalculator ( Calculator calculator) {
int calc = calculator. calc ( 10 , 20 ) ;
System . out. println ( calc) ;
}
}
@FunctionalInterface
interface Calculator {
public abstract int calc ( int a, int b) ;
}
2 Stream流
2.1 Stream的体验
package com. lystar. stream_demo ;
import java. util. ArrayList ;
public class StreamDemo {
public static void main ( String [ ] args) {
ArrayList < String > list = new ArrayList < > ( ) ;
list. add ( "张无忌" ) ;
list. add ( "张翠山" ) ;
list. add ( "张三丰" ) ;
list. add ( "谢广坤" ) ;
list. add ( "赵四" ) ;
list. add ( "刘能" ) ;
list. add ( "小沈阳" ) ;
list. add ( "张良" ) ;
ArrayList < String > list2 = new ArrayList < > ( ) ;
for ( String s : list) {
if ( s. startsWith ( "张" ) ) {
list2. add ( s) ;
}
}
ArrayList < String > list3 = new ArrayList < > ( ) ;
for ( String s : list2) {
if ( s. length ( ) == 3 ) {
list3. add ( s) ;
}
}
for ( String s : list3) {
System . out. println ( s) ;
}
System . out. println ( "================================" ) ;
list. stream ( ) . filter ( s -> s. startsWith ( "张" ) && s. length ( ) == 3 ) . forEach ( s -> System . out. println ( s) ) ;
}
}
2.2 Stream流的三类方法
获取Stream流
中间方法
流水线上的操作。 一次操作完毕之后,还可以继续进行其他操作 终结方法
一个Stream流只能有一个终结方法 是流水线上的最后一个操作
2.3 第一类 : 获取方法
package com. lystar. stream_demo ;
import java. util. ArrayList ;
import java. util. Arrays ;
import java. util. HashMap ;
public class StreamDemo2 {
public static void main ( String [ ] args) {
}
private static void method3 ( ) {
int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 } ;
Arrays . stream ( arr) . forEach ( s -> System . out. println ( s) ) ;
}
private static void method2 ( ) {
HashMap < String , String > hm = new HashMap < > ( ) ;
hm. put ( "it001" , "曹植" ) ;
hm. put ( "it002" , "曹丕" ) ;
hm. put ( "it003" , "曹熊" ) ;
hm. put ( "it004" , "曹冲" ) ;
hm. put ( "it005" , "曹昂" ) ;
hm. keySet ( ) . stream ( ) . forEach ( s -> System . out. println ( s) ) ;
hm. entrySet ( ) . stream ( ) . forEach ( s -> System . out. println ( s) ) ;
}
private static void method1 ( ) {
ArrayList < String > list = new ArrayList < > ( ) ;
list. add ( "迪丽热巴" ) ;
list. add ( "古力娜扎" ) ;
list. add ( "马尔扎哈" ) ;
list. add ( "欧阳娜娜" ) ;
list. stream ( ) . forEach ( s -> System . out. println ( s) ) ;
}
}
2.4 第二类 : 中间方法
package com. lystar. stream_demo ;
import java. util. ArrayList ;
import java. util. Comparator ;
import java. util. function. Predicate ;
import java. util. stream. Stream ;
public class StreamDemo3 {
public static void main ( String [ ] args) {
ArrayList < String > list = new ArrayList < > ( ) ;
list. add ( "张无忌" ) ;
list. add ( "张翠山" ) ;
list. add ( "张三丰" ) ;
list. add ( "谢广坤" ) ;
list. add ( "赵四" ) ;
list. add ( "刘能" ) ;
list. add ( "小沈阳" ) ;
list. add ( "张良" ) ;
list. add ( "张良" ) ;
list. add ( "张良" ) ;
list. add ( "张良" ) ;
}
private static void method3 ( ArrayList < String > list) {
list. stream ( ) . sorted ( new Comparator < String > ( ) {
@Override
public int compare ( String o1, String o2) {
return o1. length ( ) - o2. length ( ) ;
}
} ) . forEach ( s-> {
System . out. println ( s) ;
} ) ;
}
private static void method3 ( ) {
ArrayList < Integer > list2 = new ArrayList < > ( ) ;
list2. add ( 3 ) ;
list2. add ( 1 ) ;
list2. add ( 2 ) ;
list2. stream ( ) . sorted ( ) . forEach ( s-> {
System . out. println ( s) ;
} ) ;
}
private static void method2 ( ArrayList < String > list) {
ArrayList < String > list2 = new ArrayList < > ( ) ;
list2. add ( "迪丽热巴" ) ;
list2. add ( "古力娜扎" ) ;
list2. add ( "欧阳娜娜" ) ;
list2. add ( "马尔扎哈" ) ;
Stream . concat ( list. stream ( ) , list2. stream ( ) ) . forEach ( s -> {
System . out. println ( s) ;
} ) ;
}
private static void method1 ( ArrayList < String > list) {
list. stream ( ) . filter ( s ->
s. startsWith ( "张" )
) . forEach ( s -> System . out. println ( s) ) ;
}
}
2.5 第三类 : 终结方法
package com. lystar. stream_demo ;
import java. util. ArrayList ;
import java. util. function. Consumer ;
public class StreamDemo4 {
public static void main ( String [ ] args) {
ArrayList < String > list = new ArrayList < > ( ) ;
list. add ( "张无忌" ) ;
list. add ( "张翠山" ) ;
list. add ( "张三丰" ) ;
list. add ( "谢广坤" ) ;
long count = list. stream ( ) . count ( ) ;
System . out. println ( count) ;
}
private static void method1 ( ArrayList < String > list) {
list. stream ( ) . forEach ( new Consumer < String > ( ) {
@Override
public void accept ( String s) {
System . out. println ( s) ;
}
} ) ;
System . out. println ( "=====================" ) ;
list. stream ( ) . forEach ( ( String s) -> {
System . out. println ( s) ;
} ) ;
System . out. println ( "=====================" ) ;
list. stream ( ) . forEach ( s -> { System . out. println ( s) ; } ) ;
}
}
2.6 Stream流中的收集方法
package com. lystar. stream_demo ;
import java. util. ArrayList ;
public class StreamDemo5 {
public static void main ( String [ ] args) {
ArrayList < Integer > list = new ArrayList < > ( ) ;
for ( int i = 1 ; i <= 10 ; i++ ) {
list. add ( i) ;
}
list. stream ( ) . filter ( num -> num % 2 == 0 ) . forEach ( num -> System . out. println ( num) ) ;
System . out. println ( "=============" ) ;
System . out. println ( list) ;
}
}
结论:在Stream流中无法直接修改集合,数组中的数据
package com. lystar. stream_demo ;
import java. util. ArrayList ;
import java. util. List ;
import java. util. Set ;
import java. util. stream. Collectors ;
public class StreamDemo6 {
public static void main ( String [ ] args) {
ArrayList < Integer > list = new ArrayList < > ( ) ;
for ( int i = 1 ; i <= 10 ; i++ ) {
list. add ( i) ;
}
list. add ( 10 ) ;
list. add ( 10 ) ;
list. add ( 10 ) ;
list. add ( 10 ) ;
list. add ( 10 ) ;
List < Integer > list2 = list. stream ( ) . filter ( num -> num % 2 == 0 ) . collect ( Collectors . toList ( ) ) ;
System . out. println ( list2) ;
Set < Integer > set = list. stream ( ) . filter ( num -> num % 2 == 0 ) . collect ( Collectors . toSet ( ) ) ;
System . out. println ( set) ;
}
}
package com. lystar. stream_demo ;
import java. util. ArrayList ;
import java. util. Map ;
import java. util. stream. Collectors ;
public class StreamDemo7 {
public static void main ( String [ ] args) {
ArrayList < String > list = new ArrayList < > ( ) ;
list. add ( "zhangsan,23" ) ;
list. add ( "lisi,24" ) ;
list. add ( "wangwu,25" ) ;
Map < String , String > map = list. stream ( ) . filter ( s -> Integer . parseInt ( s. split ( "," ) [ 1 ] ) > 23 ) . collect (
Collectors . toMap (
( String s) -> {
return s. split ( "," ) [ 0 ] ;
}
,
( String s) -> {
return s. split ( "," ) [ 1 ] ;
}
)
) ;
System . out. println ( map) ;
}
}
3 File类
3.1 File类的介绍
java.io.File 类是文件和目录路径名的抽象表示,主要用于文件和目录的创建、查找和删除等操作
3.2 构造方法
package com. lystar. file_demo ;
import java. io. File ;
public class FileDemo1 {
public static void main ( String [ ] args) {
File f1 = new File ( "D:\\abc.txt" ) ;
System . out. println ( f1) ;
File f2 = new File ( "D:\\aaa" , "bbb.txt" ) ;
System . out. println ( f2) ;
File f3 = new File ( new File ( "D:\\aaa" ) , "bbb.txt" ) ;
System . out. println ( f3) ;
}
}
3.3 File类的创建功能
package com. lystar. file_demo ;
import java. io. File ;
import java. io. IOException ;
public class FileDemo2 {
public static void main ( String [ ] args) throws IOException {
File f1 = new File ( "D:\\a.txt" ) ;
System . out. println ( f1. createNewFile ( ) ) ;
File f2 = new File ( "day10_demo\\aaa" ) ;
System . out. println ( f2. mkdir ( ) ) ;
File f3 = new File ( "day10_demo\\bbb\\ccc" ) ;
System . out. println ( f3. mkdirs ( ) ) ;
}
}
3.4 File类的删除功能
package com. lystar. file_demo ;
import java. io. File ;
import java. io. IOException ;
public class FileDemo3 {
public static void main ( String [ ] args) throws IOException {
File f1 = new File ( "D:\\a.txt" ) ;
System . out. println ( f1. delete ( ) ) ;
File f2 = new File ( "day10_demo\\aaa" ) ;
System . out. println ( f2. delete ( ) ) ;
File f3 = new File ( "day10_demo\\bbb" ) ;
System . out. println ( f3. delete ( ) ) ;
}
}
3.5 File类的判断和获取功能
package com. lystar. file_demo ;
import java. io. File ;
public class FileDemo4 {
public static void main ( String [ ] args) {
File f1 = new File ( "day10_demo\\aaa" ) ;
File f2 = new File ( "day10_demo\\a.txt" ) ;
System . out. println ( f1. isDirectory ( ) ) ;
System . out. println ( f2. isDirectory ( ) ) ;
System . out. println ( f1. isFile ( ) ) ;
System . out. println ( f2. isFile ( ) ) ;
System . out. println ( f1. exists ( ) ) ;
System . out. println ( f1. getAbsolutePath ( ) ) ;
System . out. println ( f1. getPath ( ) ) ;
System . out. println ( f2. getName ( ) ) ;
}
}
3.6 File类高级获取功能
package com. lystar. file_demo ;
import java. io. File ;
public class FileDemo5 {
public static void main ( String [ ] args) {
File file = new File ( "day10_demo\\bbb\\ccc" ) ;
File [ ] files = file. listFiles ( ) ;
for ( File f : files) {
System . out. println ( f. getName ( ) ) ;
}
}
}
练习 :
package com. lystar. file_demo ;
import java. io. File ;
import java. util. HashMap ;
public class FileTest2 {
public static void main ( String [ ] args) {
File file = new File ( "day10_demo\\统计文件个数文件夹" ) ;
getFileCount ( file) ;
}
public static void getFileCount ( File f) {
HashMap < String , Integer > hm = new HashMap < > ( ) ;
if ( f. isDirectory ( ) ) {
File [ ] files = f. listFiles ( ) ;
for ( File file : files) {
String fileName = file. getName ( ) ;
String name = fileName. split ( "\\." ) [ 1 ] ;
if ( hm. containsKey ( name) ) {
hm. put ( name, hm. get ( name) ) ;
} else {
hm. put ( name, 1 ) ;
}
}
}
System . out. println ( hm) ;
}
}
4 递归
概述
递归概述:以编程的角度来看,递归指的是方法定义中调用方法本身的现象 好处
把一个复杂的问题层层转化为一个与原问题相似的规模较小的问题来求解 小的问题解决 , 大的问题也会逐一进行解决 注意
递归出口:否则会出现内存溢出 递归规则:需要找到与原问题相似的规模较小的问题 案例
package com. lystar. recursion_demo ;
import java. io. File ;
public class Demo2 {
public static void main ( String [ ] args) {
File f = new File ( "D:\\aaa" ) ;
deleteFiles ( f) ;
}
private static void deleteFiles ( File f) {
File [ ] files = f. listFiles ( ) ;
for ( File file : files) {
if ( file. isDirectory ( ) ) {
deleteFiles ( file) ;
} else {
file. delete ( ) ;
}
}
f. delete ( ) ;
}
}
rivate static int jc ( int n) {
if ( n == 1 ) {
return 1 ;
}
return n * jc ( n - 1 ) ;
}
}
package com. lystar. recursion_demo ;
import java. io. File ;
public class Demo2 {
public static void main ( String [ ] args) {
File f = new File ( "D:\\aaa" ) ;
deleteFiles ( f) ;
}
private static void deleteFiles ( File f) {
File [ ] files = f. listFiles ( ) ;
for ( File file : files) {
if ( file. isDirectory ( ) ) {
deleteFiles ( file) ;
} else {
file. delete ( ) ;
}
}
f. delete ( ) ;
}
}