Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Description and example usage of Lambda expression

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge of "the description of Lambda expression and the usage of examples". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Lambda expression, a simple anonymous inner class (but can only correspond to an interface with only one abstract method-> functional interface)

-for example

-basic application without parameters

```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 of anonymous inner classes")

}

}

A.show ()

/ / through lambda expression

InterfaceA b = ()-> {

System.out.println ("show of lambda expressions")

}

B.show ()

/ / simplify

InterfaceA bb = ()-> System.out.println ("show of simplified lambda expressions")

Bb.show ()

}

}

`

-basic application with parameters

```java

Interface A {

Public int add (int iMagin int j)

}

Class B {

Public int bAdd (int iMagin int j) {

Return iTunj

}

}

Public class Lambda {

Public static void main (String [] args) {

The / / lambda expression needs to return a value, and there is only one statement in its code block that omits return

/ / the lambda expression automatically returns the result of this statement.

/ / An a = (iMagnej)-> {return itemj;}

AA = (iQuery j)-> iRecij; / / basic usage, which is very similar to anonymous inner classes.

System.out.println (a.add (4. 5))

}

}

Interface A {

Public int add (int iMagin int j)

}

Class B {

Public int bAdd (int iMagin int j) {

Return iTunj

}

}

Public class Lambda {

Public static void main (String [] args) {

An a=new B (). BAdd (); / / ideas refer to C # delegation

System.out.println (a.add (4. 5))

}

}

Interface A {

Public int add (int iMagin int j)

}

Class B {

/ / static

Public static int bAdd (int iMagin int j) {

Return iTunj

}

}

Public class Lambda {

Public static void main (String [] args) {

/ / the method of calling is different.

An aegis Bvuduza bAddbette / thought reference C# delegate

System.out.println (a.add (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)

/ / simplify

/ / reference class method

/ / through:: implementation, where all the parameters of the lambda expression method are automatically passed to the current method

Converter converter2 = Integer::valueOf

Integer v2 = converter2.convert ("333")

System.out.println (v2)

}

}

`

-reference an instance method of a specific object

```java

Interface IA {

Public void show (String message)

}

Class A {

Public void play (String I) {

System.out.println ("here is the method play of A" + "I:" + I)

}

}

Class Test2 {

Public static void fun2 () {

/ / original

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

Ia.show ("hello")

/ / simplify

IA ia2 = new A ():: play

Ia2.show ("world")

}

}

`

-reference construction method

```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)

/ / simplify

/ / the constructor reference replaces the lambda expression, and all parameters of the implemented method in the functional interface pass the constructor as a parameter.

IC ic2 = Person::new

Object per1 = ic2.show ("chen", 10)

System.out.println (per1)

}

}

`

-Application

```java

String [] strings= {"123,456,789", "Zhao Zhiyang is really handsome"}

List 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 arrayList=new ArrayList ()

Person zs=new Person (Zhang San, 20)

Person zs2=new Person ("Zhang San", 20); / / will not be put in.

Person ls=new Person (Li Si, 30)

Person ww=new Person (Wang Wu, 40)

/ / contains determines that the equals method of the object is compared.

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 ())

}

}

}

`

The container associated with hash is to override equals hashcode

TreeSet also implements the Comparable interface and overrides the compareTo method. Or a custom comparator Comparator

```java

Public class Demo9 {

Public static void main (String [] args) {

/ / create a comparator object

ComStrWithLength comStrWithLength = new ComStrWithLength ()

/ / give the comparator object to TreeSet

Set set = new TreeSet (comStrWithLength)

/ *

* sorting and removing duplicates are implemented by TreeSet's add method. By calling the element's compareTo method

* the String class has implemented 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 a string

If (! (O1 instanceof String)) {

Throw new ClassCastException ("type conversion error")

}

If (! (O2 instanceof String)) {

Throw new ClassCastException ("type conversion error")

}

/ / transition downwards

String S1 = (String) o1

String S2 = (String) O2

/ / first according to the length ratio

Int num = s1.length ()-s2.length ()

/ / the length is the same, and then compare it in dictionary order

Return num==0?s1.compareTo (S2): num

}

}

Generics

Function:

1. The use of generics no longer requires fault tolerance, downward transformation, forced type conversion-simplify the code

two。 The problems in the run-time are checked in advance to the compilation stage, which improves the security and programming efficiency of the code.

This is the end of the description and example usage of Lambda expressions. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report