hp实训8.19_Java基础之Java8新特性
default void print() {
System.out.println("动物");
};
void run();
void shout();
public class Demo{
private String str = "外部类中的字符串";
static class Inner{
private String inStr = "内部类中的字符串";
public void print() {
System.out.println("内部类打印");
}
}
public void fun() {
Inner i = new Inner();
i.print();
}
}
```java
```public class Demo1 {
public static void main(String[] args) {
Demo d = new Demo();
d.fun();
Inner i = new Inner();
i.print();
}
}
```java
public class Demo3 {
public static void main(String[] args) {
print();
}
public static void print() {
new Inter() {
@Override
public void test() {
System.out.println("hhh");
}
}.test();
}
public static void test() {
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();
}
class InterForImpl implements Inter{
@Override
public void test() {
}}
* **
* lambda表达式 把代码变的更加简单。可读性比较差。 scala( spark ).
* 1、简化匿名内部类的编写。
* 2、 直接实现接口中的函数,
* 3、函数名 (参数列表)
* 4、函数实现用"->" 表示实现。{}表示实现的具体逻辑。
* 5、用接口去声明使用。
*
* 6、用声明的变量调用实现 的方法。
```java
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);
}
}
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(System.out::println);
}
}
- 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过程。
public class Demo1 {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(100,500,200,600,700);
Stream<Integer> stream = list.stream();
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(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);
Stream.of(num).filter(n -> n%2!=0).collect(Collectors.toList());
System.out.println("==========================================");
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);
Stream<List<Integer>> inputStream = Stream.of(Arrays.asList(1),Arrays.asList(2,3),Arrays.asList(4,5,6));
Stream<Integer> outputStream = inputStream.flatMap(x -> x.stream());
outputStream.forEach(x -> System.out.print(x + " "));
String str = "a bc s f as afa fafas fa f e";
List<String> list1 = Arrays.asList(str.split(" ")).stream()
.map(String::toUpperCase)
.collect(Collectors.toList());
list1.forEach(System.out::println);
}
public void test(){
Map map = new HashMap();
map.put("a", "aa");
map.forEach((k,v) -> System.out.println(v));
List<String> list = new ArrayList<String>();
Collections.sort(list,(s1,s2)->s1.compareTo(s2));
}
}