当前位置:网站首页>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
}
}
边栏推荐
- 2022.1.21 diary
- Day19 (variable parameter, enhanced for loop traversal, generic wildcard <? >, TreeSet, linkedhashset, nested traversal of sets, set set, static import,)
- Oracle SQL statement operand: rounding, rounding, differentiation and formatting
- [day40 literature extensive reading] space and time in the child's mind: metallic or atomic
- JMeter stress testing and agent recording
- 16 application problem solving
- Semantic segmentation fcns in the wild: pixel level adaptive and constraint based adaptation
- How to add an external header file in vs?
- Voxel based and second network learning
- JS function to realize simple calculator
猜你喜欢

Flex flexible layout for mobile terminal page production

Deep analysis of recursion in quick sorting

Personalized Federated Learning with Moreau Envelopes

Excel splits a worksheet into multiple worksheets according to conditions, and how to split multiple worksheets into independent tables
![H5 native player [learn video]](/img/51/83a200d0423b7274d1e981ec2ede2c.jpg)
H5 native player [learn video]

JSON Library Tutorial from scratch (II): parsing digital learning and sorting notes

Timed thread pool

ThreadLocal

JSON Library Tutorial from scratch (III): parsing strings, learning and sorting notes

Mirror image of binary tree
随机推荐
[day40 literature extensive reading] space and time in the child's mind: metallic or atomic
Fundamentals of C language
2022.1.25
3.2.3 use tcpdump to observe TCP header information (supplement common knowledge of TCP protocol)
Object creation and invocation code example
Guava new collection type
Monkey test of APP automation
Vue uses keep alive to cache page optimization projects
Timed thread pool
Common cluster deployment schemes in redis
How to add an external header file in vs?
Read the general components of antd source code
UVA816 Abbott’s Revenge
BUUCTF(web:1-50)
By inserting a section break, the word header, footer, and page number can start from any page
cuda编译报错
I got to know data types and function variables for the first time. I learned data types and function variables together today and yesterday, so I saved them in the first issue to record.
Dynamic programming example 1 leetcode 322 coin change
Guava immutable set
Flex flexible layout for mobile terminal page production