当前位置:网站首页>Use of collection
Use of collection
2022-06-25 05:41:00 【Axyzstra】
Common methods
| Modifier and Type | Method and Description |
|---|---|
boolean | add(E e) Make sure that this collection contains the specified elements ( Optional operation ). |
boolean | addAll(Collection<? extends E> c) Add all elements of the specified collection to this collection ( Optional operation ). |
void | clear() Remove all elements from this collection ( Optional operation ). |
boolean | contains(Object o) If this collection contains the specified element , Then return to true . |
boolean | containsAll(Collection<?> c) If this collection contains the specified aggregate All elements in , Then return to true. |
boolean | equals(Object o) Compares the specified object with this collection for equality . |
int | hashCode() Returns the hash code value of this collection . |
boolean | isEmpty() If this collection does not contain elements , Then return to true . |
Iterator<E> | iterator() Returns the iterator of the elements in this collection . |
default Stream<E> | parallelStream() Returns possible parallel Stream With this collection as its source . |
boolean | remove(Object o) Remove a single instance of the specified element from the collection ( If there is )( Optional operation ). |
boolean | removeAll(Collection<?> c) Deletes all elements of the specified collection contained in the specified collection ( Optional operation ). |
default boolean | removeIf(Predicate<? super E> filter) Delete all elements of this collection that satisfy the given predicate . |
boolean | retainAll(Collection<?> c) Keep only the elements contained in this collection in the specified collection ( Optional operation ). |
int | size() Returns the number of elements in this collection . |
default Spliterator<E> | spliterator() Create a Spliterator The elements in this collection . |
default Stream<E> | stream() Returns the order in which this collection is the source Stream . |
Object[] | toArray() Returns an array containing all the elements in this collection . |
<T> T[] | toArray(T[] a) Returns an array containing all the elements in this collection ; The runtime type of the specified array . |
Demonstration of general methods
package aggregate ;
import java.util.ArrayList;
import java.util.Collection;
public class CollectionDemo {
public static void main(String[] args) {
Collection coll = new ArrayList();
Collection c = new ArrayList();
// Additive elements
coll.add(" watermelon ");
coll.add(" strawberry ");
coll.add(" Apple ");
c.addAll(coll);
// Output all elements
System.out.println("====== Output all elements ========");
System.out.println(coll);
System.out.println(c);
System.out.println(c);
// Element number
System.out.println("======== Element number ===========");
System.out.println(coll.size());
System.out.println(c.size());
// Contains elements
System.out.println("======= Determine whether it contains elements =======");
System.out.println(coll.contains(" watermelon "));
System.out.println(coll.isEmpty());
System.out.println(coll.containsAll(c));
c.remove(" Apple ");
System.out.println(coll.equals(c));
System.out.println("====== Remove elements ===========");
coll.removeAll(c);
System.out.println(coll);
c.clear();
System.out.println(c.size());
}
}
A demonstration of the iteration of elements within a collection
The two methods
enhance for loop
Using the iterator method iterator() To operate
iterator() Returns all the elements in the collection as iterators , After that, the operation on the iterator is the operation on the collection ;
There are three important functions in iterators
boolean | hasNext() If the iteration has more elements , Then return to true . |
|---|---|
E | next() Returns the next element in the iteration . |
default void | remove() Remove the last element returned by this iterator from the underlying collection ( Optional operation ). |
package aggregate ;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class CollectionDemo {
public static void main(String[] args) {
Collection coll = new ArrayList();
// Additive elements
coll.add(" watermelon ");
coll.add(" strawberry ");
coll.add(" Apple ");
System.out.println("======= enhance for loop =======");
for (Object object : coll) {
String o = (String)object;
System.out.println(o);
}
System.out.println("========= Using Iterators =======");
Iterator its = coll.iterator();
while(its.hasNext()) {
String o = (String) its.next();
System.out.println(o);
// coll.remove(" Apple "); // When using iterators , You cannot modify the collection
its.remove();
}
System.out.println("======= After using the iterator to delete the elements in the collection =======");
System.out.println(coll.size());
}
}
As a collection element of an object
package aggregate ;
import java.util.ArrayList;
import java.util.Collection;
public class Student {
private String name;
private int age;
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
public static void main(String[] args) {
Collection coll = new ArrayList();
Student s1 = new Student(" Zhang San ", 22);
Student s2 = new Student(" Li Si ", 21);
Student s3 = new Student(" Wang Wu ", 19);
// Additive elements
coll.add(s1);
coll.add(s2);
coll.add(s3);
System.out.println(coll);
// Element iteration
System.out.println(" enhance for loop ");
for (Object object : coll) {
Student s = (Student) object;
System.out.println(s);
}
// Remove elements
System.out.println(coll.size());
coll.remove(s2);
System.out.println(coll.size());
coll.remove(new Student(" Zhang San ", 22));
System.out.println("new After the same element is deleted :" + coll.size()); // In this way, the elements in the collection cannot be deleted
}
}
边栏推荐
- A method of automatic continuation of previous tables in word table
- Install pytorch through pip to solve the problem that torch cannot be used in jupyter notebook (modulenotfoundererror:no module named 'Torch').
- Detailed summary of float
- Semantic segmentation fcns in the wild: pixel level adaptive and constraint based adaptation
- 2.20 learning content
- Get the first letter of Chinese phonetic alphabet in Excel and capitalize it
- 渗透测试-提权专题
- Use of MySQL variables
- By inserting a section break, the word header, footer, and page number can start from any page
- Professional things use professional people
猜你喜欢

Install pytorch through pip to solve the problem that torch cannot be used in jupyter notebook (modulenotfoundererror:no module named 'Torch').

Day18 (set, generic, hash table, tree, stack and queue, graph, array and linked list)

2022.1.23 diary

JSON Library Tutorial from scratch (I): starting to learn and organize notes

Example of dynamic programming 3 leetcode 55

Dynamic programming example 2 leetcode62 unique paths

Semantic segmentation cvpr2020 unsupervised intra domain adaptation for semantic segmentation through self supervision
![[Huawei machine test] hj16 shopping list](/img/54/d28f5aea9350af7797ca7c069e564d.jpg)
[Huawei machine test] hj16 shopping list
![H5 canvas drawing circle drawing fillet [detailed explanation]](/img/6f/a33a323b6cd0918066e8b71a22d841.jpg)
H5 canvas drawing circle drawing fillet [detailed explanation]

Professional things use professional people
随机推荐
Only these four instructions are required to operate SQL data
Dynamic programming example 2 leetcode62 unique paths
Unsupervised domain adaptation in semantic segmentation:a review unsupervised domain adaptation in semantic segmentation: a review
A brief talk on media inquiry
Array and simple function encapsulation cases
"APEC industry +" biov Tech talks about the cross-border of Chinese biotechnology enterprises and "Pratt & Whitney Kangyu" | apec-hub public welfare
The article is on the list. Welcome to learn
Semantic segmentation cvpr2020 unsupervised intra domain adaptation for semantic segmentation through self supervision
Extend the toolbar of quill editor
滲透測試-提權專題
Fundamentals of C language
About the definition of pointer variables (personal notes)
SSRF-lab
Large number operation (capable of square root, power, permutation and combination, logarithm and trigonometric value)
hr竟主动给这位测试小姐姐涨工资,她是怎么做到的?
C language - minesweeping
JS verification code input number auto skip
钱堂教育的证券账户安全吗?靠谱吗?
Transformations of pytorch torch torch vision
Create dynamic array