在Java中Collection的一些常用方法總結(jié)
1、add() 向中添加元素
add(100) 自動裝箱操作,實際上是放進去的一個對象, Integer n = new Integer(100),實際上是把n放進了
Collection co = new ArrayList();co.add(1);
2、addAll( Collection c )
將指定集合中的所有元素添加到從集合中因為ArryList類中重寫了equals() 方法,所以兩個集合比較相等。
public class lxc { public static void main(String[] args) {Collection c = new ArrayList();for(int i = 0; i < 5; i ++) { c.add(i);}Collection c1 = new ArrayList();c1.addAll(c);System.out.println(c1.equals(c)); // true }}
3、size() 獲取集合中元素個數(shù)
Collection co = new ArrayList();int n = co.size();
4、clear() 清空集合
Collection co = new ArrayList();co.clear();
5、contains(100) 判斷當前集合中是否包含100這個元素 返回 true、false
Collection co = new ArrayList();co.add(100);co.add(200);boolean r = co.contains(100); // true
*** 深入探究***
例一:
下邊代碼,new了兩個字符串,s1被添加到集合中去了,但是s2沒有添加進去,最后輸入s2是否在集合當中?分析:按道理來說,s1和s2在棧內(nèi)存中是兩個變量分別指向了在堆內(nèi)存中存儲的也是兩個對象,只不過這兩個對象同時指向了 '123' 在常量池中的地址而已,怎么地集合中都不能包含s2?。肯逻呂覀儊砜聪耤ontains源碼:
public class lxc { public static void main(String[] args) {Collection r = new ArrayList();String s1 = new String('123');r.add(s1);String s2 = new String('123');System.out.println(r.contains(s2)); // true }}
contains()源碼:
參數(shù)o是調(diào)用contains()方法傳遞的參數(shù),內(nèi)部調(diào)用了indexOf(),而indexof方法內(nèi)部調(diào)用了indexOfRange方法,在這個方法中會去獲取集合中的每一個元素,然后通過equals() 方法來判斷傳遞的參數(shù)與集合中的元素是否相等,我們傳的參數(shù)是字符串,而字符串的equals()方法在源碼中已經(jīng)被重寫了,只要字符串值相等就想等,實際判斷的是:s1.equals(s2), 結(jié)果相等,返回元素在集合中的索引,而索引一定 >= 0,所以返回true!其實調(diào)用contains() 方法,內(nèi)部是調(diào)用equals()方法來判斷的?。。。。。。。。。。。。。。。?/p>
例二:
下邊知道為什么返回false了吧,Person類的eqauls() 方法繼承的是object對象上的,所以沒有重寫equals() 方法的兩個對象比較自然返回false了。
public class lxc { public static void main(String[] args) {Collection r = new ArrayList();Person p1 = new Person('lxc', 20);r.add(p1);Person p2 = new Person('lxc', 20);System.out.println(r.contains(p2)); // false }}class Person{ String name; int age; public Person(String name, int age) {this.name = name;this.age = age; }}
我們來重寫下Person對象的eqauls() 方法:
public class lxc { public static void main(String[] args) {Collection r = new ArrayList();Person p1 = new Person('lxc', 20);r.add(p1);Person p2 = new Person('lxc', 20);System.out.println(r.contains(p2)); // true }}class Person{ String name; int age; public Person(String name, int age) {this.name = name;this.age = age; } @Override public boolean equals(Object obj) {if(!(obj instanceof Person)) return false;if(this == obj) return true;Person o = (Person) obj;if((this.name == o.name) && (this.age == o.age)) { return true;}return false; }
6、remove() 刪除集合中某個元素
Collection co = new ArrayList();co.remove(100);
****深入探究****
其實remove() 方法和contains() 方法類似,內(nèi)部也是調(diào)用了equals() 方法,所以s1和s2相等,刪除了s2等同于刪除了s1。
public class lxc { public static void main(String[] args) {Collection r = new ArrayList();String s1 = new String('abc');r.add(s1);String s2 = new String('abc');Boolean res = r.remove(s2);System.out.println(res); // 刪除成功了System.out.println(r.size()); // 0 }}
remove源碼:獲取集合中的每一個元素,使用equals() 方法判斷是否相等,如果相等調(diào)用fastRemove方法刪除元素。
7、isEmpty() 判斷集合是否為空 true、false
co.isEmpty();
8、Object r = col.toArray() 把集合轉(zhuǎn)數(shù)組
9、iterator 迭代器對象 (重點)
xxx.iterator( ); 獲取迭代器。Collection h = new HashSet();Iterator r = h.iterator() 獲取iterator對象,目的遍歷數(shù)組 r迭代器對象 - 負責(zé)迭代集合當中的元素。
r迭代器對象中的方法: (1)boolean hasNext()如果仍有元素可迭代,則返回true;(2)Object next() 返回迭代的下一個元素。(3)void remove() 沒返回,刪除集合中的元素
public class lxc { public static void main(String[] args) {Collection h = new HashSet();h.add(1);h.add(2);h.add(new Object());// 獲取迭代器Iterator r = h.iterator();while(r.hasNext()) { Object res = r.next(); System.out.println(res);} }}
public class lxc { public static void main(String[] args) {Collection c = new ArrayList();Iterator i = c.iterator();c.add(1);c.add(2);Iterator i1 = c.iterator();while(i1.hasNext()) { Object r = i1.next(); i1.remove(); System.out.println(r);}System.out.println(c.size()); // 0 }}
****重點****
當集合的結(jié)構(gòu)發(fā)生改變的時候,迭代器必須重新獲取,如果還是以前老的迭代器,會出現(xiàn)異常。下邊集合的結(jié)構(gòu)發(fā)生了改變,結(jié)果報錯:
// 報錯:java.base/java.util.ArrayList$Itr.checkForComodificationpublic class lxc { public static void main(String[] args) {Collection c = new ArrayList();Iterator i = c.iterator();c.add(1);c.add(2);while(i.hasNext()) { Object r = i.next(); System.out.println(r);} }}
修改:
public class lxc { public static void main(String[] args) {Collection c = new ArrayList();Iterator i = c.iterator();c.add(1);c.add(2);Iterator i1 = c.iterator();while(i1.hasNext()) { Object r = i1.next(); System.out.println(r);} }}
到此這篇關(guān)于在Java中Collection的一些常用方法總結(jié)的文章就介紹到這了,更多相關(guān)Java Collection常用方法內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. ajax請求添加自定義header參數(shù)代碼2. ASP基礎(chǔ)知識VBScript基本元素講解3. 解決android studio引用遠程倉庫下載慢(JCenter下載慢)4. Kotlin + Flow 實現(xiàn)Android 應(yīng)用初始化任務(wù)啟動庫5. Python requests庫參數(shù)提交的注意事項總結(jié)6. Gitlab CI-CD自動化部署SpringBoot項目的方法步驟7. 利用CSS3新特性創(chuàng)建透明邊框三角8. 淺談SpringMVC jsp前臺獲取參數(shù)的方式 EL表達式9. axios和ajax的區(qū)別點總結(jié)10. python操作mysql、excel、pdf的示例
