list.contains does not work

*爱你&永不变心* 提交于 2020-08-20 12:41:29

问题


I am trying to develop a TS3 Bot in Java with this API: https://github.com/TheHolyWaffle/TeamSpeak-3-Java-API

I have a list with all Server Groups that a Client have:

List<ServerGroup> playerGroups = TS3Bot.api.getServerGroupsByClientId(player.clientdbID);

And now I check if the List contains a Group:

if(!playerGroups.contains(TS3Bot.botGroups.get(1))){...}

And the result is false. I am 100% sure that this List contains the ServerGroup.

Already checked it out with Sysouts.

Here is the Link to the ServerGroup Class: https://github.com/TheHolyWaffle/TeamSpeak-3-Java-API/blob/master/src/main/java/com/github/theholywaffle/teamspeak3/api/wrapper/ServerGroup.java

and this is just the ServerGroup Object.toString().

{iconid=0, savedb=1, sortid=0, name=Test 2, n_member_removep=100, sgid=98, type=1, n_member_addp=100, namemode=0, n_modifyp=100}

回答1:


Do not forget to respect the Object#equals(Object o) contract

Indicates whether some other object is "equal to" this one. The equals method implements an equivalence relation on non-null object references:

It is reflexive: for any non-null reference value x, x.equals(x) should return true.

It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.

It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.

It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.

For any non-null reference value x, x.equals(null) should return false. The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.



来源:https://stackoverflow.com/questions/35782238/list-contains-does-not-work

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!