Home > Java > javaTutorial > Definition and common methods of Collection interface

Definition and common methods of Collection interface

零下一度
Release: 2017-06-26 09:42:29
Original
7612 people have browsed it

1. Definition of Collection interface

public interfaceCollectionextends iterable

It can be found from the definition of the interface that this interface uses generic Type definition, the specific operation type must be specified during operation. This can ensure the safety of class set operations and avoid ClassCastException exceptions.

Collection is the most basic collection interface. A Collection represents a group of Objects, that is, the elements of the Collection. The classes provided by Java JDK are all "sub-interfaces" inherited from Collection, such as List and Set.

All classes that implement the Collection interface must provide two standard constructors:

1. The parameterless constructor is used to create an empty Collection;

2 .The constructor with a Collection parameter is used to create a new Collection that has the same elements as a traditional Collection. This constructor allows the user to copy a Collection.

Review: Array

   [] x= [5];   Student [] list= Student[5];
Copy after login

2. Commonly used methods of Collection

Direct code:

import java.util.ArrayList;  
 import java.util.Collection;    
 public class CollT {  
 public static void main(String[] args) {  
 Collection<String> a = new ArrayList<String>();  
 Collection<String> c = new ArrayList<String>();  
 Collection<String> d = new ArrayList<String>(); 
 Collection<String> e = new ArrayList<String>(); 
 Collection<String> f = new ArrayList<String>(); 
 // Integer中的i要大写 14         
 ArrayList<Integer> h = new ArrayList<Integer>();
 // h和g做比较 15  16         
 h.add(4); 17         
 h.add(33); 18         
 h.add(66); 19         
 h.add(77); 20         
 h.add(345); 21         
 h.add(44); 22         
 // h.add("aaa");—报错 23         
 // h.add(ff);—报错 24  25         
 // Object[] g={"aaa",4,33,66,ff,77,88,345,44,"dd","cfc"}; 26  27         
 a.add("白"); 28         
 a.add("日"); 29         
 a.add("依"); 30         
 a.add("山"); 31         
 a.add("尽"); 32  33         
 c.add("黃"); 34         
 c.add("河"); 35  36         
 d.add("白"); 37         
 d.add("日"); 38         
 d.add("依"); 39         
 d.add("山"); 40         
 d.add("尽"); 41  42         
 e.add("山"); 43         
 e.add("尽"); 44  45         
 f.add("5"); 46         
 // f.add(6); 47  48         
 Object[] g = { "aaa", 4, 33, 66, 77, 88, 345, 44, "dd", "cfc" }; 49  50         
 System.out.println(a.isEmpty()); 51         
 System.out.println(a.add("白")); 52        
 System.out.println(a.addAll(c)); 53         
 System.out.println(a); 54  55         
 System.out.println(a.contains("地")); 56         
 System.out.println(a.containsAll(c));// true 57         
 System.out.println(a.equals(c)); 58         
 System.out.println(c); 59  60         
 a.clear(); 61         
 System.out.println(a); 62         
 System.out.println(a.containsAll(d)); 63  64         
 d.remove("白"); 65         
 System.out.println(d); 66         
 d.remove("wang");// 没有反应 67         
 System.out.println(d); 68  69         
 System.out.println(a.hashCode());// a此时已经被晴空了 70         
 // 第一次时,我竟然是这么写的:a.hashCode(); 71         
 System.out.println(c.hashCode()); 72         
 System.out.println(d.hashCode()); 73  74         
 a.add("浪花滚滚"); 75         System.out.println(a); 76  77         
 System.out.println(e.removeAll(d)); 78         
 System.out.println(d.removeAll(e)); 79         
 System.out.println(d); 80         
 System.out.println(e); 81         
 System.out.println(e.size()); 82         
 // 我之前写成e.size(); 83         
 System.out.println(d.size()); 84  85         
 System.out.println(a.toArray());// 哈哈哈注意 86         
 System.out.println(f.toArray());// 哈哈哈注意 87  88         
 System.out.println("=========分割线========="); 89  90         
 // System.out.println(g); 91         
 for (int i = 0; i < g.length; i++) 92             
 System.out.println(g); 93  94         
 System.out.println("**********分割线*********"); 95  96         
 // 获取数组 97         
 Integer[] array = h.toArray(new Integer[h.size()]); 98         
 // 遍历数组 99         /**100          
 * for(int i=0;i<h.length;i++){ System.out.println(h[i]+""); length101          
 * 报错:cannot be resolved or is not a field102          
 */103         for (int element : array) {104             
 System.out.println(element);// 和下面的一起打印,结果竟然!!!105 106             
 System.out.print(element + "\t");// 复习一下而已107 108         
 }109 110     
 }111 112 
 }
Copy after login

Output:


/上面示例的打印结果!! 2 
 false 3 true 4 
 true 5 [白, 日, 依, 山, 尽, 白, 黃, 河] 
  false 7 
  true 8 false 9 [黃, 河]10 []11 false12 [日, 依, 山, 尽]13 
  [日, 依, 山, 尽]14 115 128872116 79836512517 [浪花滚滚]18 
  true19 false20 [日, 依, 山, 尽]21 []22 023 424 
  [Ljava.lang.Object;@16925b025 [Ljava.lang.Object;@297ffb26 =========分割线=========27 
  [Ljava.lang.Object;@914f6a28 
  [Ljava.lang.Object;@914f6a29 [Ljava.lang.Object;@914f6a30 [Ljava.lang.Object;@914f6a31 
  [Ljava.lang.Object;@914f6a32 [Ljava.lang.Object;@914f6a33 [Ljava.lang.Object;@914f6a34 
  [Ljava.lang.Object;@914f6a35 [Ljava.lang.Object;@914f6a36 
  [Ljava.lang.Object;@914f6a37 **********分割线*********38 
  439 4    3340 33    6641 66    7742 77    34543 345    4444 44
Copy after login

Summary of common methods of Collection:

boolean add(E e) //注意它的参数类型boolean addAll(Collection<? extends E> c) 
将指定 collection 中的所有元素都添加到此 
collection 中boolean remove(Object o)boolean contains(Object o) 
//判断集合中指定的元素是否存在boolean containsAll()//   
如果此 collection 包含指定 collection 中的所有元素,
则返回 true。 boolean removeAll(Collection<?> c)boolean retainAll(Collection<?> c) 
仅保留此 collection 中那些也包含在指定 collection 的元素(可选操作)(取交集)
            clear()
            size()
            toArray() boolean isEmpty()
            Iterator<E> iterator() 返回在此 collection 的元素上进行迭代的迭代器。
Copy after login

Code examples ( Pay attention to the comparison results)

Example 1: (Mainly compare 1. Whether there is commented out content; 2. true and false and printing position; 3. Think about the comparison of execution order and string type)


import java.util.ArrayList; 2 
import java.util.Collection; 3  4 
public class CollT { 5     
public static void main(String[] args) { 6  7         
Collection<String> a = new ArrayList<String>(); 8         
Collection<String> b = new ArrayList<String>(); 9 10         
a.add("白");11         a.add("日");12         
a.add("依");13         a.add("山");14         a.add("尽");15 16         
a.remove("4");17 18         b.add("白");19         b.add("日");20         
b.add("依");21         b.add("山");22         b.add("尽");23 24         
System.out.println(b.equals(a));25 26         System.out.println(a);27         
System.out.println(b);28 29         System.out.println(a.hashCode());30        
System.out.println(b.hashCode());31 32         
System.out.println(a.removeAll(b));33 34         System.out.println(a);35         
System.out.println("===================");36 37         
Object[] c = a.toArray();38         String s = null;39         
for (int i = 0; i < c.length; i++) {40             
s = (String) c[i];41             System.out.println(s);42         
}   
}
}
Copy after login

Example 2:


 import java.util.ArrayList; 2 
 import java.util.Collection;
 public class CollT { 5     
 public static void main(String[] args) { 6  7         
 Collection<String> a = new ArrayList<String>(); 8         
 Collection<String> b = new ArrayList<String>(); 9 10         
 a.add("白");11         a.add("日");12         a.add("依");13         
 a.add("山");14         a.add("尽");15 16         //a.remove("4");17 18         
 b.add("白");19         b.add("日");20         b.add("依");21         
 b.add("山");22         b.add("尽");23 24         
 System.out.println(b.equals(a));25 26         System.out.println(a);27         
 System.out.println(b);28 29         System.out.println(a.hashCode());30         
 System.out.println(b.hashCode());31 32         //System.out.println(a.removeAll(b));33 34 
 System.out.println(a);35         
 System.out.println("===================");36 37         
 Object[] c = a.toArray();38         String s = null;39         
 for (int i = 0; i < c.length; i++) {40             
 s = (String) c[i];41             System.out.println(s);42         }
 }
 }
Copy after login

##Example 3 :


import java.util.ArrayList;
import java.util.Collection; 3  4 
public class CollT { 5     
public static void main(String[] args) { 6  7         
Collection<String> a = new ArrayList<String>(); 8         
Collection<String> b = new ArrayList<String>(); 9 10        
 a.add("白");11         a.add("日");12         
 a.add("依");13         a.add("山");14         a.add("尽");15 16         
 a.remove("4");17 18         b.add("黃");19         b.add("河");20         
 b.add("入");21         b.add("海");22         b.add("流");23 24         
 System.out.println(b.equals(a));25 26         System.out.println(a);27         
 System.out.println(b);28 29         System.out.println(a.hashCode());30         
 System.out.println(b.hashCode());31 32         
 System.out.println(a.removeAll(b));33 34         System.out.println(a);35         
 System.out.println("===================");36 37         
 Object[] c = a.toArray();38         String s = null;39         
 for (int i = 0; i < c.length; i++) {40             
 s = (String) c[i];41             System.out.println(s);42         
 }
 }
 }
Copy after login

Example 4:


import java.util.ArrayList; 
import java.util.Collection;
public class CollT { 5     
public static void main(String[] args) { 6  7         
Collection<String> a = new ArrayList<String>(); 8         
Collection<String> b = new ArrayList<String>(); 9 10         
a.add("白");11         a.add("日");12         a.add("依");13         
a.add("山");14         a.add("尽");15 16         //a.remove("4");17 18         
b.add("黃");19         b.add("河");20         b.add("入");21         
b.add("海");22         b.add("流");23 24         
System.out.println(b.equals(a));25 26         
System.out.println(a);27         System.out.println(b);28 29         
System.out.println(a.hashCode());30         System.out.println(b.hashCode());31 32         
//System.out.println(a.removeAll(b));33 34         System.out.println(a);35         
System.out.println("===================");36 37         
Object[] c = a.toArray();38         String s = null;39         
for (int i = 0; i < c.length; i++) {40             
s = (String) c[i];41             System.out.println(s);42         }
}
}
Copy after login

Example 5:


import java.util.ArrayList;
import java.util.Collection; 3  4 
public class CollT { 5     
public static void main(String[] args) 
{ 
Collection<String> a = new ArrayList<String>(); 8         
Collection<String> b = new ArrayList<String>();        
a.add("白");11         
a.add("日");12         a.add("依");13         a.add("山");14         
a.add("尽");15 16         //a.remove("4");17 18        
b.add("黃");19         b.add("河");20         b.add("入");21         
System.out.println(b.equals(a));24 25         
System.out.println(a);26         System.out.println(b);27 28         
System.out.println(a.hashCode());29         System.out.println(b.hashCode());30 31         
//System.out.println(a.removeAll(b));32 33         System.out.println(a);34         
System.out.println("===================");35 36         
Object[] c = a.toArray();37         String s = null;38         
for (int i = 0; i < c.length; i++) {39             
s = (String) c[i];40             System.out.println(s);41        
}
}
}
Copy after login

Example 6:


 import java.util.ArrayList;
 import java.util.Collection;
 public class CollT { 5     
 public static void main(String[] args) 
 { 6  7         
 Collection<String> a = new ArrayList<String>(); 8         
 Collection<String> b = new ArrayList<String>(); 9 10         
 a.add("白");11         a.add("日");12         a.add("依");13         
 a.add("山");14         a.add("尽");15 16         a.remove("4");17 18         
 b.add("黃");19         b.add("河");20         b.add("入");                
 System.out.println(b.equals(a));24 25         System.out.println(a);26         
 System.out.println(b);27 28         System.out.println(a.hashCode());29         
 System.out.println(b.hashCode());30 31         
 System.out.println(a.removeAll(b));32 33         
 System.out.println(a);34         System.out.println("===================");35 36         
 Object[] c = a.toArray();37         String s = null;38         
 for (int i = 0; i < c.length; i++) {39             
 s = (String) c[i];40             System.out.println(s);      
 }
 }
  }
Copy after login

2. Common methods of List and set

List implementation classes include: LinkedList, Vector, ArrayList

Note:

List interface, inherited from Collection, can be accessed in index order, an indexed Collection.

It has the function of list, and the order of elements is listed in the order of adding.

The Collection, List, Set and Map here are all interfaces (Interface), not specific class implementations.

List list = new ArrayList(); This is a statement we often use to create a new List. Here List is the interface and ArrayList is the specific class.

List

The List interface extends Collection and declares the characteristics of a collection that stores a sequence of elements. Using a zero-based subscript, elements can be inserted and accessed by their position in the list. A list can contain repeated elements. In addition to the methods defined by Collection, List defines some methods of its own. Note again that while a class cannot be modified, a ClassCastException will occur when one object is incompatible with another, such as when an attempt is made to add an incompatible object to a class.

For the add() and addAll() methods defined by Collection, List adds methods add(int, Object) and addAll(int, Collection). These methods insert elements at the specified index.

The semantics of add(Object) and addAll(Collection) defined by Collection are also changed by List so that they add elements to the end of the list.

To obtain the object stored at the specified location, you can call the get() method with the object's subscript.

To assign a value to an element in the class table, you can call the set() method and specify the subscript of the changed object.

Call indexOf() or lastIndexOf() to get the index of an object.

By calling the subList() method, you can obtain a sublist of the list that specifies the start subscript and end subscript. This method makes list processing very convenient.

Set

The set collection interface defines a collection. It extends Collection and describes features of collections that do not allow copying of elements. Therefore, if you try to add a copied element to the collection, the add() method will return false. It itself does not define any additional methods.

Summary:

Characteristics of List collection

1) Elements are ordered (can be accessed by index)

2) Elements Can be repeated and can store multiple null values

Features of Set:

1) Elements are unordered (cannot be accessed by index)

2) Elements cannot be repeated, There can only be one null value

Code example:

 1 /*List集合的特有特性 * 
 *public interface List<E>extends Collection<E> 4 
 *-- add(int index, E element) 在列表的指定位置插入指定元素 5
  *-- addAll(int index, Collection<? extends E> c) 
 *-- E get(int index)  //返回列表中指定位置的元素。 7 
 *-- int indexOf(Object o)  // 返回此列表中第一次出现的指定元素的索引 8 
 *-- listIterator() 
 *-- e set(int index, E element)  // 用指定元素替换列表中指定位置的元素10 
 *-- void remove(int position)  移除指定位置的元素11 
 *-- List<E> subList(int fromIndex, int toIndex ) 
 类似substring()12 */13 import java.util.ArrayList;14 15 class CollT {16     
 public static void main(String[] args) {17         
 ArrayList<String> list = new ArrayList<String>();18         
 list.add("Java"); // 下標是019         list.add("Class"); // 下標是120         
 list.add("Array"); // 下標是221         list.add("Collection"); // 下標是322         
 list.add("List"); // 下標是423         list.add("Set"); // 下標是524         
 System.out.println(list.subList(0, 3));// 注意不包含下標是3的25     }
  }
Copy after login

 

 

 

The above is the detailed content of Definition and common methods of Collection interface. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template