In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you how Java8 gets the field name through Function, I believe most people don't know much about it, so share this article for your reference. I hope you can learn a lot after reading this article. Let's learn about it together.
Method 1:
Declare String key1= "aaa"; key is key1,value and map.get (key1)
Map map=new HashMap (); map.put ("aaa", 1); / / get the key of map and value / / key is key1 String key1= "aaa"; / / value is map.get (key1) map.get (key1)
Then it seems that there is no other method in daily use, here is another way to use it, without saying much, go directly to the code [/ code]
Import java.io.Serializable;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import java.lang.invoke.SerializedLambda;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.function.Function / * Java8 obtains the field name through the Function function (gets the field name of the entity class) * @ see ColumnUtil#main (java.lang.String []) uses the example * @ author jx * / public class ColumnUtil {/ * to enable Function to obtain serialization capability * / @ FunctionalInterface public interface SFunction extends Function, Serializable {} / * * field name annotation Declaration table field * / @ Target (ElementType.FIELD) @ Retention (RetentionPolicy.RUNTIME) public @ interface TableField {String value () default "" } / / default configuration static String defaultSplit = ""; static Integer defaultToType = 0; / * get the field name of the entity class (field name declared by the entity) * / public static String getFieldName (SFunction fn) {return getFieldName (fn, defaultSplit) } / * get the field name of the entity class * @ param split delimiter, multiple letter custom delimiter * / public static String getFieldName (SFunction fn, String split) {return getFieldName (fn, split, defaultToType) } / * get the field name of the entity class * @ param split delimiter, multiple letters custom delimiter * @ param toType conversion, multiple letters return 0 in uppercase and lowercase. Do not do conversion 1. Capital 2. Lowercase * / public static String getFieldName (SFunction fn, String split, Integer toType) {SerializedLambda serializedLambda = getSerializedLambda (fn); / / String fieldName = serializedLambda.getImplMethodName (). Substring ("get" .length ()) from lambda information; fieldName = fieldName.replaceFirst (fieldName.charAt (0) + ", (fieldName.charAt (0) +") .toLowerCase (); Field field Try {field = Class.forName (serializedLambda.getImplClass (). Replace ("/", ".)) .getDeclaredField (fieldName);} catch (ClassNotFoundException | NoSuchFieldException e) {throw new RuntimeException (e);} / / retrieve the field name from field, and TableField tableField = field.getAnnotation (TableField.class) can be adjusted according to the actual situation. If (tableField! = null & tableField.value (). Length () > 0) {return tableField.value ();} else {/ / 0. Do not do conversion 1. Capital 2. Lowercase switch (toType) {case 1: return fieldName.replaceAll ("[Amurz]", split + "$0"). ToUpperCase (); case 2: return fieldName.replaceAll ("[Amurz]", split + "$0") .toLowerCase () Default: return fieldName.replaceAll ("[Amurz]", split + "$0");} private static SerializedLambda getSerializedLambda (SFunction fn) {/ / take serialization method Method writeReplaceMethod; try {writeReplaceMethod = fn.getClass () .getDeclaredMethod ("writeReplace") from function } catch (NoSuchMethodException e) {throw new RuntimeException (e);} / / retrieve serialized lambda information from the serialization method boolean isAccessible = writeReplaceMethod.isAccessible (); writeReplaceMethod.setAccessible (true); SerializedLambda serializedLambda; try {serializedLambda = (SerializedLambda) writeReplaceMethod.invoke (fn) } catch (IllegalAccessException | InvocationTargetException e) {throw new RuntimeException (e);} writeReplaceMethod.setAccessible (isAccessible); return serializedLambda;} / * Test user entity classes * / public static class TestUserDemo implements Serializable {private static final long serialVersionUID = 1L; private String loginName; private String name; private String companySimpleName @ ColumnUtil.TableField ("nick") private String nickName; public String getLoginName () {return loginName;} public void setLoginName (String loginName) {this.loginName = loginName;} public String getNickName () {return nickName;} public void setNickName (String nickName) {this.nickName = nickName } public static long getSerialVersionUID () {return serialVersionUID;} public String getName () {return name;} public void setName (String name) {this.name = name;} public String getCompanySimpleName () {return companySimpleName } public void setCompanySimpleName (String companySimpleName) {this.companySimpleName = companySimpleName;}} / * reference example * / public static void main (String [] args) {/ / entity class original field name returns System.out.println (); System.out.println ("entity class original field name returns") System.out.println ("field name:" + ColumnUtil.getFieldName (TestUserDemo::getLoginName)); System.out.println ("field name:" + ColumnUtil.getFieldName (TestUserDemo::getNickName)); System.out.println ("field name:" + ColumnUtil.getFieldName (TestUserDemo::getCompanySimpleName)); System.out.println (); System.out.println ("entity class field name plus delimiter") System.out.println ("field name:" + ColumnUtil.getFieldName (TestUserDemo::getCompanySimpleName, "_")); System.out.println (); System.out.println ("entity class field name plus delimiter + case"); System.out.println ("field name:" + ColumnUtil.getFieldName (TestUserDemo::getCompanySimpleName, "_", 0)) System.out.println ("field name:" + ColumnUtil.getFieldName (TestUserDemo::getCompanySimpleName, "_", 1)); System.out.println ("field name:" + ColumnUtil.getFieldName (TestUserDemo::getCompanySimpleName, "_", 2));}}
Output result:
The above is all the content of the article "how Java8 gets field names through Function". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.