Murali
Murali

Reputation: 378

How to sort Hashset<> by field?

I have a Set<MultiAdminComponent> rootItems = new HashSet<>();HashSet. In this I have all my Screen.

In my HashSet I am going to have all my rows. I want to sort the MultiAdminComponent based on rowId.

This is MultiAdminComponent

 public class MultiAdminComponent {

        private String componentName;
        private String componentIdentification;
        private String componentType;
        private String componentState;
        private String componentUrl;
        private String componentId;
        private String rowId;
        private List<MultiAdminComponent> items;
        private int componentStateId;
        private int ctastatus;
        private String actionId;
        private String actionToPerform;
        private int orderNumber;
        private int ctarevision;

How can I sort based on RowId in MultiAdminComponent bean

Upvotes: 1

Views: 4287

Answers (3)

LazerBanana
LazerBanana

Reputation: 7211

Just to let you know, there are few more Sets.

  • HashSet is not ordered/sorted
  • LinkedHashSet sorted by the order that it's been inserted
  • TreeSet sorted in natural order

I know the question been answered, but truly I can't see a point of using a HashSet which is not ordered and then try to order it when you can just use a TreeSet?

Upvotes: 3

andy.hu
andy.hu

Reputation: 355

Before Java 1.8 version, which can help you

public class Test {
    public static void main(String[] args) {
        MultiAdminComponent m1 = new MultiAdminComponent("1");
        MultiAdminComponent m2 = new MultiAdminComponent("2");
        MultiAdminComponent m3 = new MultiAdminComponent("3");
        Set<MultiAdminComponent> set = new HashSet<MultiAdminComponent>();
        set.add(m1);
        set.add(m3);
        set.add(m2);
        List<MultiAdminComponent> list = new ArrayList<MultiAdminComponent>(set);
        for (MultiAdminComponent m : list) {
            System.out.println("before" + m.getRowId());
        }
        Collections.sort(list, new Comparator<MultiAdminComponent>() {
            @Override
            public int compare(MultiAdminComponent m1, MultiAdminComponent m2) {
                return m1.getRowId().compareTo(m2.getRowId());
            }
        });
        for (MultiAdminComponent m : list) {
            System.out.println("after" + m.getRowId());
        }
    }
}

class MultiAdminComponent {
    private String rowId;

    public MultiAdminComponent(String rowId) {
        super();
        this.rowId = rowId;
    }

    public String getRowId() {
        return rowId;
    }

    public void setRowId(String rowId) {
        this.rowId = rowId;
    }

}

Upvotes: 1

Grzegorz Piwowarek
Grzegorz Piwowarek

Reputation: 13783

Since you are on Java 8, you can leverage Stream API and Comparator.comparing() for this:

List<MultiAdminComponent> sortedList = rootItems.stream()
   .sorted(Comparator.comparing(MultiAdminComponent::getRowId))
   .collect(Collectors.toList()); // collect to whatever you want

Keep in mind that HashSet does not maintain order so you should keep the sorted result in some other data structure.

And remember that this is not going to sort your Set this is going to return a List of naturally sorted MultiAdminComponents and you need to capture the result of this operation.

Upvotes: 9

Related Questions