Java collection set for user defined types with unique key strings










-1















I was wondering is there a collection that I can use for a class Weapon where it's like a hash_set of Weapon objects, but I want to use a name field in my weapon class and make it so that no two Weapons with the same name can exist in the set. I want to compare inputs with strings of my Weapon set to see if it already exists, and if it does, don't put it in the set, but I don't want to search the entire map every single time. Also, it wouldn't be case sensitive.










share|improve this question

















  • 1





    Use a HashSet and override the equals() method so that the two objects will be considered equal if the name property is the same

    – GBlodgett
    Nov 15 '18 at 3:19







  • 1





    @GBlodgett don't forget also to override hashCode().

    – Gene
    Nov 15 '18 at 3:29












  • "Searching the entire map" organized by weapon's name is an O(1) operation -- it's very fast even for thousands of weapons. If you have a dozen of weapons or so, you could use an array list and search it linearly; it would be pretty fast as well.

    – dasblinkenlight
    Nov 15 '18 at 3:30











  • @Gene Yes that too

    – GBlodgett
    Nov 15 '18 at 3:30















-1















I was wondering is there a collection that I can use for a class Weapon where it's like a hash_set of Weapon objects, but I want to use a name field in my weapon class and make it so that no two Weapons with the same name can exist in the set. I want to compare inputs with strings of my Weapon set to see if it already exists, and if it does, don't put it in the set, but I don't want to search the entire map every single time. Also, it wouldn't be case sensitive.










share|improve this question

















  • 1





    Use a HashSet and override the equals() method so that the two objects will be considered equal if the name property is the same

    – GBlodgett
    Nov 15 '18 at 3:19







  • 1





    @GBlodgett don't forget also to override hashCode().

    – Gene
    Nov 15 '18 at 3:29












  • "Searching the entire map" organized by weapon's name is an O(1) operation -- it's very fast even for thousands of weapons. If you have a dozen of weapons or so, you could use an array list and search it linearly; it would be pretty fast as well.

    – dasblinkenlight
    Nov 15 '18 at 3:30











  • @Gene Yes that too

    – GBlodgett
    Nov 15 '18 at 3:30













-1












-1








-1








I was wondering is there a collection that I can use for a class Weapon where it's like a hash_set of Weapon objects, but I want to use a name field in my weapon class and make it so that no two Weapons with the same name can exist in the set. I want to compare inputs with strings of my Weapon set to see if it already exists, and if it does, don't put it in the set, but I don't want to search the entire map every single time. Also, it wouldn't be case sensitive.










share|improve this question














I was wondering is there a collection that I can use for a class Weapon where it's like a hash_set of Weapon objects, but I want to use a name field in my weapon class and make it so that no two Weapons with the same name can exist in the set. I want to compare inputs with strings of my Weapon set to see if it already exists, and if it does, don't put it in the set, but I don't want to search the entire map every single time. Also, it wouldn't be case sensitive.







java hash set






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 3:18









Shinji-sanShinji-san

474615




474615







  • 1





    Use a HashSet and override the equals() method so that the two objects will be considered equal if the name property is the same

    – GBlodgett
    Nov 15 '18 at 3:19







  • 1





    @GBlodgett don't forget also to override hashCode().

    – Gene
    Nov 15 '18 at 3:29












  • "Searching the entire map" organized by weapon's name is an O(1) operation -- it's very fast even for thousands of weapons. If you have a dozen of weapons or so, you could use an array list and search it linearly; it would be pretty fast as well.

    – dasblinkenlight
    Nov 15 '18 at 3:30











  • @Gene Yes that too

    – GBlodgett
    Nov 15 '18 at 3:30












  • 1





    Use a HashSet and override the equals() method so that the two objects will be considered equal if the name property is the same

    – GBlodgett
    Nov 15 '18 at 3:19







  • 1





    @GBlodgett don't forget also to override hashCode().

    – Gene
    Nov 15 '18 at 3:29












  • "Searching the entire map" organized by weapon's name is an O(1) operation -- it's very fast even for thousands of weapons. If you have a dozen of weapons or so, you could use an array list and search it linearly; it would be pretty fast as well.

    – dasblinkenlight
    Nov 15 '18 at 3:30











  • @Gene Yes that too

    – GBlodgett
    Nov 15 '18 at 3:30







1




1





Use a HashSet and override the equals() method so that the two objects will be considered equal if the name property is the same

– GBlodgett
Nov 15 '18 at 3:19






Use a HashSet and override the equals() method so that the two objects will be considered equal if the name property is the same

– GBlodgett
Nov 15 '18 at 3:19





1




1





@GBlodgett don't forget also to override hashCode().

– Gene
Nov 15 '18 at 3:29






@GBlodgett don't forget also to override hashCode().

– Gene
Nov 15 '18 at 3:29














"Searching the entire map" organized by weapon's name is an O(1) operation -- it's very fast even for thousands of weapons. If you have a dozen of weapons or so, you could use an array list and search it linearly; it would be pretty fast as well.

– dasblinkenlight
Nov 15 '18 at 3:30





"Searching the entire map" organized by weapon's name is an O(1) operation -- it's very fast even for thousands of weapons. If you have a dozen of weapons or so, you could use an array list and search it linearly; it would be pretty fast as well.

– dasblinkenlight
Nov 15 '18 at 3:30













@Gene Yes that too

– GBlodgett
Nov 15 '18 at 3:30





@Gene Yes that too

– GBlodgett
Nov 15 '18 at 3:30












2 Answers
2






active

oldest

votes


















3














In your equals(), check only the name field for equality.



public class Weapon 

private String name;
// ... other fields

@Override
public boolean equals(Object o) getClass() != o.getClass()) return false;

Weapon weapon = (Weapon) o;

return name.equalsIgnoreCase(weapon.name); //case-insensitive


@Override
public int hashCode()
return name.toLowerCase().hashCode();




And then use your HashSet like so:-



public static void main(String args) 
Set<Weapon> weapons = new HashSet<>();
Weapon w1 = new Weapon();
// TODO set the name of the weapon or pass it in constructor
if (weapons.add(w1))
System.out.println("Weapon added");
else
System.out.println("Weapon with same name already exists");







share|improve this answer
































    1














    We have to override equals() and hashcode() methods to return the same interger for name.



    Below is the equals() and hashcode () contract:



    See JavaDoc of java.lang.Object



    In hashCode() it says:



    If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.



    If you only override equals() and not hashCode() your class violates this contract.



    This is also said in the JavaDoc of the equals() method:



    Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.






    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',
      autoActivateHeartbeat: false,
      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%2f53311904%2fjava-collection-set-for-user-defined-types-with-unique-key-strings%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









      3














      In your equals(), check only the name field for equality.



      public class Weapon 

      private String name;
      // ... other fields

      @Override
      public boolean equals(Object o) getClass() != o.getClass()) return false;

      Weapon weapon = (Weapon) o;

      return name.equalsIgnoreCase(weapon.name); //case-insensitive


      @Override
      public int hashCode()
      return name.toLowerCase().hashCode();




      And then use your HashSet like so:-



      public static void main(String args) 
      Set<Weapon> weapons = new HashSet<>();
      Weapon w1 = new Weapon();
      // TODO set the name of the weapon or pass it in constructor
      if (weapons.add(w1))
      System.out.println("Weapon added");
      else
      System.out.println("Weapon with same name already exists");







      share|improve this answer





























        3














        In your equals(), check only the name field for equality.



        public class Weapon 

        private String name;
        // ... other fields

        @Override
        public boolean equals(Object o) getClass() != o.getClass()) return false;

        Weapon weapon = (Weapon) o;

        return name.equalsIgnoreCase(weapon.name); //case-insensitive


        @Override
        public int hashCode()
        return name.toLowerCase().hashCode();




        And then use your HashSet like so:-



        public static void main(String args) 
        Set<Weapon> weapons = new HashSet<>();
        Weapon w1 = new Weapon();
        // TODO set the name of the weapon or pass it in constructor
        if (weapons.add(w1))
        System.out.println("Weapon added");
        else
        System.out.println("Weapon with same name already exists");







        share|improve this answer



























          3












          3








          3







          In your equals(), check only the name field for equality.



          public class Weapon 

          private String name;
          // ... other fields

          @Override
          public boolean equals(Object o) getClass() != o.getClass()) return false;

          Weapon weapon = (Weapon) o;

          return name.equalsIgnoreCase(weapon.name); //case-insensitive


          @Override
          public int hashCode()
          return name.toLowerCase().hashCode();




          And then use your HashSet like so:-



          public static void main(String args) 
          Set<Weapon> weapons = new HashSet<>();
          Weapon w1 = new Weapon();
          // TODO set the name of the weapon or pass it in constructor
          if (weapons.add(w1))
          System.out.println("Weapon added");
          else
          System.out.println("Weapon with same name already exists");







          share|improve this answer















          In your equals(), check only the name field for equality.



          public class Weapon 

          private String name;
          // ... other fields

          @Override
          public boolean equals(Object o) getClass() != o.getClass()) return false;

          Weapon weapon = (Weapon) o;

          return name.equalsIgnoreCase(weapon.name); //case-insensitive


          @Override
          public int hashCode()
          return name.toLowerCase().hashCode();




          And then use your HashSet like so:-



          public static void main(String args) 
          Set<Weapon> weapons = new HashSet<>();
          Weapon w1 = new Weapon();
          // TODO set the name of the weapon or pass it in constructor
          if (weapons.add(w1))
          System.out.println("Weapon added");
          else
          System.out.println("Weapon with same name already exists");








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 '18 at 3:53

























          answered Nov 15 '18 at 3:28









          KartikKartik

          4,45731537




          4,45731537























              1














              We have to override equals() and hashcode() methods to return the same interger for name.



              Below is the equals() and hashcode () contract:



              See JavaDoc of java.lang.Object



              In hashCode() it says:



              If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.



              If you only override equals() and not hashCode() your class violates this contract.



              This is also said in the JavaDoc of the equals() method:



              Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.






              share|improve this answer



























                1














                We have to override equals() and hashcode() methods to return the same interger for name.



                Below is the equals() and hashcode () contract:



                See JavaDoc of java.lang.Object



                In hashCode() it says:



                If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.



                If you only override equals() and not hashCode() your class violates this contract.



                This is also said in the JavaDoc of the equals() method:



                Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.






                share|improve this answer

























                  1












                  1








                  1







                  We have to override equals() and hashcode() methods to return the same interger for name.



                  Below is the equals() and hashcode () contract:



                  See JavaDoc of java.lang.Object



                  In hashCode() it says:



                  If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.



                  If you only override equals() and not hashCode() your class violates this contract.



                  This is also said in the JavaDoc of the equals() method:



                  Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.






                  share|improve this answer













                  We have to override equals() and hashcode() methods to return the same interger for name.



                  Below is the equals() and hashcode () contract:



                  See JavaDoc of java.lang.Object



                  In hashCode() it says:



                  If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.



                  If you only override equals() and not hashCode() your class violates this contract.



                  This is also said in the JavaDoc of the equals() method:



                  Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 4:00









                  Karthik PKarthik P

                  336




                  336



























                      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.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53311904%2fjava-collection-set-for-user-defined-types-with-unique-key-strings%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

                      Use pre created SQLite database for Android project in kotlin

                      Darth Vader #20

                      Ondo