list.contains不起作用 [英] list.contains does not work

查看:527
本文介绍了list.contains不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下API用Java开发TS3 Bot: https:// github.com/TheHolyWaffle/TeamSpeak-3-Java-API



我有一个客户端拥有的所有服务器组的列表:

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

现在我检查列表中是否包含组:

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

结果为假。我100%确定此列表包含ServerGroup。



已使用Sysouts对其进行检出。



此处是到ServerGroup类的链接: https://github.com/TheHolyWaffle/TeamSpeak-3-Java-API/blob/master/src/main/java/com/github/theholywaffle/teamspeak3/api /wrapper/ServerGroup.java



这就是ServerGroup Object.toString()。

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


解决方案

不要忘记尊重 Object#equals(Object o) contract


指示其他某个对象是否等于。这个。 equals
方法在非空对象
引用上实现等价关系:


自反:对于任何非空引用值x,
x.equals(x)应该返回true。


它是对称的:对于任何非空
参考值x和y,如果以下情况,x.equals(y)应该返回true:


它是可传递的:对于任何非空的
参考值x,y和z,如果x.equals(y)返回true,而
y.equals(z)返回true,则x.equals(z)应该返回true。


这是一致的:对于任何非空参考值x和y,x.equals(y)的多个
调用一致地返回true或一致地
返回false,只要没有修改对象的
的相等比较中使用的信息


对于任何非空参考值x,
x.equals(null)应该返回false。类Object
的equals方法在
对象上实现了最有区别的对等关系。也就是说,对于任何非空引用值x和y,当且仅当x和y引用同一对象(x
== y的值为true)时,此
方法才返回true。 / p>

请注意,通常每当覆盖此方法时,都必须覆盖hashCode方法,例如
维护hashCode方法的常规协定,其中指出
相等的对象必须具有相等的哈希码。



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}

解决方案

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.

这篇关于list.contains不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆