kevin Abel wrote:I looked at the API for HashSet. All I can tell is that it is a collection of objects.
My understanding is that the HashCode is inherited from Class Object. I'm guessing each Object is created with one that has not been allocated before.
Regards Pete
It is NOT unique except by chance.Peter Rooke wrote:Hashes are one-way and collision resistant, meaning that an input value will generate an output that is a fixed length and unique.
Here's Autin Griffith explaining the properties of hash functions.
The number of different hash codes depends on what datatype they are defined in. CRC codes and SHA256 are all kinds of hash code with their results of different “sizes”. 65535 (=2¹⁶ − 1) is the maximum size of a 16‑bit unsigned binary number, but since 0 is a permissible hash code result, a 16‑bit hash code has 65536 possible different values. Java® uses an int as the return type from hashCode(), and that has 2³² (=4,294,967,296) potential distinct values. That still means the possible number of Strings is large; it is too large for even BigInteger to calculate.Paul Clapham wrote:. . . there are only 65,535 possible hash codes. . . .
kevin Abel wrote:I think that each object gets its own unique HashCode
Peter Rooke wrote:Hashes are one-way and collision resistant
S Fox wrote: i think the set interface you'd probably never need in your entire life
S Fox wrote:yeah i meant all you really need is HashSet and TreeSet, i have never needed to implement the set interface itself to create a custom type of set.
S Fox wrote:take a list and run it thru a for loop, append each item to a set
i think there is also a convenience method in collections to add everything to a set
i think the set interface you'd probably never need in your entire life.
im not too sure how java decides if 2 objects are equal, but you can override their default behavior with your own equality operator as needed.
I thought that had already been explained in this thread.S Fox wrote:. . . im not too sure how java decides if 2 objects are equal
That is very imprecise. You cannot override operators in Java® at all. You have to override methods.but you can override their default behavior with your own equality operator . . .
I don't think OP knows about strcmp. Although String#equals() and strcmp can both tell you whether two bits of text would read the same, they are different. For a start, a Java® String is a full‑blown object, whereas text in C can sort of degenerate to a pointer to its first character. It even says in the Java® Language Specification (=JLS) that the two are different datatypes. I don't think that C supports strings as a distinct datatype, but uses char[]s instead. Remember too, the char datatype in Java® is different from the similarly named datatype in C.. . . the equals() method is basically the same as strcmp in C
There is no “probably” about it. The behaviour of == is strictly defined in Java®.. . .== is probably just comparing the memory address of 2 objects to see if they're the same. . . .
What Stephan means, is that an un‑overridden equals() method uses == as a default, as that link will tell you.Stephan van Hulst wrote:. . . For objects that don't have their equals() method overriden, Java just uses the == operator . . .
Campbell wrote:I think you need to revise some maths before looking at those classes; find out what the difference between a List (sometimes called a sequence) and a Set is.
Carey Brown wrote:Just to see how a hash set might be implemented.
kevin Abel wrote:I worked with data sets in BASIC and VBA although at the time didn't know they were called sets.
I remember seeing link-lists in C so I know what lists are. It's items stored as one long amount of items. There is no random access.
Please tell us why the & operator (=bitwise AND) might be better than the remainder operator (%).Stephan van Hulst wrote:. . . the % operator, not the & operator.
Stephan wrote:Lists are always ordered, sets might or might not.
kevin Abel wrote:When I first read "concrete implementation" my thoughts jumped to interfaces and something to do with concrete. I don't think it means that in your last post.
It means, if you make a wall out of it and bang your head against it, it hurts a lot more than banging your head against an abstract wall.kevin Abel wrote:. . . "concrete implementation" . . . I don't think it means that in your last post. . . .
greg bowers wrote:HashCode:
... It returns an integer value that represents the object's memory address or a unique identifier generated by the object's contents.
This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the Java™ programming language.)
Only in old versions. The latest version for JDK20 says something different. If you go back to Java11, it does mention memory:-Junilu Lacar wrote:. . . The API documentation for Object.hashCode() says this . . .
Even Java7 hedges its bets:-(The hashCode may or may not be implemented as some function of an object's memory address at some point in time.)
(This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)
Don't get me started about those stupid light bulbs. |