https://kotlinlang.org logo
Title
b

bhavin

08/08/2019, 9:01 AM
How to compare Any type arraylist
var getVideoList: ArrayList<Any>
look at this my code https://paste.ofcode.org/PpCQuDjkAvtKpv7U29k7cD
d

diesieben07

08/08/2019, 9:03 AM
I am not sure what your question is. I am guessing
ActionHelp
does not implement
equals
and
hashCode
, which means
contains
and friends will use object identity. You should either implement
equals
and
hashCode
or make it a
data class
, which will generate those for you based on the component values.
b

bhavin

08/08/2019, 9:11 AM
@diesieben07 this is my ActionHelp class https://paste.ofcode.org/38JtXHaKMdDD3MFrrmfP6Xz I don't understand by implement
equals
g

gildor

08/08/2019, 9:12 AM
also runnable code snippet is very helpful for anyone who wants to help you
👍 1
d

diesieben07

08/08/2019, 9:14 AM
@bhavin Please actually clarify what your issue is.
b

bhavin

08/08/2019, 9:19 AM
ok I have an ArrayList with data which I get from another activity so I want to add new data in same list so problem is I have already some data with same id and I don't want to add same data in arraylist . I can do that If I add type in arraylist like
var getVideoList: ArrayList<ActionHelp>
but instead I'm using (i need to use )
var getVideoList: ArrayList<Any>
so I can't compare this list with ActionHelp class's data.
d

diesieben07

08/08/2019, 9:21 AM
The generic type you specify doesn't matter,
ArrayList<ActionHelp>
and
ArrayList<Any>
will behave exactly the same in regards to equality of their items. To me it sounds like you want a
Map<ID, ActionHelp>
b

bhavin

08/08/2019, 9:22 AM
I checked this https://stackoverflow.com/a/30022564/6333971 It's exactly like you said before @diesieben07 but I can't understand It😅 I am trying that right now
hello guys @diesieben07 @gildor I don't know how to do using
Map<ID, ActionHelp>
but I did Like this which is working right now ActionHelp Class : https://paste.ofcode.org/H8rg7Ly9vrTMVU4JXujcAc Small code snippet : https://paste.ofcode.org/BUB9ij6YzWMmJ5aB4kZQ7F please check It and suggest me better way 🙂
d

diesieben07

08/08/2019, 11:03 AM
@bhavin I am really not sure what you are trying to achieve there. Your
equals
method will not do anything useful. It classifies everything that is not the same object as "equal". Moreover, if you override
equals
you must override
hashCode
in coordination, the two methods always go together.
b

bhavin

08/08/2019, 11:13 AM
@diesieben07 What I'm trying to achieve is if Item is already in list than don't want to add it into list again
d

diesieben07

08/08/2019, 11:14 AM
Yes. You can either implement
equals
and
hashCode
accordingly (so that the same item is regarded as equal) or you can use a Map if you don't want to do that.
b

bhavin

08/08/2019, 11:17 AM
Yeah I get It but don't know how? that's problem😅 Thanks
d

diesieben07

08/08/2019, 11:18 AM
equals
needs to return true if you want the two objects (
this
and the parameter) to be equal.
b

bhavin

08/08/2019, 11:23 AM
@diesieben07 You are saying to do like this https://dzone.com/articles/working-with-hashcode-and-equals-in-java Right?
d

diesieben07

08/08/2019, 11:24 AM
Yes. Kotlin can do all that for you automatically if you use a
data class
b

bhavin

08/08/2019, 11:27 AM
yeah I read that but problem is I'm not using
List < Student > studentsLst = new ArrayList < Student > ();
I m use like this
List < Any > studentsLst = new ArrayList < Any > ();
so problem is when I try to do
studentsLst.contain
is not working
d

diesieben07

08/08/2019, 11:28 AM
It is completely irrelevant whether you use
List<Any>
or
List<Student>
equals
and
hashCode
are virtual (non-static) methods, they will be dispatched at runtime.
b

bhavin

08/08/2019, 11:58 AM
@diesieben07 Thanks and I did it using equals It's very simple and I wasted 2 hours .. sorry for waste your time and I'm thinking some time "kabhi kabhi to esa lagta he apun hi bhagvan hu" feel like dumb😅