Guava记录 - Ordering (一) - Creation

GUAVA提供了一个比较强大的排序器Ordering

一、简介:

官网简介如下
Ordering is Guava's "fluent" Comparator class, which can be used to build complex comparators and apply them to collections of objects.

At its core, an Ordering instance is nothing more than a special Comparator instance. Ordering simply takes the methods that rely on a Comparator (for example, Collections.max) and makes them available as instance methods. For additional power, Ordering class provides chaining methods to tweak and enhance existing comparators

借助翻译软件了解到,其大致概念是:它可以用来构建复杂的比较器,并将它们应用于对象集合。其核心,一个Ordering实例只不过是一个特殊的比较器实例。ordered简单地采用依赖于比较器(例如Collections.max)的方法,并将它们作为实例方法提供。为了获得更多的功能,orderclass提供了chaining的方式来调整和增强现有的比较器

二、Ordering的Creation实验

实验列表如下:

step1 测试Ordering.natural()创建ordering

Ordering通过natural()方法创建时,功能为对创建时指定的类型,进行自然排序

简单实验代码:

        //数字自然排序
        List naturalNumberList = Lists.newArrayList(1, 7, 2, 0, 4, 5, 3, 6);
        Ordering naturalNumberOrdering = Ordering.natural();
        System.out.println("======数字测试naturalOrdering ========" + JsonMoreUtils.toJson(naturalNumberOrdering.sortedCopy(naturalNumberList)));


        //日期自然排序
        LocalDateTime localDateTime1 = LocalDateTime.of(2018, 9, 25, 16, 46);
        LocalDateTime localDateTime2 = LocalDateTime.of(2018, 9, 26, 16, 46);
        LocalDateTime localDateTime3 = LocalDateTime.of(2018, 8, 1, 16, 46);

        List naturalDateList = Lists.newArrayList(localDateTime1,localDateTime2,localDateTime3);

        Ordering naturalDateOrdering = Ordering.natural();

        System.out.println("======日期测试naturalOrdering========" + JsonMoreUtils.toJson(naturalDateOrdering.sortedCopy(naturalDateList)));

实验结果:

======数字测试naturalOrdering ========[0,1,2,3,4,5,6,7]

======日期测试naturalOrdering========[{"year":2018,"monthValue":8,"month":"AUGUST","dayOfMonth":1,"dayOfYear":213,"dayOfWeek":"WEDNESDAY","hour":16,"minute":46,"second":0,"nano":0,"chronology":{"id":"ISO","calendarType":"iso8601"}},{"year":2018,"monthValue":9,"month":"SEPTEMBER","dayOfMonth":25,"dayOfYear":268,"dayOfWeek":"TUESDAY","hour":16,"minute":46,"second":0,"nano":0,"chronology":{"id":"ISO","calendarType":"iso8601"}},{"year":2018,"monthValue":9,"month":"SEPTEMBER","dayOfMonth":26,"dayOfYear":269,"dayOfWeek":"WEDNESDAY","hour":16,"minute":46,"second":0,"nano":0,"chronology":{"id":"ISO","calendarType":"iso8601"}}]

结论:可以看出其确实是按照指定类型自然排序

step2 测试Ordering.from()创建ordering

Ordering通过from()方法创建时,会传入一个指定的Comparator.
功能为按照创建时指定的Comparator,进行排序.

简单实验代码:

        List fromNumberList = Lists.newArrayList(1, 7, 2, 0, 4, 5, 3, 6);
        //from方法测试
        Ordering fromOrdering = Ordering.from((Comparator) Comparator.naturalOrder());
        System.out.println("======数字测试fromOrdering ========" + JsonMoreUtils.toJson(fromOrdering.sortedCopy(fromNumberList)));

实验结果:

======数字测试fromOrdering ========[0,1,2,3,4,5,6,7]
step3 测试Ordering.explicit()创建ordering

Ordering通过explicit()方法创建时,会传入一个指定的List.
功能为按照创建时指定的List的顺序,进行精准排序.

简单实验代码:

        List explicitNumberList = Lists.newArrayList(1, 7, 2, 0, 4, 5, 3, 6);
        
        //explicit方法测试
        System.out.println("====================测试Ordering.explicit========================");
        List explicitList1 = Lists.newArrayList(1, 7, 2, 0, 4, 5, 3, 6);
        System.out.println("=====正向测试======explicitOrdering1===============" + JsonMoreUtils.toJson(Ordering.explicit(explicitList1).sortedCopy(explicitNumberList)));
        List explicitList2 = Lists.newArrayList(1, 3, 2, 4, 5, 7, 6, 0);
        System.out.println("=======正向测试====测试explicitOrdering2===============" + JsonMoreUtils.toJson(Ordering.explicit(explicitList2).sortedCopy(explicitNumberList)));
        
        //值得注意的是sortedCopy中的元素都得有比较的值,否则会出现某节点无法compare异常 & 精确序列中不允许存在相同的元素,否则会抛出same key异常
        List explicitList3 = Lists.newArrayList(1, 3, 2, 4, 5, 7, 6, 1);
        System.out.println("=======反向测试====测试explicitOrdering3相同key===============" + JsonMoreUtils.toJson(Ordering.explicit(explicitList3).sortedCopy(explicitNumberList)));
        List explicitList4 = Lists.newArrayList(1, 3, 2, 4, 5, 7, 6, 8);
        System.out.println("=======反向测试====测试explicitOrdering4相同key===============" + JsonMoreUtils.toJson(Ordering.explicit(explicitList4).sortedCopy(explicitNumberList)));

实验结果:

=====正向测试======explicitOrdering1===============[1,7,2,0,4,5,3,6]
=======正向测试====测试explicitOrdering2===============[1,3,2,4,5,7,6,0]

拥有重复元素结果:


image.png

未完全包含精确元素结果:


image.png
step4 测试Ordering.usingToString()创建ordering

Ordering通过usingToString()方法创建时。功能为对创建时指定的类型,进行对象toString()之后自然排序

简单实验代码:

        List usingToStringList = Lists.newArrayList(1, 7, 2, 0, 4, 5, 3, 6);
        Ordering usingToStringOrdering = Ordering.usingToString();
        System.out.println("=========正向数字测试========usingToStringOrdering=======" + JsonMoreUtils.toJson(usingToStringOrdering.sortedCopy(usingToStringList)));

        List usingToStringList12 = Lists.newArrayList("aaa", "ccccc", "bbbb");
        System.out.println("=========正向字符测试========usingToStringOrdering=======" + JsonMoreUtils.toJson(usingToStringOrdering.sortedCopy(usingToStringList12)));
 
 

实验结果:

=========正向数字测试========usingToStringOrdering=======[0,1,2,3,4,5,6,7]
=========正向字符测试========usingToStringOrdering=======["aaa","bbbb","ccccc"]
step5 测试Ordering.arbitrary()创建ordering

Ordering通过arbitrary()方法创建时。功能为打乱原有的排序

简单实验代码:

        List arbitraryList = Lists.newArrayList(1, 7, 2, 0, 4, 5, 3, 6);

        //打乱原有的排序
        Ordering arbitraryOrdering = Ordering.arbitrary();
        System.out.println("=========正向测试========arbitraryOrdering1=======" + JsonMoreUtils.toJson(arbitraryOrdering.sortedCopy(arbitraryList)));
        System.out.println("=========正向测试========arbitraryOrdering2=======" + JsonMoreUtils.toJson(arbitraryOrdering.sortedCopy(arbitraryList)));
        System.out.println("=========正向测试========arbitraryOrdering3=======" + JsonMoreUtils.toJson(arbitraryOrdering.sortedCopy(arbitraryList)));
        System.out.println("=========正向测试========arbitraryOrdering4=======" + JsonMoreUtils.toJson(arbitraryOrdering.sortedCopy(arbitraryList)));
 
 

实验结果:

=========正向测试========arbitraryOrdering1=======[7,4,0,3,5,2,1,6]
=========正向测试========arbitraryOrdering2=======[7,4,0,3,5,2,1,6]
=========正向测试========arbitraryOrdering3=======[7,4,0,3,5,2,1,6]
=========正向测试========arbitraryOrdering4=======[7,4,0,3,5,2,1,6]

可以看出顺序的确是打乱了,但是每次调用返回的结果是一致的.

未完待续................

你可能感兴趣的:(Guava记录 - Ordering (一) - Creation)