文章来源:http://www.itnose.net/detail/6036550.html
更多文章:http://www.itnose.net/type/1.html
Java 反射类:ReflexUtil
public class ReflexUtil { static Logger logger = LoggerFactory.getLogger(ReflexUtil.class); //getMethod static public Object invokeMethod(String propertiesName, Object object) { try { if(object==null) return null; if (!propertiesName.contains(".")) { String methodName = "get"+getMethodName(propertiesName); Method method = object.getClass().getMethod(methodName); return method.invoke(object); } String methodName = "get"+getMethodName(propertiesName.substring(0,propertiesName.indexOf("."))); Method method = object.getClass().getMethod(methodName); return invokeMethod(propertiesName.substring(propertiesName.indexOf(".")+1), method.invoke(object)); } catch (Exception e) { logger.error(e.toString(), e); return null; } } private static String getMethodName(String fildeName) { byte[] items = fildeName.getBytes(); items[0] = (byte) ((char) items[0] - 'a' + 'A'); return new String(items); } public static void main(String args[]) { Video video = new Video(); Album album = new Album(); album.setAlbumId(346l); video.setAlbum(album); video.setVideoId(126l); System.out.println(ReflexUtil.invokeMethod("album.albumId", video)); } }
Java 对象排序 : CompareUtil
public class CompareUtil { //sort 1正序 -1 倒序 filed 多字段排序 public static <T> Comparator createComparator(int sort, String... filed) { return new ImComparator(sort, filed); } public static class ImComparator implements Comparator { int sort = 1; String[] filed; public ImComparator(int sort, String... filed) { this.sort = sort == -1 ? -1 : 1; this.filed = filed; } @Override public int compare(Object o1, Object o2) { int result = 0; for (String file : filed) { Object value1 = ReflexUtil.invokeMethod(file, o1); Object value2 = ReflexUtil.invokeMethod(file, o2); if (value1 == null || value2 == null) { continue; } if (!(value1 instanceof Integer) || !(value1 instanceof Integer)) { continue; } int v1 = Integer.valueOf(value1.toString()); int v2 = Integer.valueOf(value2.toString()); if (v1 == v2) continue; if (sort == 1) { return v1 - v2; } else if (sort == -1) { return v2 - v1; } else { continue; } } return result; } } public static void main(String args[]) { LabelAlbum label1 = new LabelAlbum(); label1.setLabelId(1); label1.setSequnces(1); LabelAlbum label2 = new LabelAlbum(); label2.setLabelId(1);label2.setSequnces(2); LabelAlbum label3 = new LabelAlbum(); label3.setLabelId(3); label3.setSequnces(4); LabelAlbum label4 = new LabelAlbum(); label4.setLabelId(3);label4.setSequnces(3); LabelAlbum label5 = new LabelAlbum(); label5.setLabelId(4);label5.setSequnces(2); List<LabelAlbum> list = new ArrayList<LabelAlbum>(); list.add(label1); list.add(label2); list.add(label3); list.add(label4); list.add(label5); Collections.sort(list, CompareUtil.createComparator(1, "labelId","sequnces")); for (int i = 0; i < list.size(); i++) { LabelAlbum labelAlbum=list.get(i); System.out.println("labelId:"+labelAlbum.getLabelId()+" sequence:"+labelAlbum.getSequnces()); } } }
相关推荐
java排序Comparator和Comparable
程序实现时间排序Comparator
java Comparator 用法 例子java Comparator 用法 例子java Comparator 用法 例子java Comparator 用法 例子
1.import java.util.Comparator; 2.import net.sourceforge.pinyin4j.PinyinHelper; 3./** 4. * 汉字按照拼音排序的比较器 5. * @author KennyLee 2009-2-23 10:08:59 6. * 7. */ 8.public class ...
Java-中的-Comparator-和-Comparable.md
List对象集合的排序:比较器Comparator
Java中Comparator接口的用法 对集合排序的例子 Comparator
可以通过两种方式自定义对象的属性大小进行比较,对一个list或数组对根据属性排序一个list或数组
主要介绍了JAVA使用Comparator接口实现自定义排序,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
NULL 博文链接:https://singhoo.iteye.com/blog/1704428
Comparable和Comparator接口都可用作普通意义上对象间的比大小,但两个接口在实例化方面的用法不尽相同,接下来我们就来详细对比Java中的Comparable排序接口和Comparator比较器接口
本文实例讲述了Java编程实现中英混合字符串数组按首字母排序的方法。分享给大家供大家参考,具体如下: 在Java中对于字符串数组的排序,我们可以使用Arrays.sort(String[])方法很便捷的进行排序。例如: String[]...
java的Comparator和Comparable
主要针对Java中Comparable和Comparator对象进行比较,感兴趣的小伙伴们可以参考一下
像这样的日志代码会更好: if (log.isLoggable(Level.FINE)) { ...假定你已经完成了分析,并且判断出是运行环境中Java 组件的性能需要改善。 ——参考资料:仅个人学习,未详尽测试,请自行调试!!
要注意的是List,Set,Queue继承了Collection接口,...这里想用一个简单的例子展示一下他们的使用,内容包括:List、Map、Set、Queue,Collections、Comparable与Comparator,排序、搜索,内部类,泛型、重写equals、hashCode
本文,先介绍Comparable 和Comparator两个接口,以及它们的差异;接着,通过示例,对它们的使用方法进行说明
在这篇教程里,我们将要去了解下即将到来的JDK 8(译注,现在JDK 8已经发布了)中的Lambda表达式——特别是怎样使用它来编写Comparator和对集合(Collection)进行排序。 这篇文章是Baeldung上的“Java ——...
主要介绍了java 中Comparable与Comparator详解与比较的相关资料,需要的朋友可以参考下
主要是对java中将汉字按照拼音排序的实现代码进行了详细的分析介绍。需要的朋友可以过来参考下,希望对大家有所帮助