Skip to content

Commit ca87ba3

Browse files
committedNov 6, 2017
JavaSE:
1、Set集合 2、Map集合
1 parent dc09e39 commit ca87ba3

File tree

4 files changed

+498
-38
lines changed

4 files changed

+498
-38
lines changed
 

‎.idea/uiDesigner.xml

+124
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/workspace.xml

+299-38
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/JavaSE_Collection_Set.java

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import java.util.*;
2+
3+
/**
4+
* @Author:L1ANN
5+
* @Description:
6+
* @Date:Created in 19:38 2017/11/6
7+
* @Modified By:
8+
*/
9+
public class JavaSE_Collection_Set {
10+
public static void main(String[] args){
11+
//创建一个List集合
12+
List<String> list = new ArrayList<>();
13+
list.add("a");
14+
list.add("b");
15+
list.add("c");
16+
list.add("d");
17+
list.add("a");
18+
list.add("d");
19+
//创建一个Set集合
20+
Set<String> set = new HashSet<>();
21+
//将List中的元素放入Set中
22+
set.addAll(list);
23+
set.add("111");
24+
set.add("222");
25+
set.remove("111");
26+
Iterator<String> iterator = set.iterator();
27+
while(iterator.hasNext()){
28+
System.out.print(iterator.next()+"\t");
29+
}
30+
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package Serializable;
2+
3+
import java.util.HashMap;
4+
import java.util.Iterator;
5+
import java.util.Map;
6+
7+
/**
8+
* @Author:L1ANN
9+
* @Description: Map的四种遍历方式
10+
* @Date:Created in 20:14 2017/11/6
11+
* @Modified By:
12+
*/
13+
public class JavaSE_Collection_Map {
14+
public static void main(String[] args){
15+
Map<String,String> map = new HashMap<String,String>();
16+
map.put("1","小明");
17+
map.put("2","小丽");
18+
map.put("3","小美");
19+
map.put("4","小芳");
20+
21+
//第一种遍历方式,通过加强for循环map.keySet(),然后通过键key得到value值
22+
for(String s:map.keySet()){
23+
System.out.print("key:"+s+" value:"+map.get(s)+"\t");
24+
}
25+
System.out.println();
26+
//第二种遍历方式,通过加强for循环map.values()
27+
for(String s:map.values()){
28+
System.out.print("value:"+s+"\t");
29+
}
30+
System.out.println();
31+
//第三种遍历方式,通过加强for循环Map.Entry<String,String>
32+
for(Map.Entry<String,String> entry:map.entrySet()){
33+
System.out.print("key:"+entry.getKey()+" value:"+entry.getValue()+"\t");
34+
}
35+
System.out.println();
36+
//第四种遍历方式,通过Iterator遍历获取
37+
Iterator<Map.Entry<String,String>> it = map.entrySet().iterator();
38+
while(it.hasNext()){
39+
Map.Entry<String,String> entry = it.next();
40+
System.out.print("key:"+entry.getKey()+" value:"+entry.getValue()+"\t");
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)
Please sign in to comment.