Code Monkey home page Code Monkey logo

useful-java-snippets's People

Stargazers

 avatar

Watchers

 avatar

useful-java-snippets's Issues

根据属性名获取属性值

/**
* 根据属性名获取属性值
* @param fieldName
* @param o
* @return
*/
public static Object getFieldValueByName(String fieldName, Object o) {
try {
String firstLetter = fieldName.substring(0, 1).toUpperCase();
String getter = "get" + firstLetter + fieldName.substring(1);
Method method = o.getClass().getMethod(getter, new Class[] {});
Object value = method.invoke(o, new Object[] {});
return value;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

合并两个 list 中的元素属性

/**
     * 把 sourceList 的一些属性合并到 targetList 中
     * 基于 testFunction 的条件,合入逻辑实现为 biConsumer
     * @param targetList
     * @param sourceList
     * @param testFunction
     * @param biConsumer
     * @param <T>
     * @param <S>
     */
    public static <T, S> void merge(List<T> targetList, List<S> sourceList,
                                    BiFunction<? super T, ? super S,Boolean> testFunction,
                                    BiConsumer<? super T, ? super S> biConsumer) {
        targetList.forEach((t)->{
            Optional<S> optional = sourceList.stream().filter(s -> testFunction.apply(t,s)).findFirst();
            if (optional.isPresent()) {
                biConsumer.accept(t,optional.get());
            }
        });
    }

get 请求拼接参数

 /**
     * get 请求拼接参数
     * @param params
     * @return
     */
    public static String paramsConvertUrl(Map<String, Object> params) {
        StringBuilder urlParams = new StringBuilder("?");
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            urlParams.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
        }
        String urlParamsStr = urlParams.toString();
        return urlParamsStr.substring(0, urlParamsStr.length()-1);
    }

利用反射设置对象对应属性的值

/**
* 设置对象的对应属性的值
* @param object
* @param var
* @param value
*/
public static void setData(Object object, String var, Object value){
Class clazz = object.getClass();
Field[] fields = clazz.getDeclaredFields();
for (int i=0;i<fields.length;i++){
Field field = fields[i];
field.setAccessible(true);
String fileName = field.getName();
if(fileName.equals(var)){
try {
field.set(object,value);
break;
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.