惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

SecWiki News
SecWiki News
I
InfoQ
The Cloudflare Blog
人人都是产品经理
人人都是产品经理
博客园 - Franky
T
Tailwind CSS Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
量子位
博客园_首页
罗磊的独立博客
V
V2EX
李成银的技术随笔
大猫的无限游戏
大猫的无限游戏
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
True Tiger Recordings
Vercel News
Vercel News
Cyberwarzone
Cyberwarzone
Cisco Talos Blog
Cisco Talos Blog
F
Fox-IT International blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
M
Microsoft Research Blog - Microsoft Research
Know Your Adversary
Know Your Adversary
爱范儿
爱范儿
The Register - Security
The Register - Security
G
Google Developers Blog
The Hacker News
The Hacker News
Malwarebytes
Malwarebytes
S
Securelist
博客园 - 三生石上(FineUI控件)
Jina AI
Jina AI
T
Threat Research - Cisco Blogs
T
The Exploit Database - CXSecurity.com
S
SegmentFault 最新的问题
博客园 - 叶小钗
F
Fortinet All Blogs
Apple Machine Learning Research
Apple Machine Learning Research
宝玉的分享
宝玉的分享
博客园 - 聂微东
T
Threatpost
博客园 - 【当耐特】
D
Docker
P
Privacy & Cybersecurity Law Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
G
GRAHAM CLULEY
V
Visual Studio Blog
C
Cisco Blogs
IT之家
IT之家
S
Security Archives - TechRepublic
Latest news
Latest news
阮一峰的网络日志
阮一峰的网络日志

qubeijun的博客

言语理解与表达 父亲往事-我儿子叫冬冬 公务员行测答题顺序及用时及答题技巧 判断推理 数量关系 冬甜夫妇5-10-5-21日本蜜月行程安排回归总结篇(无图片) 冬甜夫妇5.10-5.21日本蜜月行程安排计划篇 5.1 小贝壳疫苗计划 100DAYS 父亲往事-猴牤大爷的“帽子” 父亲往事-姨妈家的葡萄架 父亲往事-十五的月饼 父亲往事-姑妈的压岁钱 父亲往事-母亲的缝纫机父亲的锯 父亲往事-家属来队 父亲往事-新兵连的生活 父亲往事-村北的槐树林 父亲往事-小贝壳 公务员面试 系统架构设计师 图形推理 数学运算 父亲往事-惊蛰 资料分析 父亲往事-偷吃西红柿有绝窍 父亲往事-毯子衣服 父亲往事-水壶煮鸡蛋 父亲往事-煎饼蘸香油 系统架构设计师论文 软件设计师 父亲往事-大哥 资料分析-旧 基于grafana的监控部署 mysql数据库定时备份&运行日志定时清理 各种问题解决答案汇总 Alibaba Cloud Toolkit的使用说明 Dojo简单入门 Angular简单入门 vue基础知识点总结 java正则表达式收集 java处理时间和日期api java常用的io,输入输出 java调试代码不再用system.out.println(),用日志来调试程序 java常用工具类总结 java常用字符串操作总结 python之pymysql增删改查mysql数据库 词性标注集 python获取当前文件夹下的所有的文件名 mysql数据库操作总结 (翻译)Understanding LSTM Networks (翻译)Understanding Convolutional Neural Networks for NLP (翻译)传统和深度学习模型在文本分类中的应用综述与基准 小木虫论坛-学术科研互动平台爬虫 solr命令(持续更新) python json读写操作 python pip安装包镜像 python数字进度条 抓取维基百科分类索引下某一分类下的子分类及其分类下的页面(仅限词条) python3循环读取excel文件并写入json python3 requests动态网页post提交数据 python+selenium搜狗微信主页抓取 2017年5月25日 2017年5月18日 2017年5月17日 2017年5月15日 2017年5月16日 2017年5月14日 2017年5月13日 2017年5月12日 2017年5月11日 2017年5月10日 2017年5月9日 2017年5月8日 2017年5月7日 2017年5月6日 2017年5月5日 2017年5月4日 2017年5月3日 2017年5月2日 2017年5月1日 儿子的信-2013年7月9日 儿子的信-2013年7月8日 儿子的信-2011年11月26日 儿子的信-2010年8月16日 儿子的信-2010年1月2日
java常见集合汇总
2020-02-27 · via qubeijun的博客

发表于|更新于|java

|总字数:1.5k|阅读时长:8分钟|浏览量:|

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package _27javacollect;

import java.time.DayOfWeek;
import java.util.*;

class Student {
public String name;
public int score;

public Student(String name, int score) {
this.name = name;
this.score = score;
}
}
class UserComparator implements Comparator<User></User> {
public int compare(User u1, User u2) {
if (u1.number.charAt(0) == u2.number.charAt(0)) {
// 如果两人的号都是A开头或者都是V开头,比较号的大小:
return u1.number.compareTo(u2.number);
}
if (u1.number.charAt(0) == 'V') {
// u1的号码是V开头,优先级高:
return -1;
} else {
return 1;
}
}
}

class User {
public final String name;
public final String number;

public User(String name, String number) {
this.name = name;
this.number = number;
}

public String toString() {
return name + "/" + number;
}
}


public class List_exe {

public static void main(String[] args) {
/** 使用List*/
List<String> list = new ArrayList<>();
list.add("apple");
list.add("pear");
list.add("apple");
list.add(null);
System.out.println(list);

// 迭代器遍历list
for(Iterator<String> it=list.iterator();it.hasNext();){
String s = it.next();
System.out.println("迭代器:"+s);
}
//再次简化
for(String s:list){
System.out.println("简化:"+s);
}

//list转换array,不常用
Object[] array = list.toArray();
for(Object o:array){
System.out.println("list转array,不常用:"+o);
}

//list转换array,常用
String[] array1 = list.toArray(new String[list.size()]);
for (String n : array1) {
System.out.println("list转array,常用:"+n);
}

//array变list
Integer[] array2 = { 1, 2, 3 };
List<Integer> list2 = Arrays.asList(array2);
System.out.println(list2);

/**使用map */
Student s = new Student("Xiao Ming", 99);
Map<String, Student> map = new HashMap<>();
map.put("Xiao Ming", s); // 将"Xiao Ming"和Student实例映射并关联
Student target = map.get("Xiao Ming"); // 通过key查找并返回映射的Student实例
System.out.println(target == s); // true,同一个实例
System.out.println(target.score); // 99
Student another = map.get("Bob"); // 通过另一个key查找
System.out.println(another); // 未找到返回null

//key不能重复,value可以重复
Map<String, Integer> map1 = new HashMap<>();
map1.put("apple", 123);
map1.put("pear", 123);
System.out.println(map1.get("apple")); // 123
map1.put("apple", 789); // 再次放入apple作为key,但value变为789
System.out.println(map1.get("apple")); // 789

//遍历key
for (String key : map1.keySet()) {
Integer value = map1.get(key);
System.out.println(key + " = " + value);
}
//同时遍历key,value
for (Map.Entry<String, Integer> entry : map1.entrySet()) {
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println(key + " = " + value);
}

/**使用EnumMap */
Map<DayOfWeek, String> map2 = new EnumMap<>(DayOfWeek.class);
map2.put(DayOfWeek.MONDAY, "星期一");
map2.put(DayOfWeek.TUESDAY, "星期二");
map2.put(DayOfWeek.WEDNESDAY, "星期三");
map2.put(DayOfWeek.THURSDAY, "星期四");
map2.put(DayOfWeek.FRIDAY, "星期五");
map2.put(DayOfWeek.SATURDAY, "星期六");
map2.put(DayOfWeek.SUNDAY, "星期日");
System.out.println(map2);
System.out.println(map2.get(DayOfWeek.MONDAY));

/**使用TreeMap 对Key进行排序*/
Map<String, Integer> map3 = new TreeMap<>();
map3.put("orange", 1);
map3.put("apple", 2);
map3.put("pear", 3);
for (String key : map3.keySet()) {
System.out.println(key);
}

/**Set */
//Set存储不重复的元素组合
Set<String> set = new HashSet<>();
System.out.println(set.add("abc")); // true
System.out.println(set.add("xyz")); // true
System.out.println(set.add("xyz")); // false,添加失败,因为元素已存在
System.out.println(set.contains("xyz")); // true,元素存在
System.out.println(set.contains("XYZ")); // false,元素不存在
System.out.println(set.remove("hello")); // false,删除失败,因为元素不存在
System.out.println(set.size()); // 2,一共两个元素

//HashSet无序
Set<String> set1 = new HashSet<>();
set1.add("apple");
set1.add("banana");
set1.add("pear");
set1.add("orange");
for (String s1 : set1) {
System.out.println("HashSet:"+s1);
}

//TreeSet有序
Set<String> set2 = new TreeSet<>();
set2.add("apple");
set2.add("banana");
set2.add("pear");
set2.add("orange");
for (String s2 : set2) {
System.out.println("TreeSet:"+s2);
}

/**Queue*/
Queue<String> q = new LinkedList<>();
// 添加3个元素到队列:
q.offer("apple");
q.offer("pear");
q.offer("banana");

q.add("apple"); //添加失败抛出异常
q.offer("apple"); //添加失败返回false
q.remove(); //获取队首并删除失败抛出异常
q.poll(); //获取队首并删除失败返回null
q.element(); //获取队首不删除失败抛出异常
q.peek(); //获取队首不删除失败返回null
// 从队列取出元素:
System.out.println(q.poll()); // apple
System.out.println(q.poll()); // pear
System.out.println(q.poll()); // banana
System.out.println(q.poll()); // null,因为队列是空的
// 队首永远都是apple,因为peek()不会删除它:
System.out.println(q.peek()); // apple
System.out.println(q.peek()); // apple
System.out.println(q.peek()); // apple

/**PriorityQueue 优先队列,顺序决定优先级*/
Queue<String> q2 = new PriorityQueue<>();
// 添加3个元素到队列:
q2.offer("apple");
q2.offer("pear");
q2.offer("banana");
System.out.println(q2.poll()); // apple
System.out.println(q2.poll()); // banana
System.out.println(q2.poll()); // pear
System.out.println(q2.poll()); // null,因为队列为空


Queue<User> q3 = new PriorityQueue<>(new UserComparator());
// 添加3个元素到队列:
q3.offer(new User("Bob", "A1"));
q3.offer(new User("Alice", "A2"));
q3.offer(new User("Boss", "V1"));
System.out.println(q3.poll()); // Boss/V1
System.out.println(q3.poll()); // Bob/A1
System.out.println(q3.poll()); // Alice/A2
System.out.println(q3.poll()); // null,因为队列为空

/**Deque 两头进,两头出*/
Deque<String> deque = new LinkedList<>();
deque.addFirst("a");
deque.addLast("a");
deque.offerFirst("C");
deque.offerLast("A");
deque.getFirst();
deque.getLast();
deque.peekFirst();
deque.peekLast();
deque.removeFirst();
deque.removeLast();
deque.pollFirst();
deque.pollLast();
System.out.println(deque.pollFirst()); // C, 剩下B -> A
System.out.println(deque.pollLast()); // B
System.out.println(deque.pollFirst()); // A
System.out.println(deque.pollFirst()); // null

/**Stack*/
/**Java中没有单独的接口,只能用Deque代替Stack,只调用push(),pop(),peek()*/
deque.push("a");
deque.push("c");
System.out.println(deque.pop());
System.out.println(deque.peek());

/**Collections*/
////创建空集合
List<String> list3 = Collections.emptyList();
System.out.println(list3);
////排序
List<String> list4 = new ArrayList<>();
list4.add("apple");
list4.add("pear");
list4.add("orange");
// 排序前:
System.out.println(list4);
Collections.sort(list4);
// 排序后:
System.out.println(list4);
////洗牌
List<Integer> list5 = new ArrayList<>();
for (int i=0; i<10; i++) {
list5.add(i);
}
// 洗牌前:
System.out.println(list5);
Collections.shuffle(list5);
// 洗牌后:
System.out.println(list5);
////不可变集合
List<String> mutable = new ArrayList<>();
mutable.add("apple");
mutable.add("pear");
// 变为不可变集合:
List<String> immutable = Collections.unmodifiableList(mutable);
mutable.add("orange");
try{
immutable.add("orange"); // UnsupportedOperationException!
}
catch (UnsupportedOperationException u){
System.out.println(u);
}
System.out.println(immutable);
}
}

版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 qubeijun的博客