RTFJD (the JavaDocs are your friends!) If you haven't read them in a long time, then RRTFJD (they might have changed!)
Of course it isn't:-Jesse Silverman wrote:. . . HashSet< HashSet<Type> > . . .
This isn't an exam forum. Who cares whether it is in the exam syllabus; it is in scope for this forum.Whether the hashCode() equals() contract is in scope for the 819 or not, I know I need to know it for decent programming . . .
You may wish to make two Cars equal if they have the same paintwork identification and maker. You might wish to ignore speed and direction. The important thing is that the alphabet of variables used to determine equality and to calculate a hash code be the same. (I think you may be able to get away with missing something out of the hash code, but not vice versa.). . . it was essential to include in both .equals() and .hashCode() overrides everything about an instance of its class. . . .
What's one of them when it's at home?an Equalatator object, . . .
The method in Object doesn't know whether its subclasses will have any fields or what they will be, so it can only use identity to determine equality: obj == this. What's “dumb‑dumb” about that? The idea is that subclass writers will know which fields they want to compare for equality, and they can follow the general contract. Easy. (Actually it isn't at all easy.). . . notion of identity equality with respect to this class. Not Object's dumb-dumb idea of whether they both point to the same heap instance . . .
Yes, that agrees with what I said earlier. This is what it says in the documentation for Set:-My prior interpretation of the latter had me feeling "Only immutable objects may ever safely be placed in HashSet' or as keys in HashMaps!
The API for Set wrote:Note: Great care must be exercised if mutable objects are used as set elements.
That is a contradiction in terms.JS wrote:Even then, we need to be very careful because anything that would 'change' an immutable object requires a removal and re-add in that Set or Map which the code making the change hopefully knows about." . . .
All the Collection framework classes are written on the assumption that equals() and hashCode() are implemented correctly. Otherwise you would have to write your own collections classes to accommodate different implementations."Where else am I contractually bound to making use of .equals() or .hashCode() apart from the Hash-based data structures?" . . . we are constrained to have that single definition of .equals(Object o) be used in all those places to be identical to that utilized for purposes of hashSet or hashMap key identity.
The collections framework classes have no way of “knowing” whether its elements have different methods for testing equality. They can only be sure about equals() and hashCode(). Unless you want to write your own specifications and implementations.It feels like we might want to have a totallyEquals() or reallyEquals() that checks values of all state variables in cases we care . . . But wait, .retainAll(), removeAll(), replaceAll() methods would just blindly follow equals() . . .
Guilty until proven innocent, or innocent until proven guilty? Winston follows one approach: equal until proven different, but the standard Java® approach assumes different until proven equal. Yes, you can write your own language under different assumptions, but that is the way Java® does it.. . . Winston's "Road to Equality" . . . .
RTFJD (the JavaDocs are your friends!) If you haven't read them in a long time, then RRTFJD (they might have changed!)
RTFJD (the JavaDocs are your friends!) If you haven't read them in a long time, then RRTFJD (they might have changed!)
I might be mistaken, but I suspect they ran out of time or money when Java1.0.2 was issued nearly twenty‑four years ago. Generics was well known back then. That must have produced lots of complaints about type‑unsafety of collections, and they started considering generics pretty quickly after the Collections Framework. Unfortunately, they couldn't work out how to combine generics with reification and backward compatibility, so they had to use erasure. The existence of legacy code means it isn't possible to ban non‑generic use of objects. There were promises that raw types in a generic context would be banned in Java6, but I have never seen that prediction fulfilledJesse Silverman wrote:. . . the Collections Framework pre-dated . . . Java 5 and its generics. . . .
People didn't know there were default methods to wait for. OO programming was all the rage in the 1990s, and nobody thought that a simpler way of threading (=parallel Streams) or functional features (e.g. λs) would become popular. Not until fifteen years later, when Java® looked old‑fashioned and needed dragging kicking and screaming in to the 21st century. Oracle devoted a great deal of effort to that, and it took much longer than they originally anticipated.. . . people couldn't wait for . . . Java 8 and its default methods . . .
Yes. That is quite possible. I don't think it is going to happen, though. It would mean adding methods, overloaded or with new method names, to the current interfaces, and that would entail more default methods. If you want that sort of collection, you will have to write your own, or maybe there is a different language that supports that concept.. . . it at least seems reasonable that some (all?) of the Collections-based stuff could potentially be retro-fitted with overloads that would let you say "what exactly do we mean by equals for this operation or data structure being assembled??"
You mean the decision Gosling made for equals() thinking, surely.. . . the decision you make for equals() thinking . . .
I always thought that point was correct.I still think my idea that "You can only safely put immutable objects into hash-based data structures" was wrong, at least trivially.
Whenever you write a class, you decide which fields you are going to include in and exclude from equals() and hashCode(). Look at my Car example where I excluded the speed and direction fields. What if you have have a blue Car and a second Car object returning true from equals() compared to that Car? What if you respray one of them in that nice metallic paint where two colours shade into each other? Now the two will no longer be equal. Or if you change the registration to JS 1. Since OO (=object‑oriented) programming has no way to make fields in multiple instances distinct from one another, you are now going to get false from equals(). If you go with the traditional interpretation of what OO means, you will have mutable fields and there is no guarantee that two objects will remain equal to each other (or vice versa) ever. If you are going to avoid mutable fields in equals() and hashCode(), then why override those methods on mutable types in the first place? If Odersky Spoon and Venners had written in 2019 rather than 2009, they might have introduced a more recent concept in OO, and told their readers to make every type immutable, in which case any change entails creating a new object.Oderskey et al, point 3 "Don't include mutable state in your .hashCode() and .equals() overrides", if followed, means you are free to happily mutate anything left out at will it seems.
I think I have just spent twenty minutes explaining why I am going to agree with you. . . "if you change any mutable state . . . only store totally immutable objects everywhere. . . .
I tried it. Wouldn't compile.When I referred to "changing an immutable object" . . . calling ++ on an Integer that is also stored in a Set . . .
“Program to the interface.” the concept may be right, but the name entails an implementation. Better to write Set. I can't think of any Sets that don't use some sort of comparison between elements because they need to verify that their elements are distinct.I think that my HashSet< HashSet<String> > case would have been valid . . .
But you are thinking out those implications. . . hadn't thought out the full global implications . . .
People didn't know there were default methods to wait for.
RTFJD (the JavaDocs are your friends!) If you haven't read them in a long time, then RRTFJD (they might have changed!)
Monica Shiralkar wrote:
People didn't know there were default methods to wait for.
What does this mean?
RTFJD (the JavaDocs are your friends!) If you haven't read them in a long time, then RRTFJD (they might have changed!)
RTFJD (the JavaDocs are your friends!) If you haven't read them in a long time, then RRTFJD (they might have changed!)
Caution should be exercised when using a comparator capable of imposing an ordering inconsistent with equals to order a sorted set (or sorted map). Suppose a sorted set (or sorted map) with an explicit comparator c is used with elements (or keys) drawn from a set S. If the ordering imposed by c on S is inconsistent with equals, the sorted set (or sorted map) will behave "strangely." In particular the sorted set (or sorted map) will violate the general contract for set (or map), which is defined in terms of equals.
For example, suppose one adds two elements a and b such that (a.equals(b) && c.compare(a, b) != 0) to an empty TreeSet with comparator c. The second add operation will return true (and the size of the tree set will increase) because a and b are not equivalent from the tree set's perspective, even though this is contrary to the specification of the Set.add method.
RTFJD (the JavaDocs are your friends!) If you haven't read them in a long time, then RRTFJD (they might have changed!)
Jesse Silverman wrote:
Hi Monica:
I meant that because prior to Java 8 giving Sun/Oracle (and everyone else!) the ability to place default methods and static methods in interfaces, you could only place them in classes.
If you needed a method that seemed logically to belong to an interface, you needed to either push that method down to some (possibly abstract) class that implemented it, or to make a helper/Utility class to contain it.
It seems weird now to see all the List-specific methods all over the Collections utility class for example -- we could just place them as static or default methods in the List interface where people would expect them, right?
But they were needed, and had to be put somewhere, for years before that was a possibility. So they live in Collections which sounds like a utility class for things that are common to all Collection classes, but instead has a bunch of things in it that are only applicable to *some* Collection objects.
RTFJD (the JavaDocs are your friends!) If you haven't read them in a long time, then RRTFJD (they might have changed!)
Not Oracle, but Sun. That was a long time before Oracle. Not won't but can't. They tried hard to fix it. I still think the real problem was not impementing generics from the word “go”.Jesse Silverman wrote:. . . a bug that Oracle can't/won't fix due to reverse compatibility. . . .
RTFJD (the JavaDocs are your friends!) If you haven't read them in a long time, then RRTFJD (they might have changed!)
RTFJD (the JavaDocs are your friends!) If you haven't read them in a long time, then RRTFJD (they might have changed!)
That is simply an Equalizer<E> with a very long formal type parameter. It “knows” nothing about the Employee class.Jesse Silverman wrote:. . . an Equalitator<Employee> or an Equalitator<Dog>
Yes.. . . an Equalitator<? extends Employee> perhaps? . . .
Don't understand. All @Override does is confirm that you are overriding the method. It doesn't affect the logic or implementation or anything. There is no need to be very careful using @Override; if you put it in the wrong place you will suffer a compiler error and no harm will ensue.your choice of @Override for .equals(Object o) when defining a new class affects a whole lot, and had best be made pretty carefully. . . .
RTFJD (the JavaDocs are your friends!) If you haven't read them in a long time, then RRTFJD (they might have changed!)
Don't worry; I can split this thread whenever I have the time and energy to decide which posts to split. I agree; your part of the discussion merits a thread of its own.Jesse Silverman wrote:. . . I now feel like I've carjacked Monica's nice thread. . . .
Jesse Silverman wrote:
I now feel like I've carjacked Monica's nice thread.
Jesse Silverman wrote:a lot of people seem to not use it because it is expensive or fattening or they tax it in some states.
I really don't know why the people who avoid it avoid it, those are just guesses.
Campbell Ritchie wrote:Eclipse adds @Override when it detects overriding.
RTFJD (the JavaDocs are your friends!) If you haven't read them in a long time, then RRTFJD (they might have changed!)
RTFJD (the JavaDocs are your friends!) If you haven't read them in a long time, then RRTFJD (they might have changed!)
Campbell Ritchie wrote:
That is simply an Equalizer<E> with a very long formal type parameter. It “knows” nothing about the Employee class.Jesse Silverman wrote:. . . an Equalitator<Employee> or an Equalitator<Dog>
Yes... . . an Equalitator<? extends Employee> perhaps? . . .
RTFJD (the JavaDocs are your friends!) If you haven't read them in a long time, then RRTFJD (they might have changed!)
RTFJD (the JavaDocs are your friends!) If you haven't read them in a long time, then RRTFJD (they might have changed!)
RTFJD (the JavaDocs are your friends!) If you haven't read them in a long time, then RRTFJD (they might have changed!)
Immutable Sequence Types
The only operation that immutable sequence types generally implement that is not also implemented by mutable sequence types is support for the hash() built-in.
This support allows immutable sequences, such as tuple instances, to be used as dict keys and stored in set and frozenset instances.
Attempting to hash an immutable sequence that contains unhashable values will result in TypeError.
RTFJD (the JavaDocs are your friends!) If you haven't read them in a long time, then RRTFJD (they might have changed!)
You don't like waffles? Well, do you like this tiny ad?
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
|