2020-08-20

第十五次课
一.
package com.hpe.demo1;

/**

  • 动物接口
  • @author Administrator

*/
interface Animals {
// 信息展示方法 ,接口中,就可以提供一种实现。 就可以使用这种功能。
default void print() {
System.out.println(“动物”);
};

void run();
void shout();

}

// 狗接口
interface Dog{
void shout();
}

class LabuladuoDog implements Animals,Dog{

// @Override
// public void print() {
// System.out.println(“动物”);
// }

@Override
public void run() {
	// TODO Auto-generated method stub
	System.out.println("跑的慢");
}

@Override
public void shout() {
	// TODO Auto-generated method stub
	System.out.println("叽叽");
}

}

class LangGou implements Animals,Dog{

// @Override
// public void print() {
// System.out.println(“动物”);
// }

@Override
public void run() {
	// TODO Auto-generated method stub
	System.out.println("跑的快");
}

@Override
public void shout() {
	// TODO Auto-generated method stub
	System.out.println("汪汪");
}

}

public class Demo1{
public static void main(String[] args) {
LabuladuoDog l = new LabuladuoDog();
LangGou g = new LangGou();
l.print();
g.print();

}

}

另一个文件夹
一.
package com.hpe.demo2;

/**

  • 内部类 演示
  • @author Administrator

*/
public class Demo{

private String str = "外部类中的字符串";

static class Inner{
	private String inStr = "内部类中的字符串";
	public void print() {
		// 静态内部类无法调用 ,非静态的外部参数。
		//System.out.println(str);
		System.out.println("内部类打印");
	}
}

// 
public void fun() {
	// 在外部类 创建内部类对象
	Inner i = new Inner();
	i.print();
}

}

二.
package com.hpe.demo2;

import com.hpe.demo2.Demo.Inner;

/**

  • 内部类的 测试
  • @author Administrator

*/
public class Demo1 {
public static void main(String[] args) {
Demo d = new Demo();
d.fun();
// 需要导入包
Inner i = new Inner();
i.print();
}
}
三.
package com.hpe.demo2;

/**

  • 匿名内部类
  • @author Administrator

*/
public class Demo3 {
public static void main(String[] args) {
print();
}

public static void print() {
	// 实现Inter接口,定义一个类 。  匿名
	new Inter() {
		@Override
		public void test() {
			System.out.println("hhh");
		}
	}.test();
}
public static void test() {
	// 内部类,1---n的累加
	class XXx{
		public void sum(int n) {
			int count = 0;
			for (int i = 1; i <= n; i++) {
				count = count + i;
				System.out.println(count);
			}
		}
	}
	XXx x = new XXx();
	x.sum(100);
}

}

interface Inter{
void test();
}

// InterForImpl
class InterForImpl implements Inter{

@Override
public void test() {
	// TODO Auto-generated method stub
	
}}

另一个文件夹
一.
package com.hpe.demo3;

/**

  • lambda表达式 把代码变的更加简单。可读性比较差。 scala( spark ).
  • 1、简化匿名内部类的编写。
  • 2、 直接实现接口中的函数,
  • 3、函数名 (参数列表)
  • 4、函数实现用"->" 表示实现。{}表示实现的具体逻辑。
  • 5、用接口去声明使用。
  • 6、用声明的变量调用实现 的方法。
  • @author Administrator

*/

interface Inter{
int c();
}
interface Inter1{
int c(int x);
}

interface A{
int c(int a,int b);
}

interface B{
void b(String str);
}

public class Demo1 {

// 函数传递的形式。parm1=数值1  ,parm2=数值2 ,parm3=运算方式	
private static int test(int x,int y,A a) {
	return a.c(x,y);
}

public static void main(String[] args) {
	
	// 5、接收字符串对象,并在控制台打印。
	new B() {
		public void b(String s) {
			System.out.println(s);
		}
	}.b("hello");
	// () -》 {};  === () -> o
	B b5 = (String str) -> {System.out.println(str);};
	B b5_1 = (s) -> System.out.println(s);
	b5_1.b("hello");
	
	//3、lambda表达式 ;实现接口A。  (x,y) -》 { return  x - y};
	A d3 = (x,y) -> { return x - y;};
	d3.c(23, 20); // 没有声明具体类型
	
	//4、接收声明 类型参数
	A d4 = (int a,int b) -> { return a - b; };
	A d4_1 = (int a ,int b ) -> a - b;
	d4.c(1,2);
	d4_1.c(100, 70);
	A d4_2 = (a,b)-> a * b;
	A d4_3 = (a,b)-> a / b;
	A d4_4 = (a,b)-> a + b;
	
	// 6 ,传递接口,实现不同业务的访问。
	int s = test(1,2,d4_4);
	System.out.println(s);
	System.out.println(test(1,2,d4_1));
	System.out.println(test(1,2,d4_2));
	
	// 内部类:匿名
	new Inter() {
		@Override
		public int c() {
			return 6;
		}
	}.c();
	// lambda表达式:
	// 1、没有参数,直接返回。这样的函数   () -> 6;  参数列表  --》 语句块
	Inter d1 = () -> 6;
	d1.c();
	
	// 2\有参数的匿名内部类
	new Inter1() {
		@Override
		public int c(int x) {
			return x * 2;
		};
	}.c(100);
	
	// 接收一个参数  (类型) ---> x * 2;
	Inter1 d2 = (x) -> {return x * 2;};
	d2.c(100);
	
	
}

}
二.
package com.hpe.demo3;

import java.util.*;

/**

  • 方法的引用 ::,代码更加紧凑简洁。
  • @author Administrator

*/
public class Demo2 {
public static void main(String[] args) {
String[] strs = new String[] {“b”,“a”,“c”};
Arrays.asList(strs);

	List<String> list = new ArrayList<String>();
	list.add("a");
	list.add("b");
	//list.forEach((String b) -> System.out.print(b));
	//list.forEach(e -> System.out.println(e));
	// 方法引用
	list.forEach(System.out::println);
	
	
}

}
另一个文件夹。
一.
package com.hpe.demo4;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**

  • java高级特性 Stream流式处理 集合(把一个数据集中的元素 并行聚会处理)
  • a b c s b s c a a b c e s f b —> spit(" ") --> a a a a b b b c c --> a:4,b:3,a:3 —> a:100,b:89
  • stream(elements) —> filter(过滤spit)–> sorted() —> map() —> collect()
    // * elements流的来源: 集合 list set map;数组;内存(I/o通道)
  • steam的使用,就是实现一个filter-map-reduce过程。
  • @author Administrator

*/
public class Demo1 {

public static void main(String[] args) {
	List<Integer> list = Arrays.asList(100,500,200,600,700);
	// forEach迭代每个数据。
	// stream()--为list集合,去创建串行流
	Stream<Integer> stream = list.stream();
	// map 把信息拆解
	Stream<Integer> map = stream.map(new Function<Integer,Integer>() {
		@Override
		public Integer apply(Integer t) {
			
			return t / 2;
		}
	});
	// 遍历数据
	map.forEach(System.out::println);
	
	// 简化:
	Arrays.asList(100,500,200,600,700).stream().map((i) -> i / 2).forEach(System.out::println);
	
	//================================================================
	System.out.println("============================");
	Integer[] num = {1,2,3,4,5,6,7};
	// Stream.of();管道流     / 从1234567中取奇数偶数。 filter过滤方法()
	Stream.of(num).filter(n -> n%2==0).forEach(x -> System.out.print(x + " "));
	Stream.of(num).filter(n -> n%2!=0).forEach(x -> System.out.print(x + " "));
	// 取偶数,然后把取到的偶数放到一个数组中。 了解(方法引用)
	Integer[] ii = Stream.of(num).filter(n -> n%2==0).toArray(Integer[]::new);
	// 把奇数放到集合容器中 list中。   
	Stream.of(num).filter(n -> n%2!=0).collect(Collectors.toList());
	/*
	 * 过滤 转化的过程: 归并操作。将转化的数据流放到集合或集合元素中。 Collectors返回列表或字符串。
	 */
	//-=================================================s
	System.out.println("==========================================");
	/*
	 * reduce: 求和   求最大  求最小  聚会操作。
	 * 
	 */
	Integer[] nums = {1,2,3,4,5,6,7};
	int sum = Stream.of(nums).reduce(0,(a,b) -> a + b);
	
	Stream.of(nums).reduce(0, Integer::sum);
	System.out.println(sum);
	Stream.of(nums).reduce(0, Integer::max);
	
	//=======================================
	// [1]
	// [2,3]
	// [4,5,6]
	Stream<List<Integer>> inputStream = Stream.of(Arrays.asList(1),Arrays.asList(2,3),Arrays.asList(4,5,6));
	// flatMap扁平化处理      1, 2, 3,4,5 ,6
	Stream<Integer> outputStream = inputStream.flatMap(x -> x.stream());
	//[1,2,3,4,5,6]
	outputStream.forEach(x -> System.out.print(x + " "));
	/*
	 * Integer[] s = outputStream.toArray(Integer[]::new);
	 *  System.out.println(s);
	 */
	
	//========================= 字符串大小写转化
	// 把字符串转化成大小,并转化成一个list容器。“ ”隔开的每一个单词。
	String str = "a bc s f as afa fafas fa f e";
	
	// map
	List<String> list1 = Arrays.asList(str.split(" ")).stream()
			.map(String::toUpperCase)
			.collect(Collectors.toList());
	
	list1.forEach(System.out::println);
	
	
}




public void test(){
	// 回顾 ,lamdba应用;

// // 1\ List.forEach(System.out::println);
// 2 map
Map map = new HashMap();
map.put(“a”, “aa”);
map.forEach((k,v) -> System.out.println(v));
// 3
List list = new ArrayList();
Collections.sort(list,(s1,s2)->s1.compareTo(s2));

}

}在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值