Skip to content

Commit 965f8c9

Browse files
authored
Create GenericMethodEx.java
1 parent b8fce65 commit 965f8c9

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

generics/GenericMethodEx.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.zetcode;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
public class GenericMethodEx {
7+
8+
public static void main(String[] args) {
9+
10+
Set<String> words = new HashSet<>();
11+
words.add("blue");
12+
words.add("pen");
13+
words.add("dog");
14+
words.add("narrow");
15+
16+
Set<String> words2 = new HashSet<>();
17+
words.add("blue");
18+
words.add("pencil");
19+
words.add("forest");
20+
words.add("narrow");
21+
22+
Set<String> res = union(words, words2);
23+
System.out.println(res);
24+
}
25+
26+
public static <E> Set<E> union(Set<E> s1, Set<E> s2) {
27+
28+
Set<E> result = new HashSet<>(s1);
29+
result.addAll(s2);
30+
31+
return result;
32+
}
33+
}

0 commit comments

Comments
 (0)