Thread: Writing better Java

Page 6 of 13 FirstFirst ... 45678 ... LastLast
Results 51 to 60 of 129
  1. #51  


    Major's Avatar
    Join Date
    Jan 2011
    Posts
    2,997
    Thanks given
    1,293
    Thanks received
    3,556
    Rep Power
    5000
    Quote Originally Posted by lare96 View Post
    took the time out to read this, hopefully will help me improve my code

    question though, what does a 'good' hashcode implementation look like? I understand that if the Objects are equal then then the hashcodes must be equal as well, and I also understand that distinct hashcodes make hash collections perform better but I'm a bit reluctant to override the method for classes like Position

    Code:
    @Override
    public int hashCode() {
        return (x * y) + z;
    }
    would something like that be appropriate?


    tl;dr what's an appropriate hashcode and what isn't
    H3ll Ruler's post is sort-of correct (and you can learn that much from the Java tutorials page).

    What you pasted is a bad hash code implementation - consider the following two positions:

    Code:
    Position first = new Position(1, 0, 0);
    Position second = new Position(0, 1, 0);
    Both of those will have a hash code of 0. The default eclipse one (and afaik IDEA) would be:

    Code:
    public int hashCode() {
        final int prime = 31;
        int result = prime + x;
        result = prime * result + y;
        return prime * result + z;
    }
    This is reasonable (particularly because 31 is a prime), but could be better. When you know the maximum values, and if they are small enough, you can make the hash code perfect, which is desirable to eliminate collisions (which are slow).

    The height will only ever be 0-3, inclusive, so it will fit in 2 bits. This means we can fit the x and y values into 15 bits each (0-32,767), which is enough for RS coordinates.

    Code:
    public int hashCode() {
        return z << 30 & 0xC0000000 | y << 15 & 0x3FFF8000 | x & 0x7FFF;
    }
    This is perfect as long as 0 <= x <= 2^15, 0 <= y <= 2^15, and 0 <= z <= 3 (and also technically isn't a hash function given these conditions, but it doesn't matter).


    The reason the above post isn't quite correct is that there is an incorrect initial assumption:

    Quote Originally Posted by H3ll Ruler View Post
    2. if the objects are equal their hashcodes should also be equal.

    If these conditions are met then the hashcode will work.
    A premise of this is that it won't work if two different objects (as determined by equals(), not identity) have identical hash codes, when it will - HashTables are designed to deal with collisions (but if there is a collision, it'll probably be slower). Also, for most objects it'll be impossible to have a perfect hash function, because there is more variety than can fit into 32 bits (or it's infeasible to have a surjective function for the input). This is the case with e.g. String and they still work as keys.
    Last edited by Major; 09-16-2014 at 10:22 PM.
    Reply With Quote  
     

  2. Thankful users:


  3. #52  
    Registered Member waempie's Avatar
    Join Date
    Jul 2013
    Posts
    252
    Thanks given
    20
    Thanks received
    15
    Rep Power
    0
    WOW thank you!
    Reply With Quote  
     

  4. #53  
    ???

    funkE's Avatar
    Join Date
    Feb 2008
    Posts
    2,612
    Thanks given
    255
    Thanks received
    989
    Rep Power
    1366
    why can't hashcodes just be longs instead?
    .
    Reply With Quote  
     

  5. #54  
    Writing better Java



    Scu11's Avatar
    Join Date
    Aug 2007
    Age
    30
    Posts
    16,307
    Thanks given
    7,215
    Thanks received
    12,308
    Rep Power
    5000
    Quote Originally Posted by Supah Fly View Post
    why can't hashcodes just be longs instead?
    ...for what reason?

    Attached image
    Reply With Quote  
     

  6. #55  
    ???

    funkE's Avatar
    Join Date
    Feb 2008
    Posts
    2,612
    Thanks given
    255
    Thanks received
    989
    Rep Power
    1366
    Quote Originally Posted by Scu11 View Post
    ...for what reason?
    store more data?

    why is it an int?

    i am just curious about the reasons why things are the way they are and why they can't be something different (due to some design limitation, etc.)
    .
    Reply With Quote  
     

  7. #56  
    Writing better Java



    Scu11's Avatar
    Join Date
    Aug 2007
    Age
    30
    Posts
    16,307
    Thanks given
    7,215
    Thanks received
    12,308
    Rep Power
    5000
    Quote Originally Posted by Supah Fly View Post
    store more data?

    why is it an int?

    i am just curious about the reasons why things are the way they are and why they can't be something different (due to some design limitation, etc.)
    and give me a scenario in which you have that many collisions that you need a long instead of an int??

    Attached image
    Reply With Quote  
     

  8. #57  
    Banned

    Join Date
    May 2011
    Posts
    1,773
    Thanks given
    854
    Thanks received
    853
    Rep Power
    0
    This is realy nice. Never knew about ARM will make sure to study this thread
    Reply With Quote  
     

  9. #58  
    ???

    funkE's Avatar
    Join Date
    Feb 2008
    Posts
    2,612
    Thanks given
    255
    Thanks received
    989
    Rep Power
    1366
    Quote Originally Posted by Scu11 View Post
    and give me a scenario in which you have that many collisions that you need a long instead of an int??
    why have collisions at all if you have enough room to store your data
    .
    Reply With Quote  
     

  10. #59  
    Writing better Java



    Scu11's Avatar
    Join Date
    Aug 2007
    Age
    30
    Posts
    16,307
    Thanks given
    7,215
    Thanks received
    12,308
    Rep Power
    5000
    Quote Originally Posted by Supah Fly View Post
    why have collisions at all if you have enough room to store your data
    A hashmap basically does hashcode % size_of_map so using a long wont reduce collisions unless you have a giant map. Also the max number of buckets is 2^31 because of the limitations of java arrays and the purpose of a hash code is not to "store data".

    With regards to why it was done this way; when java was made most cpus were 32 bits so it made sense from a performance standpoint to use int for everything unless you really _needed_ 64 bits.

    Attached image
    Reply With Quote  
     

  11. Thankful user:


  12. #60  
    Registered Member Cloud_'s Avatar
    Join Date
    Feb 2014
    Posts
    21
    Thanks given
    6
    Thanks received
    1
    Rep Power
    24
    I have a question

    Code:
    Don't use singletons - they suck (pretty much impossible to unit test, hard to refactor, basically global which isn't OO, ...).
    I remember reading through one of grahams frameworks and he did the following

    Code:
    public static World getSingletion() {
       if(instance == null)
            instance = new World();//expensive object
    return instance;
    }
    Would it be possible for this to have a race condition of a few threads accessed this method simultaneously before it was instanced so lets say 4 threads did this at the same time
    Code:
     instance = new World();//expensive object
    Also do you have any advice on not doing this?
    Reply With Quote  
     

Page 6 of 13 FirstFirst ... 45678 ... LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Replies: 29
    Last Post: 09-29-2009, 06:25 PM
  2. Replies: 20
    Last Post: 07-31-2009, 11:19 AM
  3. Making a Java File Write Content on Websites
    By Nima304 in forum Application Development
    Replies: 2
    Last Post: 03-09-2009, 05:13 PM
  4. Replies: 4
    Last Post: 01-16-2009, 03:42 AM
  5. [503-PD] Better Theiving.java
    By Ian... in forum Configuration
    Replies: 4
    Last Post: 08-16-2008, 06:04 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •