Sort a set in Java











up vote
2
down vote

favorite












Framework used: Spring



ORM used: Hibernate



I have two classes



class BatchExceptionDetails{
...
private Set<BatchExceptionComments> batchExceptionComments;
}

class BatchExceptionComments implements Comparable<BatchExceptionComments>{
...
@Override
public int compareTo(BatchExceptionComments o) {
// TODO Auto-generated method stub

return this.getAddedOn().compareTo(o.getAddedOn());
}
}


They are mapped with one to many mapping.



There is a set of BatchExceptionComments in BatchExceptionDetails.



I want to sort the set on the basis of Date. BatchExcpetionComment has an attribute of type java.util.Date i.e. addedOn. I want the latest comment to be the first element of set.



The set I am receiving is not sorted. Will you please guide me where I am going wrong.



Thanks in advance










share|improve this question






















  • Are you using a SortedSet? Or some other type of Set?
    – Joe C
    Nov 22 at 7:45










  • Collections.sort(batchExceptionComments);
    – Lorelorelore
    Nov 22 at 7:46










  • @Lorelorelore That's only available for lists, not for sets.
    – Joe C
    Nov 22 at 7:48






  • 2




    Sets are unsorted. Use a list instead.
    – Robby Cornelissen
    Nov 22 at 7:49








  • 2




    You can't be using just Set, because Set is an interface. What implementation of Set are you using?
    – Joe C
    Nov 22 at 7:50















up vote
2
down vote

favorite












Framework used: Spring



ORM used: Hibernate



I have two classes



class BatchExceptionDetails{
...
private Set<BatchExceptionComments> batchExceptionComments;
}

class BatchExceptionComments implements Comparable<BatchExceptionComments>{
...
@Override
public int compareTo(BatchExceptionComments o) {
// TODO Auto-generated method stub

return this.getAddedOn().compareTo(o.getAddedOn());
}
}


They are mapped with one to many mapping.



There is a set of BatchExceptionComments in BatchExceptionDetails.



I want to sort the set on the basis of Date. BatchExcpetionComment has an attribute of type java.util.Date i.e. addedOn. I want the latest comment to be the first element of set.



The set I am receiving is not sorted. Will you please guide me where I am going wrong.



Thanks in advance










share|improve this question






















  • Are you using a SortedSet? Or some other type of Set?
    – Joe C
    Nov 22 at 7:45










  • Collections.sort(batchExceptionComments);
    – Lorelorelore
    Nov 22 at 7:46










  • @Lorelorelore That's only available for lists, not for sets.
    – Joe C
    Nov 22 at 7:48






  • 2




    Sets are unsorted. Use a list instead.
    – Robby Cornelissen
    Nov 22 at 7:49








  • 2




    You can't be using just Set, because Set is an interface. What implementation of Set are you using?
    – Joe C
    Nov 22 at 7:50













up vote
2
down vote

favorite









up vote
2
down vote

favorite











Framework used: Spring



ORM used: Hibernate



I have two classes



class BatchExceptionDetails{
...
private Set<BatchExceptionComments> batchExceptionComments;
}

class BatchExceptionComments implements Comparable<BatchExceptionComments>{
...
@Override
public int compareTo(BatchExceptionComments o) {
// TODO Auto-generated method stub

return this.getAddedOn().compareTo(o.getAddedOn());
}
}


They are mapped with one to many mapping.



There is a set of BatchExceptionComments in BatchExceptionDetails.



I want to sort the set on the basis of Date. BatchExcpetionComment has an attribute of type java.util.Date i.e. addedOn. I want the latest comment to be the first element of set.



The set I am receiving is not sorted. Will you please guide me where I am going wrong.



Thanks in advance










share|improve this question













Framework used: Spring



ORM used: Hibernate



I have two classes



class BatchExceptionDetails{
...
private Set<BatchExceptionComments> batchExceptionComments;
}

class BatchExceptionComments implements Comparable<BatchExceptionComments>{
...
@Override
public int compareTo(BatchExceptionComments o) {
// TODO Auto-generated method stub

return this.getAddedOn().compareTo(o.getAddedOn());
}
}


They are mapped with one to many mapping.



There is a set of BatchExceptionComments in BatchExceptionDetails.



I want to sort the set on the basis of Date. BatchExcpetionComment has an attribute of type java.util.Date i.e. addedOn. I want the latest comment to be the first element of set.



The set I am receiving is not sorted. Will you please guide me where I am going wrong.



Thanks in advance







java spring hibernate sorting collections






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 at 7:44









Ayush

658




658












  • Are you using a SortedSet? Or some other type of Set?
    – Joe C
    Nov 22 at 7:45










  • Collections.sort(batchExceptionComments);
    – Lorelorelore
    Nov 22 at 7:46










  • @Lorelorelore That's only available for lists, not for sets.
    – Joe C
    Nov 22 at 7:48






  • 2




    Sets are unsorted. Use a list instead.
    – Robby Cornelissen
    Nov 22 at 7:49








  • 2




    You can't be using just Set, because Set is an interface. What implementation of Set are you using?
    – Joe C
    Nov 22 at 7:50


















  • Are you using a SortedSet? Or some other type of Set?
    – Joe C
    Nov 22 at 7:45










  • Collections.sort(batchExceptionComments);
    – Lorelorelore
    Nov 22 at 7:46










  • @Lorelorelore That's only available for lists, not for sets.
    – Joe C
    Nov 22 at 7:48






  • 2




    Sets are unsorted. Use a list instead.
    – Robby Cornelissen
    Nov 22 at 7:49








  • 2




    You can't be using just Set, because Set is an interface. What implementation of Set are you using?
    – Joe C
    Nov 22 at 7:50
















Are you using a SortedSet? Or some other type of Set?
– Joe C
Nov 22 at 7:45




Are you using a SortedSet? Or some other type of Set?
– Joe C
Nov 22 at 7:45












Collections.sort(batchExceptionComments);
– Lorelorelore
Nov 22 at 7:46




Collections.sort(batchExceptionComments);
– Lorelorelore
Nov 22 at 7:46












@Lorelorelore That's only available for lists, not for sets.
– Joe C
Nov 22 at 7:48




@Lorelorelore That's only available for lists, not for sets.
– Joe C
Nov 22 at 7:48




2




2




Sets are unsorted. Use a list instead.
– Robby Cornelissen
Nov 22 at 7:49






Sets are unsorted. Use a list instead.
– Robby Cornelissen
Nov 22 at 7:49






2




2




You can't be using just Set, because Set is an interface. What implementation of Set are you using?
– Joe C
Nov 22 at 7:50




You can't be using just Set, because Set is an interface. What implementation of Set are you using?
– Joe C
Nov 22 at 7:50












2 Answers
2






active

oldest

votes

















up vote
5
down vote













Set is an interface, so it is not possible to establish if it is sortable or not. You have to use the correct implementation, like TreeSet. If you want to
emphasize that it is a sorted set, you should use the SortedSet interface. TreeSet implements SortedSet.



Alternatively you can use a List and then you can sort it using Collections.sort.






share|improve this answer





















  • Can I use other collections instead of Set ? I am in assumption that if we are using Spring then we have to use Set for Containment.
    – Ayush
    Nov 22 at 7:58






  • 3




    The problem is that Hibernate requires collections to be specified as an interface type. In terms of implementation classes, it uses its own to support things like lazy loading. Specifying the type as SortedSet instead of Set should work though.
    – Robby Cornelissen
    Nov 22 at 7:59






  • 1




    No need to initialize it. Hibernate will automatically initialize it with its own SortedSet implementation.
    – Robby Cornelissen
    Nov 22 at 8:04












  • @RobbyCornelissen, I used SortedSet and I am getting an exception IllegalArgumentException occurred while calling setter for property [combeans.BatchExceptionDetails.batchExceptionComments (expected type = java.util.SortedSet)];
    – Ayush
    Nov 22 at 8:15










  • @RobbyCornelissen, my setter method is public void setBatchExceptionComments( SortedSet<BatchExceptionComments> batchExceptionComments) { this.batchExceptionComments = batchExceptionComments; }
    – Ayush
    Nov 22 at 8:17


















up vote
1
down vote



accepted










After some goofing around I found a solution.



I just declared the set



private Set<BatchExceptionComments> batchExceptionComments;


Instead of using Comparable I used Order By to arrange the set.



 <set name="batchExceptionComments" table="BATCH_EXCEPTION_COMMENTS" 
inverse="true" fetch="select" lazy="false" order-by="commentId">
<key>
<column name="EXCEPTION_ID" not-null="true" />
</key>
<one-to-many class="com.beans.BatchExceptionComments" />
</set>


I believe ordering by Id will be better.



P.S. I am using hbm.xml instead of annotation






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426073%2fsort-a-set-in-java%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    5
    down vote













    Set is an interface, so it is not possible to establish if it is sortable or not. You have to use the correct implementation, like TreeSet. If you want to
    emphasize that it is a sorted set, you should use the SortedSet interface. TreeSet implements SortedSet.



    Alternatively you can use a List and then you can sort it using Collections.sort.






    share|improve this answer





















    • Can I use other collections instead of Set ? I am in assumption that if we are using Spring then we have to use Set for Containment.
      – Ayush
      Nov 22 at 7:58






    • 3




      The problem is that Hibernate requires collections to be specified as an interface type. In terms of implementation classes, it uses its own to support things like lazy loading. Specifying the type as SortedSet instead of Set should work though.
      – Robby Cornelissen
      Nov 22 at 7:59






    • 1




      No need to initialize it. Hibernate will automatically initialize it with its own SortedSet implementation.
      – Robby Cornelissen
      Nov 22 at 8:04












    • @RobbyCornelissen, I used SortedSet and I am getting an exception IllegalArgumentException occurred while calling setter for property [combeans.BatchExceptionDetails.batchExceptionComments (expected type = java.util.SortedSet)];
      – Ayush
      Nov 22 at 8:15










    • @RobbyCornelissen, my setter method is public void setBatchExceptionComments( SortedSet<BatchExceptionComments> batchExceptionComments) { this.batchExceptionComments = batchExceptionComments; }
      – Ayush
      Nov 22 at 8:17















    up vote
    5
    down vote













    Set is an interface, so it is not possible to establish if it is sortable or not. You have to use the correct implementation, like TreeSet. If you want to
    emphasize that it is a sorted set, you should use the SortedSet interface. TreeSet implements SortedSet.



    Alternatively you can use a List and then you can sort it using Collections.sort.






    share|improve this answer





















    • Can I use other collections instead of Set ? I am in assumption that if we are using Spring then we have to use Set for Containment.
      – Ayush
      Nov 22 at 7:58






    • 3




      The problem is that Hibernate requires collections to be specified as an interface type. In terms of implementation classes, it uses its own to support things like lazy loading. Specifying the type as SortedSet instead of Set should work though.
      – Robby Cornelissen
      Nov 22 at 7:59






    • 1




      No need to initialize it. Hibernate will automatically initialize it with its own SortedSet implementation.
      – Robby Cornelissen
      Nov 22 at 8:04












    • @RobbyCornelissen, I used SortedSet and I am getting an exception IllegalArgumentException occurred while calling setter for property [combeans.BatchExceptionDetails.batchExceptionComments (expected type = java.util.SortedSet)];
      – Ayush
      Nov 22 at 8:15










    • @RobbyCornelissen, my setter method is public void setBatchExceptionComments( SortedSet<BatchExceptionComments> batchExceptionComments) { this.batchExceptionComments = batchExceptionComments; }
      – Ayush
      Nov 22 at 8:17













    up vote
    5
    down vote










    up vote
    5
    down vote









    Set is an interface, so it is not possible to establish if it is sortable or not. You have to use the correct implementation, like TreeSet. If you want to
    emphasize that it is a sorted set, you should use the SortedSet interface. TreeSet implements SortedSet.



    Alternatively you can use a List and then you can sort it using Collections.sort.






    share|improve this answer












    Set is an interface, so it is not possible to establish if it is sortable or not. You have to use the correct implementation, like TreeSet. If you want to
    emphasize that it is a sorted set, you should use the SortedSet interface. TreeSet implements SortedSet.



    Alternatively you can use a List and then you can sort it using Collections.sort.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 22 at 7:53









    Lorelorelore

    1,75451124




    1,75451124












    • Can I use other collections instead of Set ? I am in assumption that if we are using Spring then we have to use Set for Containment.
      – Ayush
      Nov 22 at 7:58






    • 3




      The problem is that Hibernate requires collections to be specified as an interface type. In terms of implementation classes, it uses its own to support things like lazy loading. Specifying the type as SortedSet instead of Set should work though.
      – Robby Cornelissen
      Nov 22 at 7:59






    • 1




      No need to initialize it. Hibernate will automatically initialize it with its own SortedSet implementation.
      – Robby Cornelissen
      Nov 22 at 8:04












    • @RobbyCornelissen, I used SortedSet and I am getting an exception IllegalArgumentException occurred while calling setter for property [combeans.BatchExceptionDetails.batchExceptionComments (expected type = java.util.SortedSet)];
      – Ayush
      Nov 22 at 8:15










    • @RobbyCornelissen, my setter method is public void setBatchExceptionComments( SortedSet<BatchExceptionComments> batchExceptionComments) { this.batchExceptionComments = batchExceptionComments; }
      – Ayush
      Nov 22 at 8:17


















    • Can I use other collections instead of Set ? I am in assumption that if we are using Spring then we have to use Set for Containment.
      – Ayush
      Nov 22 at 7:58






    • 3




      The problem is that Hibernate requires collections to be specified as an interface type. In terms of implementation classes, it uses its own to support things like lazy loading. Specifying the type as SortedSet instead of Set should work though.
      – Robby Cornelissen
      Nov 22 at 7:59






    • 1




      No need to initialize it. Hibernate will automatically initialize it with its own SortedSet implementation.
      – Robby Cornelissen
      Nov 22 at 8:04












    • @RobbyCornelissen, I used SortedSet and I am getting an exception IllegalArgumentException occurred while calling setter for property [combeans.BatchExceptionDetails.batchExceptionComments (expected type = java.util.SortedSet)];
      – Ayush
      Nov 22 at 8:15










    • @RobbyCornelissen, my setter method is public void setBatchExceptionComments( SortedSet<BatchExceptionComments> batchExceptionComments) { this.batchExceptionComments = batchExceptionComments; }
      – Ayush
      Nov 22 at 8:17
















    Can I use other collections instead of Set ? I am in assumption that if we are using Spring then we have to use Set for Containment.
    – Ayush
    Nov 22 at 7:58




    Can I use other collections instead of Set ? I am in assumption that if we are using Spring then we have to use Set for Containment.
    – Ayush
    Nov 22 at 7:58




    3




    3




    The problem is that Hibernate requires collections to be specified as an interface type. In terms of implementation classes, it uses its own to support things like lazy loading. Specifying the type as SortedSet instead of Set should work though.
    – Robby Cornelissen
    Nov 22 at 7:59




    The problem is that Hibernate requires collections to be specified as an interface type. In terms of implementation classes, it uses its own to support things like lazy loading. Specifying the type as SortedSet instead of Set should work though.
    – Robby Cornelissen
    Nov 22 at 7:59




    1




    1




    No need to initialize it. Hibernate will automatically initialize it with its own SortedSet implementation.
    – Robby Cornelissen
    Nov 22 at 8:04






    No need to initialize it. Hibernate will automatically initialize it with its own SortedSet implementation.
    – Robby Cornelissen
    Nov 22 at 8:04














    @RobbyCornelissen, I used SortedSet and I am getting an exception IllegalArgumentException occurred while calling setter for property [combeans.BatchExceptionDetails.batchExceptionComments (expected type = java.util.SortedSet)];
    – Ayush
    Nov 22 at 8:15




    @RobbyCornelissen, I used SortedSet and I am getting an exception IllegalArgumentException occurred while calling setter for property [combeans.BatchExceptionDetails.batchExceptionComments (expected type = java.util.SortedSet)];
    – Ayush
    Nov 22 at 8:15












    @RobbyCornelissen, my setter method is public void setBatchExceptionComments( SortedSet<BatchExceptionComments> batchExceptionComments) { this.batchExceptionComments = batchExceptionComments; }
    – Ayush
    Nov 22 at 8:17




    @RobbyCornelissen, my setter method is public void setBatchExceptionComments( SortedSet<BatchExceptionComments> batchExceptionComments) { this.batchExceptionComments = batchExceptionComments; }
    – Ayush
    Nov 22 at 8:17












    up vote
    1
    down vote



    accepted










    After some goofing around I found a solution.



    I just declared the set



    private Set<BatchExceptionComments> batchExceptionComments;


    Instead of using Comparable I used Order By to arrange the set.



     <set name="batchExceptionComments" table="BATCH_EXCEPTION_COMMENTS" 
    inverse="true" fetch="select" lazy="false" order-by="commentId">
    <key>
    <column name="EXCEPTION_ID" not-null="true" />
    </key>
    <one-to-many class="com.beans.BatchExceptionComments" />
    </set>


    I believe ordering by Id will be better.



    P.S. I am using hbm.xml instead of annotation






    share|improve this answer

























      up vote
      1
      down vote



      accepted










      After some goofing around I found a solution.



      I just declared the set



      private Set<BatchExceptionComments> batchExceptionComments;


      Instead of using Comparable I used Order By to arrange the set.



       <set name="batchExceptionComments" table="BATCH_EXCEPTION_COMMENTS" 
      inverse="true" fetch="select" lazy="false" order-by="commentId">
      <key>
      <column name="EXCEPTION_ID" not-null="true" />
      </key>
      <one-to-many class="com.beans.BatchExceptionComments" />
      </set>


      I believe ordering by Id will be better.



      P.S. I am using hbm.xml instead of annotation






      share|improve this answer























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        After some goofing around I found a solution.



        I just declared the set



        private Set<BatchExceptionComments> batchExceptionComments;


        Instead of using Comparable I used Order By to arrange the set.



         <set name="batchExceptionComments" table="BATCH_EXCEPTION_COMMENTS" 
        inverse="true" fetch="select" lazy="false" order-by="commentId">
        <key>
        <column name="EXCEPTION_ID" not-null="true" />
        </key>
        <one-to-many class="com.beans.BatchExceptionComments" />
        </set>


        I believe ordering by Id will be better.



        P.S. I am using hbm.xml instead of annotation






        share|improve this answer












        After some goofing around I found a solution.



        I just declared the set



        private Set<BatchExceptionComments> batchExceptionComments;


        Instead of using Comparable I used Order By to arrange the set.



         <set name="batchExceptionComments" table="BATCH_EXCEPTION_COMMENTS" 
        inverse="true" fetch="select" lazy="false" order-by="commentId">
        <key>
        <column name="EXCEPTION_ID" not-null="true" />
        </key>
        <one-to-many class="com.beans.BatchExceptionComments" />
        </set>


        I believe ordering by Id will be better.



        P.S. I am using hbm.xml instead of annotation







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 at 14:37









        Ayush

        658




        658






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426073%2fsort-a-set-in-java%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks