Big Data Big Data learning self-study course course of two good programmers
lambda expressions
Compare lambda expressions and anonymous inner classes:
lambda expression is simply an anonymous inner classes
Anonymous inner classes can create an instance for any interface, no matter how abstract methods interfaces;
lambda expression can only create an instance of a function interface (i.e., there is only an abstract method)
The basic syntax:
( Parameters ) -> expression, ( parameter ) -> { method body ;}
1. parameter: the parameter list allows omitting the parameter type parameter list if only one parameter, the parameter list in parentheses may be omitted in the code
2. Arrow: by scribing number is greater than the English symbols
3. code block: If the code block contains only one statement, the lambda expressions allow code blocks are omitted braces
4. Return value: the lambda block only one return statement even be omitted return keywords
lambda expression return value is needed, and it is only a block of code is omitted return statement, lambda expression automatically returns the statement of results
5.lambda use the expression variable: If global variables directly, if it is a local variable will default to the front final
```java
// anonymous inner classes
InterfaceA a = new InterfaceA() {
public void show() {
System.out.println ( " anonymous inner classes show");
}
};
a.show();
// by lambda expressions
InterfaceA b = ()->{
System.out.println ( "lambda expressions show");
};
b.show();
// simplified
InterfaceA b1 = () -> System.out.println ( " simplified lambda expressions show");
b1.show();
```
And references cited constructor method
1. referenced class method
2. The reference example of a method of a particular object
3. The reference method of a class object instance
4. The reference constructor
```java
// 1. No parameters
InterA aa = ()->System.out.println("没有参数");
//2.一个参数的
InterB bb = (ib)->System.out.println("一个参数:"+ib);
//3.两个参数的
InterC cc = (ia,ib)->System.out.println("两个参数:"+"ia:"+ia+"ib:"+ib);
//4.返回值
InterD dd = (i,j)->i+j;
int value1 = dd.showD(4, 7);
//5.lambda表达式作为参数
```
```java
public class Demo3 {
public static void main(String[] args) {
//
//
//
//
}
}
//* 1.引用类方法
interface Converter{
//将字符串转换成整数
Integer convert(String value);
}
class Test1{
public static void fun1() {
}
}
//* 2.引用特定对象的实例方法
interface IA{
public void show(String message);
}
class A{
public void play(String i) {
}
}
class Test2{
public static void fun2() {
}
}
//* 3.引用某类对象的实例方法
interface IB{
String subString(String string,int stat,int end);
}
class Test3{
public static void fun3() {
}
}
//* 4.引用构造方法
interface IC{
Object show(String name,int age);
}
class Person{
String name;
int age;
public Person(String name, int age) {
}
@Override
public String toString() {
}
}
class Test4{
public static void fun4() {
}
}
```
集合
可以存储不同类型的多个数据,只能存储引用数据类型,动态分配内存
Collection:接口
添加:boolean add(Object o);、boolean addAll(Collection<?> c);
删除:boolean remove(Object o);、boolean removeAll(Collection<?> c);、
判断:boolean contains(Object o);、boolean contains(Collection<?> c);
获取:Iterator<E> iterator();
集合变数组:Object toArray();
List:接口:
有序的(存储有序),可重复的
底层数据结构是数组,线程不安全,特点:查找速度快,添加删除慢
底层数据结构是数组,线程安全,特点:查找速度快,添加删除慢
底层数据结构是链表,线程不安全,特点:查找速度慢,添加删除快
Set:接口:
无序的,不可重复的
底层是哈希表,线程不安全
底层是二叉树,线程不安全
Map:接口
HashMap:DAY07笔记2019-07-30
TreeMap:DAY07笔记2019-07-30
泛型
通过<数据类型>接受一种数据类型
1.减少容错处理,简化代码
2.将运行错误提前到编译阶段
泛型应用在方法上:
1.类上的泛型与方法上的泛型保持一致
```java
class Dog<F>{
public void eat(F f) {
}
}
```
2.方法上独立使用泛型
```java
public <E> void song(E e) {
}
```
3.静态方法上使用泛型(必须独立使用):
```java
public static <W> void show(W w) {}
```
泛型应用在接口上:
1.子类上的泛型与接口上的一致
```java
interface Inte<E>{
public void show(E e);
}
class Pig<E> implements Inte<E>{
@Override
public void show(E e) {
}
}
```
2.接口上使用泛型,子类上不用泛型
```java
interface Inte<E>{
public void show(E e);
}
class Bird implements Inte<String>{
public void show(String e) {};
}
```
1.如果是重写的方法,泛型与接口一致
2.如果是子类自己的方法,可以与接口一致,也可以有自己的泛型
限制上限和限制下限
限制上限<? extends E>:E以及E的子类
限制上限<? super E>:E以及E的父类