简体   繁体   中英

Different output of sorting code in Java 6 and Java 8

I have a below code written for sorting my data . Output of this code is different in Java 6 and Java 8. Also the sequence in which objects are compared is reverse and I think this is causing change in final output. The sorting algorithm may not be correct but still can someone please explain why this is working this way ?

Please check attached code and output in Java 6 and Java 8.

Code

package assignements;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

class ComparableObject {
    private String id;
    private String name;
    private boolean hidden;
    private Integer order;


    ComparableObject() {
    }

    ComparableObject(String id, String name, boolean hidden, Integer columnOrder) {
        this.id = id;
        this.name = name;
        this.hidden = hidden;
        this.order = columnOrder;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isHidden() {
        return hidden;
    }

    public void setHidden(boolean hidden) {
        this.hidden = hidden;
    }

    public Integer getOrder() {
        return order;
    }

    public void setOrder(Integer order) {
        this.order = order;
    }
}

public class ComparatorPoc {

    public static void main(String[] args) {
        List<ComparableObject> list = new ArrayList<ComparableObject>();
        list.add(new ComparableObject("1547", "id", false, 10));
        list.add(new ComparableObject("1548", "name", true, 20));
        list.add(new ComparableObject("1592", "customername", false, 30));
        list.add(new ComparableObject("1549", "customername", true, 40));
        list.add(new ComparableObject("1551", "OrderTo", false, 50));
        list.add(new ComparableObject("1553", "PO", true, 60));

        Collections.sort(list, new Comparator<ComparableObject>() {
            @Override
            public int compare(ComparableObject o1, ComparableObject o2) {
                System.out.println(o1.getName());
                System.out.println(o2.getName());
                System.out.println("****************");
                boolean o1Hidden = o1.isHidden();
                boolean o2Hidden = o2.isHidden();
                if (o1Hidden && o2Hidden)
                    return 0;
                else if (o1Hidden)
                    return 1;
                else
                    return -1;
            }
        });

        for (ComparableObject p : list) {
            System.out.println("Id=" + p.getId() + " Name=" + p.getName() + " Column Order=" + p.getOrder() + " IsHidden=" + p.isHidden());
        }
    }
}

Java 6 Output

id
name
****************
name
customername
****************
id
customername
****************
name
customername
****************
customername
OrderTo
****************
name
OrderTo
****************
customername
OrderTo
****************
customername
PO
****************
Id=1547 Name=id Column Order=10 IsHidden=false
Id=1592 Name=customername Column Order=30 IsHidden=false
Id=1551 Name=OrderTo Column Order=50 IsHidden=false
Id=1548 Name=name Column Order=20 IsHidden=true
Id=1549 Name=customername Column Order=40 IsHidden=true
Id=1553 Name=PO Column Order=60 IsHidden=true

Java 8 Output

name
id
****************
customername
name
****************
customername
name
****************
customername
id
****************
customername
id
****************
customername
name
****************
OrderTo
name
****************
OrderTo
id
****************
OrderTo
customername
****************
PO
id
****************
PO
customername
****************
Id=1551 Name=OrderTo Column Order=50 IsHidden=false
Id=1592 Name=customername Column Order=30 IsHidden=false
Id=1547 Name=id Column Order=10 IsHidden=false
Id=1548 Name=name Column Order=20 IsHidden=true
Id=1549 Name=customername Column Order=40 IsHidden=true
Id=1553 Name=PO Column Order=60 IsHidden=true

All guarantees given by methods using a Comparator assume that the Comparator correctly implements the compare contract . But there are several easy ways in which it can be subtly broken.

In your specific case you break the constract that the signs of compare(a, b) and compare(b, a) have to be opposite (unless they both return 0 ) when hidden is false for both a and b :

The implementor must ensure that sgn(compare(x, y)) == -sgn(compare(y, x)) for all x and y .

Since you broke the contract, the exact behaviour of the sort() method is no longer specified.

There has probably been a change in the sorting algorithm used between Java 6 and Java 8, which means that they handle this (unspecified) situation differently. That's not a bug in the sort code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM