Big Data learning path shares Lambda expressions, a simple anonymous inner class (but only for interfaces with only one abstract method –> functional interface)

– for example,

– Basic application without reference

“`java

interface InterfaceA{

public void show();

}

public class Demo1 {

public static void main(String[] args) {

// Anonymous inner class

InterfaceA a = new InterfaceA() {

public void show() {

System.out.println(” show for anonymous inner classes “);

}

};

a.show();

// Through lambda expressions

InterfaceA b = ()->{

System.out.println(“lambda expression show”);

};

b.show();

/ / to simplify

InterfaceA bb = ()-> system.out.println (” simplified lambda expression show”);

bb.show();

}

}

` ` `

– Basic application with parameters

“`java

interface A{

public int add(int i,int j);

}

class B{

public int bAdd(int i,int j){

return i+j;

}

}

public class Lambda {

public static void main(String[] args) {

Lambda expressions require a return value, and its code block contains only one statement that omits return

Lambda expressions automatically return the result of this statement

//A a=(i,j)->{return i+j; };

A a=(i,j)->i+j; // Basic usage, which is similar to anonymous inner classes

System. The out. Println (a.a dd (4, 5));

}

}

interface A{

public int add(int i,int j);

}

class B{

public int bAdd(int i,int j){

return i+j;

}

}

public class Lambda {

public static void main(String[] args) {

A a=new B().bAdd(); // think of C# delegate

System. The out. Println (a.a dd (4, 5));

}

}

interface A{

public int add(int i,int j);

}

class B{

/ / static

public static int bAdd(int i,int j){

return i+j;

}

}

public class Lambda {

public static void main(String[] args) {

// The call method is different

A a=B::bAdd; // think of C# delegate

System. The out. Println (a.a dd (4, 5));

}

}

` ` `

– Reference class methods

“`java

interface Converter{

// Convert a string to an integer

Integer convert(String value);

}

class Test1{

public static void fun1() {

// The original method

Converter converter = value->Integer.valueOf(value);

Integer v1 = converter.convert(“222”);

System.out.println(v1);

/ / to simplify

// Reference class methods

// Implemented with ::, which automatically passes all arguments to the current method of the lambda expression

Converter converter2 = Integer::valueOf;

Integer v2 = converter2.convert(“333”);

System.out.println(v2);

}

}

` ` `

– References an instance method of a particular object

“`java

interface IA{

public void show(String message);

}

class A{

public void play(String i) {

System.out.println(” here is A method play”+” I :”+ I);

}

}

class Test2{

public static void fun2() {

/ / the original

IA ia = message->new A().play(message);

ia.show(“hello”);

/ / to simplify

IA ia2 = new A()::play;

ia2.show(“world”);

}

}

` ` `

– Reference constructor

“`java

interface IC{

Object show(String name,int age);

}

class Person{

String name;

int age;

public Person(String name, int age) {

super();

this.name = name;

this.age = age;

}

@Override

public String toString() {

return “Person [name=” + name + “, age=” + age + “]”;

}

}

class Test4{

public static void fun4() {

IC ic = (name,age)->new Person(name, age);

Object per = ic.show(“bing”, 19);

System.out.println(per);

/ / to simplify

// Instead of lambda expressions, constructor references are passed as arguments to all implemented methods in functional interfaces

IC ic2 = Person::new;

Object per1 = ic2.show(“chen”, 10);

System.out.println(per1);

}

}

` ` `

Application –

“`java

String[] strings={“123″,”456″,”789″,” 789″};

List<String> lists= Arrays.asList(strings);

Lists. ForEach ((s)-> out.println(” string is :”+s));

lists.forEach(out::println);

` ` `

# # collection

“`java

package collection;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.Objects;

class Person{

private String name;

private int age;

public Person(String name, int age) {

this.name = name;

this.age = age;

}

@Override

public boolean equals(Object o) {

if (this == o) return true;

if (o == null || getClass() ! = o.getClass()) return false;

Person person = (Person) o;

return age == person.age &&

Objects.equals(name, person.name);

}

public void setName(String name) {

this.name = name;

}

public void setAge(int age) {

this.age = age;

}

@Override

public String toString() {

return “Person{” +

“name='” + name + ‘\” +

“, age=” + age +

‘} ‘;

}

}

public class Main {

public static void main(String[] args) {

ArrayList<Person> arrayList=new ArrayList<>();

Person zs=new Person(” zS “,20);

Person zs2=new Person(” Person “,20); // Will not fit

Person ls=new Person(” Person “,30);

Person ww=new Person(” ww “,40);

//contains Compares objects to equals.

if(! arrayList.contains(zs)){

arrayList.add(zs);

}

if(! arrayList.contains(zs2)){

arrayList.add(zs2);

}

if(! arrayList.contains(ls)){

arrayList.add(ls);

}

if(! arrayList.contains(ww)){

arrayList.add(ww);

}

Iterator iterator=arrayList.iterator();

while(iterator.hasNext()){

System.out.println(iterator.next());

}

}

}

` ` `

Using hash related containers is to override equals HashCode

TreeSet also implements the Comparable interface and overwrites the compareTo method. Or customize the Comparator Comparator

“`java

public class Demo9 {

public static void main(String[] args) {

// Create a comparator object

ComStrWithLength comStrWithLength = new ComStrWithLength();

// Pass the comparator object to TreeSet

Set set = new TreeSet<>(comStrWithLength);

/ *

* TreeSet’s Add method implements sorting, de-duplication. By calling the element’s compareTo method

* The String class already implements the Comparable interface

* /

set.add(“java”);

set.add(“hadoop”);

set.add(“spark”);

set.add(“HDFS”);

set.add(“HDFS”);

set.add(“Mapreduce”);

System.out.println(set);

}

}

// Create a comparator class

class ComStrWithLength implements Comparator{

@Override

public int compare(Object o1, Object o2) {

// Compare the length of strings

if (! (o1 instanceof String)) {

Throw new ClassCastException(” type conversion error “);

}

if (! (o2 instanceof String)) {

Throw new ClassCastException(” type conversion error “);

}

// Downward transition

String s1 = (String)o1;

String s2 = (String)o2;

// Start with the length ratio

int num = s1.length()-s2.length();

// The length is the same

return num==0? s1.compareTo(s2):num;

}

}

The generic

Function:

1. Generics no longer need to be fault-tolerant, downward transformation, cast —- simplify code

2. Check the problems in the running stage in advance to the compilation stage, which improves the code security and programming efficiency