Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of java.util.function.*pojo reflection in java8

Shulou Source: shulou.com Published: 2022-06-03 14:33:25 10月01日 Update

This article will explain in detail about java.util.function.* in java 8 Pojo reflection example analysis, Xiaobian think quite practical, so share to everyone for a reference, I hope you can read this article after some harvest.

Write an ordinary POJO

public class City { private String name; private String code; public City() { } public City(String name, String code) { this.name = name; this.code = code; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCode() { return code; } public void setCode(String code) { this.code = code; }}

a conventional manner

// Use a constructor with parameters to create a CityCity sf = new City("San Francisco", "SF");// Use a default constructor with no parameters to create a CityCity la = new City();// Set the members using settersla.setName("Los Angeles");la.setCode("LA");

New getter access method

// Use the City's method references and assign them to functionsFunction getNameFunction = City::getName;Function getCodeFunction = City::getCode;System.out.println("The code for " + getNameFunction.apply(sf) + " is " + getCodeFunction.apply(sf));-> The code for San Francisco is SF

New setter access method

// Use the City's method references and assign them to biconsumersBiConsumer setNameBiConsumer = City::setName;BiConsumer setCodeBiConsumer = City::setCode;City ny = new City();setNameBiConsumer.accept(ny, "New York");setCodeBiConsumer.accept(ny, "NY");

Visit constructor to create a new instance

// Use the City's constructor method reference to create// a default constructor reference.Supplier defaultConstructor = City::new;City sd = defaultConstructor.get();sd.setName("San Diego");sd.setCode("SD");

Parametric Builder

// Use the City's constructor method reference to create// a two-parameter constructor reference.BiFunction twoParameterConstructor = City::new;City dc = twoParameterConstructor.apply("Washington, D. C. ", "DC"); About" java.util.function.* in java8 Pojo reflection example analysis "This article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.

Tags: Ways articles examples analysis reflection more good practical ordinary traditional content parameters examples articles knowledge C.Bing D. reference help related Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker macOS Shulou Tech Info Microsoft OPPO Reno