• 热门专题

Java对象多字段排序Comparator

作者:  发布日期:2014-05-06 21:42:34
Tag标签:字段  对象  
  • 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());
            }
        }
    }


About IT165 - 广告服务 - 隐私声明 - 版权申明 - 免责条款 - 网站地图 - 网友投稿 - 联系方式
本站内容来自于互联网,仅供用于网络技术学习,学习中请遵循相关法律法规