Set과 List

개비의 끄적끄적·2023년 3월 4일
  • 중복 요소가 있는 List를 Set로 변환 후, 다시 List로 변환하면 중복이 사라짐!
	List<String> list = new ArrayList<String>();
   list.add("셍");
    list.add("숨");
    list.add("갭");
    list.add("봉");
    list.add("뇽");
    list.add("뇽");
  	Set<String> set = new HashSet<String>(list);  // Set에 list 전달해서 Set로 변환~_~
    list = new ArrayList<String>(set);			  // 다시 List로 변환
  	System.out.println(list);					  // [갭, 숨, 뇽, 셍, 봉]
  	// 중복 요소 '뇽'이 없어짐.
profile
앗녕하세요! 블비의 개적발적🐾

0개의 댓글