当前位置:网站首页>Detailed explanation of collection class methods____ (4) Judgment and assignment, etc
Detailed explanation of collection class methods____ (4) Judgment and assignment, etc
2022-06-28 06:57:00 【Xiaobinbin &】
1、disjoint(Collection<?> c1,Collection<?> c2);
Compare sets c1 And collection c2 There are no common elements between them , Go back when you have true, Instead, return to false
public class CollectionsDome3 {
public static void main(String[] args) {
ArrayList<Integer> c1 = new ArrayList<Integer>(Arrays.asList(2,4,6,8,9,6));
ArrayList<Integer> c2 = new ArrayList<Integer>(Arrays.asList(1));
boolean flge = Collections.disjoint(c1 , c2);
System.out.println(flge); // The output is true
}
}
2、addAll(Collection<? super T> c,T...a)
Add the specified collection to the collection c in ,T...a Represents a variable parameter ,a Array equivalent
public class CollectionsDome3 {
public static void main(String[] args) {
ArrayList<Integer> c1 = new ArrayList<Integer>(Arrays.asList(2,4,6,8,9,6));
Collections.addAll(c1,2,3,5,7,8,9);
System.out.println(c1);
}
}3、copy(List m,List n)
take n The element in is assigned to m in .
public class CollectionsDome3 {
public static void main(String[] args) {
ArrayList<Integer> c1 = new ArrayList<Integer>(Arrays.asList(2,4,6,8,9,6));
ArrayList<Integer> c2 = new ArrayList<Integer>(Arrays.asList(1));
Collections.copy(c1,c2);// take c2 The value of is assigned to c1, Cover from beginning to end c2.size()<=c1.size()
System.out.println(c1);//[1, 4, 6, 8, 9, 6]
System.out.println(c2);//[1]
}
}4、checkedXxx()
Check if this collection is Xxx Collection types .
for example : checkedCollection(Collection c,class o.class)
c、 Represents a collection for which type safe views are obtained at run time .o、 Represents the element types that a given collection is allowed to store .
This command is used to return the specified set Dynamic security view
The following other methods are created in the same way as this
public class CollectionsDome3 {
public static void main(String[] args) {
Collection c3=new ArrayList();
c3.add(1);
c3.add("df");
Collection<Student> strings = Collections.checkedCollection(c3 ,Integer.class);
c3.add("dddd");
System.out.println(c3);//[1, df, dddd]
System.out.println(strings);//[1, df, dddd] strings The set cannot be changed
}
}| checked | explain |
| checkedList(List c,class o.class) | List aggregate , Return to dynamic security view |
| checkedMap(Mapn c,class o.class) | Map aggregate , Return to dynamic security view |
| checkedNavigableMap(NavigableMap c,class o.class) | NavigableMap aggregate , Return to dynamic security view |
| checkedNavigableSet(NavigableSet c,class o.class) | NavigableSet aggregate , Return to dynamic security view |
| checkedQueue(Queue c,class o.class) | Queue aggregate , Return to dynamic security view |
| checkedSet(Set c,class o.class) | Set aggregate , Return to dynamic security view |
| checkedSortedMap(SortedMap c,class o.class) | SortedMap aggregate , Return to dynamic security view |
| checkedSortedSet(SortedSet c,class o.class) | SortedSet aggregate , Return to dynamic security view |
5、asLifoQueue(Deque deque);
Double ended queue ( fifo , The queue is full , Then the first segment has priority to exit ) Expressed as Lifo queue (LIFO Last in first out )
public class CollectionsDome3 {
public static void main(String[] args) {
Deque<Integer> queue=new ArrayDeque<Integer>();// deque , fifo
queue.add(1);queue.add(2);
System.out.println(queue);// Output [1,2]
// Deque convert to Queue queue
Queue<Integer> i = Collections.asLifoQueue(queue);//Lifo queue , First in, then out
System.out.println(i);
}
}6、list(Enumeration en);
Returns an array , This list contains the given Enumeration ( enumeration ) All elements returned , And store these elements in the order returned by enumeration ArrayList The method in .
package cn.cm;
import java.util.*;
public class Peal {
public static void main(String args[]) {
// Instantiate an ArrayList and
// Stack object
List arr_l = new ArrayList();
Stack st = new Stack();// Stack queue , Last in, first out
// By using push() method is
// to add elements in stack
st.push(10);// Store elements
st.push(20);
st.push(30);
st.push(40);
st.push(50);
System.out.println(st.pop());// Output 50, Take out this element , There is no such element in the collection
System.out.println(st.pop());// Output 40, Take out this element , There is no such element in the collection
System.out.println(st.pop());// Output 30, Take out this element , There is no such element in the collection
System.out.println(st);//st Only left in the set 【10,20】
// Get elements in an enumeration object
Enumeration en = st.elements();// Create an enum type object
System.out.println(en.nextElement());// take out 1 Elements 10
// By using list() method is to
// return the array list of the
// given enumeration object
ArrayList arr_1 = Collections.list(en);// take en The return element of enumerating the remaining elements exists arr_1 in
// only [20]
System.out.println("Collections.list(en): " + arr_1);// Output [20]
}
}7、singleton(),singletonMap()、singletonList()
These are single column modes , There's only one element .
adopt Collection.singleton(sc) Create a single column schema element , In turn, is Set type ,Map type ,List type .
public class Sidjsn extends Student {
public static void main(String[] args) {
List sc=new ArrayList(Arrays.asList(1,2,4,5));
List sc1=new ArrayList(Arrays.asList(1,2,4,6));
Set<List> s = Collections.singleton(sc);
System.out.println(s); // [[1, 2, 4, 5]]
// s.add(sc); I can't ,s Just a singleton mode , There can only be one element
List<List> s1 = Collections.singletonList(sc);
System.out.println(s1);// [[1, 2, 4, 5]]
Map<Integer, List> s2 = Collections.singletonMap(1 , sc);
System.out.println(s2);// System.out.println(s2);//
System.out.println(sc); // [1, 2, 4, 5]
}
}边栏推荐
- C语言教程大全
- JDBC learning (I) -- implementing simple CRUD operations
- CMAKE小知识
- Puge -- understanding of getordefault() method
- BACnet/IP網關如何采集樓宇集中控制系統數據
- 手把手教你用Ucos
- ImportError: cannot import name 'ensure_dir_exists'的可解决办法
- Niubi 666, this project makes web page making as simple as building blocks
- VM332 WAService.js:2 Error: _vm.changeTabs is not a function报错
- JS of learning notes -- split(), replace(), join()
猜你喜欢

金山云团队分享 | 5000字读懂Presto如何与Alluxio搭配

Techo day Tencent technology open day, June 28 online waiting for you!

声网 VQA:将实时互动中未知的视频画质用户主观体验变可知

Linked list (III) - reverse linked list

Freeswitch uses Mod_ Shot module plays mp3

推荐几款0代码、免费、现学现用的可视化工具

FPGA - 7系列 FPGA SelectIO -09- 高级逻辑资源之IO_FIFO

强化学习——格子世界

Boost the rising point | yolov5 combined with alpha IOU

MySQL (I) - Installation
随机推荐
Yolov5 adds a small target detection layer
YOLOv5增加小目标检测层
浮动与定位
语音增强-频谱映射
最后的二十九天
Freeswitch使用originate转dialplan
FPM tool installation
Techo Day 腾讯技术开放日,6月28日线上等你!
pytorch RNN 学习笔记
My MVVM open source project "travel epidemic prevention app" has been released
Wechat applet paging function, pull-down refresh function, direct dry goods
What if the applet page is set to 100% height or left blank?
MySQL (II) - basic operation
FPGA - 7 Series FPGA selectio -09- io of advanced logic resources_ FIFO
ROS rviz_ Satellite function package visualizes GNSS track and uses satellite map
Triode driven brushless motor
Shell script one click deployment (MySQL)
[staff] arpeggio mark
4~20ma input /0~5v output i/v conversion circuit
[produced by Xinghai] operation and maintenance inspection collection