Skip to content

Instantly share code, notes, and snippets.

@daejinseok
Last active June 13, 2017 21:24
Show Gist options
  • Save daejinseok/712702f1c7fa7f7e3d4cb41d6bf20bd3 to your computer and use it in GitHub Desktop.
Save daejinseok/712702f1c7fa7f7e3d4cb41d6bf20bd3 to your computer and use it in GitHub Desktop.
Java Strong into HashSet
package test_daejin;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
public class test_set {
// Java 1.4버전 String을 HashSet으로 변환
public static HashSet strToSet( String s){
return new HashSet(Arrays.asList(s.trim().split("\\s*,\\s*")));
}
public static void main(String[] args) {
HashSet hs = strToSet(" 아빠, 엄마, 누나, 동생 ");
hs.add("아빠");
hs.add("누나");
Iterator i = hs.iterator();
while(i.hasNext()){
System.out.println( "|" + i.next() + "|");
}
}
}
/* output
|누나|
|동생|
|아빠|
|엄마|
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment