Official 'what is Dota anymore' discussion - Page 10042
| Forum Index > Dota 2 General |
|
PoulsenB
Poland7754 Posts
| ||
|
Firebolt145
Lalalaland34505 Posts
| ||
|
PoulsenB
Poland7754 Posts
| ||
|
Faruko
Chile34173 Posts
Damn the guy is fucking good, wtf. Edit: He fed | ||
|
teddyoojo
Germany22369 Posts
+ Show Spoiler + public class Thing{ public int hashCode(){ return name.hashCode(); } like whats the best way for this method, which is supposed to check if parameter Thing is in the arraylist, to look? public boolean isInTheBox(Thing thing){} (no idea if that is understandable in any way) | ||
|
Nevuk
United States16280 Posts
Easiest way to check if something is in an arraylist is the arraylist.contains(thing) method, with streams as the second easiest. What does "best" mean here? Fastest, fewest lines? | ||
|
teddyoojo
Germany22369 Posts
if i return this.hashcode() then theres no point overwriting the hashcode method also it should compare only based on the name therefore the overwriting hashcode method its hard to explain without showing the relevant parts IDK | ||
|
Nevuk
United States16280 Posts
CurrentAccount conta5 = new CurrentAccount("João Lopes", 3135); boolean itemExists = lista.stream().anyMatch(c -> c.equals(conta5)) https://stackoverflow.com/questions/4404084/check-if-a-value-exists-in-arraylist If you can't use java 8 then yeah, you have to overload hashcode and equals methods. Very common task though, lots of examples. | ||
|
teddyoojo
Germany22369 Posts
| ||
|
Nevuk
United States16280 Posts
On May 12 2019 08:58 teddyoojo wrote: well i understand that method but i just want to know why my arraylist.contains(obj) WONT RETURN TRUE T_T_T_T_T_T_T_T_T_ It is because it uses the .equals method underneath the code. This works really well for int and strings, but not for user defined objects. See here : https://www.geeksforgeeks.org/difference-equals-method-java Edit: Basically it is comparing their location in memory rather than their value if it using the default .equals method. | ||
|
teddyoojo
Germany22369 Posts
| ||
|
Nevuk
United States16280 Posts
On May 12 2019 09:27 teddyoojo wrote: but i overloaded the equals method aswell to only take name into consideration How did you overload it? You would need to override the equals method with the @Override command. In the post earlier you only had the hashcode method. Overriding the hashcode is only a small performance boost after you've overridden the equals method (so there's no reason to ever do it on a modern computer). Well, or if you are using hash lists like hashmap or hashsets. Your hashcode has to return an int. You can't just do it on the name string, as it can return different values. There are a lot of ways to override it, but usually it involves creating an int as part of the object definition and using this to generate the hashcode. | ||
|
teddyoojo
Germany22369 Posts
| ||
|
Nevuk
United States16280 Posts
On May 12 2019 10:14 teddyoojo wrote: oh my i just noticed that the equals method doesnt override why doesnt it override ??? At least on eclipse you just have to add @Override before the class definition. Like this: @Override public boolean equals(Object object) { boolean sameSame = false; if (object != null && object instanceof Thing) { sameSame = this.value == ((Thing) object).value; } return sameSame; } | ||
|
teddyoojo
Germany22369 Posts
| ||
|
Nevuk
United States16280 Posts
| ||
|
teddyoojo
Germany22369 Posts
thanks tho nevuk | ||
|
rabidch
United States20289 Posts
| ||
|
Artisreal
Germany9235 Posts
Watched most of it while doing housework and two of the last 3 games on my newly wall mounted monitor in my comfy chair with a beer. That was great! Let's go liquid! Secret seems too strong though! | ||
|
PoulsenB
Poland7754 Posts
| ||
| ||