wave-gbt / useful-java-snippets Goto Github PK
View Code? Open in Web Editor NEWA curated collection of useful Java snippets
License: Apache License 2.0
A curated collection of useful Java snippets
License: Apache License 2.0
/**
* 根据属性名获取属性值
* @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;
}
lists.stream().collect(collectingAndThen(toCollection(() -> new TreeSet<>(Comparator.comparing(o -> Rmap.getStr(o,"orderId")))),
ArrayList::new));
public static Map asMap(Object... objects) {
Map m = new HashMap(16);
for (int i = 0; i < objects.length; i += 2) {
if (i + 1 < objects.length) {
m.put(objects[i], objects[i + 1]);
} else {
m.put(objects[i], null);
}
}
return m;
}
/**
* 把 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 请求拼接参数
* @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();
}
}
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.