상세 컨텐츠

본문 제목

Generic Method의 사용

Java

by techbard 2016. 7. 25. 20:09

본문

반응형



# Main.java


import java.util.Arrays;

import java.util.HashSet;

import java.util.Set;


public class Main {

    

    public static <E> Set<E> union(Set<E> s1, Set<E> s2) {

Set<E> result = new HashSet<>(s1);

result.addAll(s2);

return result;

    } 


    public static void main(String[] args) {

Set<String> guys = new HashSet<>(Arrays.asList("Tom", "Dick", "Harry"));

Set<String> numbers = new HashSet<>(Arrays.asList("1", "2", "3"));

Set<String> mix = union(guys, numbers);

System.out.println(mix);

    }

}



# 결과


[1, 2, Tom, 3, Harry, Dick]


반응형

관련글 더보기

댓글 영역