java contains是什么意思(java中list集合中contains()的用法,啥意思)
各位老铁们,大家好,今天由我来为大家分享java contains是什么意思,以及java中list集合中contains()的用法,啥意思的相关问题知识,希望对大家有所帮助。如果可以帮助到大家,还望关注收藏下本站,您的支持是我们最大的动力,谢谢大家了哈,下面我们开始吧!
java中list集合中contains()的用法,啥意思
1、java中list集合中contains()()的用法为:
public boolean list.contains(Object o)
意思为:当前列表若包含某元素,返回结果为true,若不包含该元素,返回结果为false。
2、contains()方法实现的具体细节为:
当list调用contains()方法并传递一个元素时,会执行遍历,逐个对比item是否等于该元素,当遍历结束后,如果还没有一个元素等于该元素的值,则返回false,否则返回true
3、java中list集合中contains()用法的代码示例为:
import java.util.ArrayList;
public class ArrayListDemo{
public static void main(String[] args){
//创建1个空列表
ArrayList<Integer> arrlist= new ArrayList<Integer>();
//添加测试数据
arrlist.add(20);
arrlist.add(25);
arrlist.add(10);
arrlist.add(15);
//列表是否包含元素 10
boolean res= arrlist.contains(10);
if(res== true){
System.out.println("包含");
} else{
System.out.println("不包含");
}}}
扩展资料:
1、List的contains(obj)方法
实际上,List调用contains(Object obj)方法时,会遍历List中的每一个元素,然后再调用每个元素的equals()方法去跟contains()方法中的参数进行比较,如果有一个元素的equals()方法返回true则contains()方法返回true,否则所有equals()方法都不返回true,则ontains()方法则返回false。因此,重写了Course类的equals()方法,否则,testListContains()方法的第二条输出为false。
2、Set的Contains(obj)方法
当调用HashSet的contains(Object obj)方法时,其实是先调用每个元素的hashCode()方法来返回哈希码,如果哈希码的值相等的情况下再调用equals(obj)方法去判断是否相等,只有在这两个方法所返回的值都相等的情况下,才判定这个HashSet包含某个元素。因此,需重写Course类的hashCode()方法和equals()方法。
参考资料来源:Java官方文档-Interface List
java的equals和contains的区别
java的equals和contains()的区别:
1、功能不同
equals指示其他某个对象是否与此对象“相等”。
contains当且仅当此字符串包含指定的 char值序列时,返回 true。
2、用法不同
equals实现对象上差别可能性最大的相等关系;即,对于任何非空引用值 x和 y,当且仅当 x和 y引用同一个对象时,此方法才返回 true(x== y具有值 true)。
contains常用与集合中判断某个对象是否含有这个元素。
3、含义不同
equals是比较两个东西是否等同,适用于任何类别的对象。
contains是是否包含的意思,左边的对象是一个容器了。
4、参数不同
equals参数为obj-要与之比较的引用对象。
contains参数为s-要搜索的序列。
参考资料来源:百度百科——equals
百度百科——contains()
java (String) s.peek()是什么意思
s.peek()表示的是查看堆栈顶部的对象,但不从堆栈中移除它。
除此之外:
push(E item)表示的是把项压入堆栈顶部。
pop()表示的是移除堆栈顶部的对象,并作为此函数的值返回该对象。
empty()表示的是测试堆栈是否为空。
search(Object o)表示的是返回对象在堆栈中的位置,以 1为基数。
以下是从jdk中拿下来的相关方法的源码,可以参看下:
publicclassStack<E>extendsVector<E>{
/**
*CreatesanemptyStack.
*/
publicStack(){
}
/**
*Pushesanitemontothetopofthisstack.Thishasexactly
*thesameeffectas:
*<blockquote><pre>
*addElement(item)</pre></blockquote>
*
*@paramitemtheitemtobepushedontothisstack.
*@returnthe<code>item</code>argument.
*@seejava.util.Vector#addElement
*/
publicEpush(Eitem){
addElement(item);
returnitem;
}
/**
*Removestheobjectatthetopofthisstackandreturnsthat
*objectasthevalueofthisfunction.
*
*@returnTheobjectatthetopofthisstack(thelastitem
*ofthe<tt>Vector</tt>object).
*@exceptionEmptyStackExceptionifthisstackisempty.
*/
publicsynchronizedEpop(){
E obj;
int len=size();
obj=peek();
removeElementAt(len-1);
returnobj;
}
/**
*Looksattheobjectatthetopofthisstackwithoutremovingit
*fromthestack.
*
*@returntheobjectatthetopofthisstack(thelastitem
*ofthe<tt>Vector</tt>object).
*@exceptionEmptyStackExceptionifthisstackisempty.
*/
publicsynchronizedEpeek(){
int len=size();
if(len==0)
thrownewEmptyStackException();
returnelementAt(len-1);
}
/**
*Testsifthisstackisempty.
*
*@return<code>true</code>ifandonlyifthisstackcontains
*noitems;<code>false</code>otherwise.
*/
publicbooleanempty(){
returnsize()==0;
}
/**
*Returnsthe1-basedpositionwhereanobjectisonthisstack.
*Iftheobject<tt>o</tt>occursasaniteminthisstack,this
*methodreturnsthedistancefromthetopofthestackofthe
*occurrencenearestthetopofthestack;thetopmostitemonthe
*stackisconsideredtobeatdistance<tt>1</tt>.The<tt>equals</tt>
*methodisusedtocompare<tt>o</tt>tothe
*itemsinthisstack.
*
*@paramothedesiredobject.
*@returnthe1-basedpositionfromthetopofthestackwhere
*theobjectislocated;thereturnvalue<code>-1</code>
*indicatesthattheobjectisnotonthestack.
*/
publicsynchronizedintsearch(Objecto){
inti=lastIndexOf(o);
if(i>=0){
returnsize()-i;
}
return-1;
}
/**useserialVersionUIDfromJDK1.0.2forinteroperability*/
privatestaticfinallongserialVersionUID=1224463164541339165L;
}
java contains返回的值不正确
大哥,你这个当然是false了。你构造器里面new了3个对象,内存地址分别为1,2,3,后面你主方法里面调用contains方法时,传值2和3又是new的对象,此事它的内存地址是4了。所以,虽然它和nodes里面的某一个元素值一样,但实际它们指向的是不同的物理地址。你加一个就new一个,这里的代码你改一下,不要边加边new,new一个Node对象,加多个元素。
关于本次java contains是什么意思和java中list集合中contains()的用法,啥意思的问题分享到这里就结束了,如果解决了您的问题,我们非常高兴。