美文网首页
Compare 自反性复现

Compare 自反性复现

作者: 十二找十三 | 来源:发表于2022-06-29 13:32 被阅读0次
    package com.study.other;
    
    import java.math.BigDecimal;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    public class CompareQuestion {
    
        private static class Demo implements Comparable<Demo> {
            private BigDecimal prop1;
    
            public BigDecimal getProp1() {
                return prop1;
            }
    
            public void setProp1(BigDecimal prop1) {
                this.prop1 = prop1;
            }
    
            @Override
            public int compareTo(Demo o) {
                return o.prop1.compareTo(this.prop1) == 0 ? -1 : o.prop1.compareTo(this.prop1);
            }
        }
    
        public static void main(String[] args) {
            int[] array = new int[]{1, 2, 3, 1, 2, 3,
                    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
                    3, 2, 1, 3, 2, 1, 3, 2, 1, 3, 2,
                    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
                    1, 1, 1, 1, 1, 1};
    
            int len = array.length;
            System.out.println(len);
    
            List<Demo> list = new ArrayList<>(len);
    
            for (int i = 0; i < len; i++) {
                Demo obj = new Demo();
                obj.setProp1(new BigDecimal(array[i]));
                list.add(obj);
            }
            Collections.sort(list);
        }
    }
    
    

    相关文章

      网友评论

          本文标题:Compare 自反性复现

          本文链接:https://www.haomeiwen.com/subject/mritbrtx.html