我是靠谱客的博主 个性奇迹,这篇文章主要介绍Java集合编程题训练(一),现在分享给大家,希望可以做个参考。

1.遍历一个集合,删除所有与s相同的元素
注意:不要使用for循环遍历删除,会出现删除不干净的情况

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/** 删除集合中指定的元素 @param list 集合 @param s 指定的元素 */ public static void removes(List<String> list, String s) { Iterator iterator = list.iterator(); while (iterator.hasNext()) { String str = (String) iterator.next(); if (str.equals(s)) { iterator.remove(); } } }

2.如何判断两个集合是否有交集,并打印出他们的交集

复制代码
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
public static void main(String[] args) { HashSet<String> hs=new HashSet<String>(); hs.add("George"); hs.add("Jim"); hs.add("Blake"); hs.add("Kevin"); hs.add("Mecheal"); hs.add("John"); HashSet<String> hs2=new HashSet<String>(); hs2.add("George"); hs2.add("Kate"); hs2.add("Kevin"); hs2.add("Mecheal"); hs2.add("Ryan"); hs.retainAll(hs2);//retainAll()的方法返回时boolean,但不能作为又没有交集的判断,判断有没有交集要通过hs的size()。 if (hs.size()==0){ System.out.println("没有交集"); }else{ System.out.println("有交集"); } for (String s : hs) { System.out.println(s); } }

3.各种集合的遍历方法
①List

复制代码
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
public static void main(String[] args) { List<String> list=new ArrayList<>(); list.add("George"); list.add("Jim"); list.add("Blake"); list.add("Kevin"); list.add("Mecheal"); list.add("John"); //for循环 for (int i = 0; i < list.size(); i++) { String s = list.get(i); System.out.println(s); } //foreach for (String s : list) { System.out.println(s); } //迭代器遍历 Iterator<String> iterator = list.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } }

②Set

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public static void main(String[] args) { Set<String> list=new HashSet<>(); list.add("George"); list.add("Jim"); list.add("Blake"); list.add("Kevin"); list.add("Mecheal"); list.add("John"); //foreach for (String s : list) { System.out.println(s); } //迭代器遍历 Iterator<String> iterator = list.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); }

③Map

复制代码
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
public static void main(String[] args) { Map<String, String> map = new HashMap<>(); map.put("name", "Tom"); map.put("age", "20"); map.put("address", "beijing"); for (String key : map.keySet()) { String value = map.get(key); System.out.println(key+"==="+value); } Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry<String, String> entry = iterator.next(); String key = entry.getKey(); String value = entry.getValue(); System.out.println(key+"==="+value); } for (Map.Entry<String,String> entry: map.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); System.out.println(key+"==="+value); } for (String value:map.values()) { System.out.println(value); } }

最后

以上就是个性奇迹最近收集整理的关于Java集合编程题训练(一)的全部内容,更多相关Java集合编程题训练(一)内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(62)

评论列表共有 0 条评论

立即
投稿
返回
顶部