Java에서 HashSet 항목 삭제

이 코드는 remove() 삭제할 객체를 인수로 사용합니다. 차이점을 확인하기 위해 삭제 전후의 HashSet 목록을 표시합니다.

import java.util.HashSet; 

public class HashSetremove{

public static void main(String[] args) {

HashSet< 문자열> hashset = 새로운 HashSet< 문자열> ();
hashset.add("v1");
hashset.add("v2");
hashset.add("v3");
hashset.add("v4");
hashset.add("v5");

System.out.println("제거 전 HashSet: "+hashset);

개체 e = "v2";

부울 b = hashset.remove("v2");
System.out.println("제거된 "+e+" 요소: "+b);

System.out.println(""+e+"를 제거한 후 HashSet: "+hashset);
}
}
Output:

HashSet 제거하기 전: [v1, v5, v4, v3, v2]
v2 제거된 항목: true
HashSet v2 제거 후: [v1, v5, v4, v3]