Skip to content

Commit 0edacda

Browse files
committed
feat: add java collection demo
1 parent 4ab63d2 commit 0edacda

7 files changed

Lines changed: 208 additions & 0 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.wdbyte.collection;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* @author niulang
8+
*/
9+
public class CollectionTest {
10+
public static void main(String[] args) {
11+
List<String> arrayList = new ArrayList<>();
12+
// 向 List 中添加元素
13+
arrayList.add("www");
14+
arrayList.add("wdbyte");
15+
arrayList.add("com");
16+
arrayList.add("com");
17+
// 输出
18+
System.out.println(arrayList);
19+
}
20+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.wdbyte.collection;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashSet;
5+
import java.util.LinkedList;
6+
import java.util.List;
7+
import java.util.Set;
8+
import java.util.Stack;
9+
import java.util.TreeSet;
10+
11+
/**
12+
* @author niulang
13+
*/
14+
public class CollectionTest2 {
15+
public static void main(String[] args) {
16+
List<String> arrayList = new ArrayList<>();
17+
List<String> linkedList = new LinkedList<>();
18+
Set<String> hashSet = new HashSet<>();
19+
Set<String> treeSet = new TreeSet<>();
20+
Stack<String> stack = new Stack<>();
21+
// 添加元素
22+
arrayList.add("wdbyte.com");
23+
linkedList.add("wdbyte.com");
24+
hashSet.add("wdbyte.com");
25+
treeSet.add("wdbyte.com");
26+
stack.add("wdbyte.com");
27+
// 输出
28+
System.out.println(arrayList);
29+
System.out.println(linkedList);
30+
System.out.println(hashSet);
31+
System.out.println(treeSet);
32+
System.out.println(stack);
33+
}
34+
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.wdbyte.collection;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
import java.util.Iterator;
6+
import java.util.List;
7+
8+
/**
9+
* @author niulang
10+
*/
11+
public class CollectionTest3 {
12+
public static void main(String[] args) {
13+
List<String> arrayList = new ArrayList<>();
14+
// 向 List 中添加元素
15+
arrayList.add("www");
16+
arrayList.add("wdbyte");
17+
arrayList.add("com");
18+
// 普通遍历
19+
for (int i = 0; i < arrayList.size(); i++) {
20+
System.out.println(arrayList.get(0));
21+
}
22+
System.out.println("--------");
23+
// iterator 迭代器遍历
24+
Iterator<String> iterator = arrayList.iterator();
25+
while (iterator.hasNext()) {
26+
System.out.println(iterator.next());
27+
}
28+
}
29+
30+
public void printCollection(Collection collection) {
31+
for (Object o : collection) {
32+
System.out.println(o);
33+
}
34+
}
35+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.wdbyte.collection;
2+
3+
import java.time.LocalDate;
4+
import java.util.ArrayList;
5+
import java.util.HashSet;
6+
import java.util.Iterator;
7+
import java.util.List;
8+
9+
/**
10+
* @author niulang
11+
*/
12+
public class CollectionTest4 {
13+
public static void main(String[] args) {
14+
List<String> arrayList = new ArrayList<>();
15+
// 向 List 中添加元素
16+
arrayList.add("www");
17+
arrayList.add("wdbyte");
18+
arrayList.add("com");
19+
20+
HashSet<String> hashSet = new HashSet<>();
21+
hashSet.addAll(arrayList);
22+
23+
printCollection(arrayList.iterator());
24+
System.out.println("--------------");
25+
printCollection(hashSet.iterator());
26+
}
27+
28+
public static void printCollection(Iterator<String> iterator) {
29+
// 当前日期
30+
LocalDate now = LocalDate.now();
31+
while (iterator.hasNext()) {
32+
String obj = iterator.next();
33+
System.out.println(now + ":" + obj.toString());
34+
}
35+
}
36+
37+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.wdbyte.collection;
2+
3+
import java.time.LocalDate;
4+
import java.util.ArrayList;
5+
import java.util.HashSet;
6+
import java.util.Iterator;
7+
import java.util.List;
8+
9+
/**
10+
* @author niulang
11+
*/
12+
public class CollectionTest5 {
13+
public static void main(String[] args) {
14+
List<String> arrayList = new ArrayList<>();
15+
// 向 List 中添加元素
16+
arrayList.add("www");
17+
arrayList.add("wdbyte");
18+
arrayList.add("com");
19+
HashSet<String> hashSet = new HashSet<>();
20+
hashSet.addAll(arrayList);
21+
22+
printCollection(arrayList);
23+
printCollection(hashSet);
24+
}
25+
public static void printCollection(Iterable<String> iterable) {
26+
// 当前日期
27+
LocalDate now = LocalDate.now();
28+
Iterator<String> iterator = iterable.iterator();
29+
while (iterator.hasNext()) {
30+
String obj = iterator.next();
31+
System.out.println(now + ":" + obj.toString());
32+
}
33+
}
34+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.wdbyte.collection;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* @author niulang
8+
*/
9+
public class MapTest {
10+
public static void main(String[] args) {
11+
Map<String, String> hashMap = new HashMap<>();
12+
// 添加元素
13+
hashMap.put("site","www.wdbyte.com");
14+
hashMap.put("author","程序猿阿朗");
15+
hashMap.put("github","github.com/niumoo");
16+
// 获取元素
17+
System.out.println(hashMap.get("github"));
18+
// 输出全部元素
19+
System.out.println(hashMap);
20+
}
21+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.wdbyte.collection;
2+
3+
import java.util.HashMap;
4+
import java.util.LinkedHashMap;
5+
import java.util.Map;
6+
7+
/**
8+
* @author niulang
9+
*/
10+
public class MapTest2 {
11+
public static void main(String[] args) {
12+
Map<String, String> hashMap = new HashMap<>();
13+
Map<String, String> linkedHashMap = new LinkedHashMap<>();
14+
// 添加元素
15+
hashMap.put("site", "www.wdbyte.com");
16+
hashMap.put("author", "程序猿阿朗");
17+
hashMap.put("github", "github.com/niumoo");
18+
linkedHashMap.put("site", "www.wdbyte.com");
19+
linkedHashMap.put("author", "程序猿阿朗");
20+
linkedHashMap.put("github", "github.com/niumoo");
21+
22+
// 输出全部元素
23+
System.out.println(hashMap);
24+
System.out.println(linkedHashMap);
25+
}
26+
}

0 commit comments

Comments
 (0)