How to compare Any type arraylist `var getVideoLis...
# announcements
b
How to compare Any type arraylist
var getVideoList: ArrayList<Any>
look at this my code https://paste.ofcode.org/PpCQuDjkAvtKpv7U29k7cD
d
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
@diesieben07 this is my ActionHelp class https://paste.ofcode.org/38JtXHaKMdDD3MFrrmfP6Xz I don't understand by implement
equals
g
also runnable code snippet is very helpful for anyone who wants to help you
👍 1
d
@bhavin Please actually clarify what your issue is.
b
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
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
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
@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
@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
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
Yeah I get It but don't know how? that's problem😅 Thanks
d
equals
needs to return true if you want the two objects (
this
and the parameter) to be equal.
b
@diesieben07 You are saying to do like this https://dzone.com/articles/working-with-hashcode-and-equals-in-java Right?
d
Yes. Kotlin can do all that for you automatically if you use a
data class
b
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
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
@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😅