티스토리 뷰

JAVA/PRIMER

[JAVA] HashSet

yulrang 2018. 4. 19. 14:12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import·java.util.HashSet;¬
import·java.util.LinkedList;¬
¬
//·HashSet··:·Set()··¬
//·————————————:········->·index···X¬
//——————————————:····¬
//······->·iterator·¬
¬
public·class·HashSet1·{¬
¬
————public·static·void·main(String[]·args)·{¬
————————LinkedList<Integer>·ll·=·new·LinkedList<Integer>();¬
————————HashSet<Integer>·hs·=·new·HashSet<Integer>();¬
————————¬
————————ll.add(1);——ll.add(2);——ll.add(3);——ll.add(1);¬
————————hs.add(1);——hs.add(2);——hs.add(3);——hs.add(1);¬
————————¬
————————System.out.println(ll);¬
————————System.out.println(hs);¬
————}¬
¬
}¬
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[1, 2, 3, 1]
[1, 2, 3]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import·java.util.HashSet;¬
¬
¬
public·class·HashSet2·{¬
¬
————public·static·void·main(String[]·args)·{¬
————————HashSet<String>·hs·=·new·HashSet<String>();¬
————¬
————————//··¬
————————hs.add("");———hs.add("");¬
————————¬
————————//···(·····)¬
————————hs.remove("");¬
————————¬
————————//···¬
————————System.out.println(·hs.size()·);¬
————————¬
————————//······true/false¬
————————System.out.println(·hs.contains("")·);¬
————————¬
————————//···true/false¬
————————System.out.println(·hs.isEmpty()·);¬
————————¬
————————System.out.println(hs);¬
————}¬
¬
}¬
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
false
false
[화]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import·java.util.HashSet;¬
¬
¬
public·class·HashSet3·{¬
¬
————public·static·void·main(String[]·args)·{¬
————————HashSet<String>·h1·=·new·HashSet<String>();¬
————————HashSet<String>·h2·=·new·HashSet<String>();¬
————————¬
————————h1.add("");———h1.add("");———h1.add("");¬
————————¬
————————//·h1··h2··¬
————————h2.addAll(h1);¬
————————¬
————————h1.add("");¬
————————¬
————————//·h1·h2····true/false¬
————————//·==·h2·h1·¬
————————System.out.println(·h1.containsAll(h2)·);¬
————————System.out.println(·h2.containsAll(h1)·);¬
————————¬
————————System.out.println(h1);¬
————————System.out.println(h2);¬
————}¬
¬
}¬
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
true
false
[월, 화, 수, 목]
[월, 화, 수]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import·java.util.HashSet;¬
import·java.util.Iterator;¬
¬
//······->·iterator·¬
public·class·HashSet4·{¬
¬
————public·static·void·main(String[]·args)·{¬
————————HashSet<String>·h1·=·new·HashSet<String>();¬
————————HashSet<String>·h2·=·new·HashSet<String>();¬
————————h1.add("");———h1.add("");———h1.add("");¬
————————h2.add("");———h2.add("");———h2.add("");¬
————————¬
————————//·Iterator()¬
————————//······¬
————————//·····,···¬
¬
————————Iterator·i·=··h1.iterator();————//··¬
————————Iterator·i2·=··h1.iterator();¬
————————¬
————————System.out.print(·i.next()·);———//····¬
————————System.out.print(·i.next()·);¬
————————System.out.print(·i.next()·);———//······¬
————————¬
————————System.out.println();¬
————————¬
————————while(·i2.hasNext()·){¬
————————————System.out.print(·i2.next()·);¬
————————}¬
————}¬
¬
}¬
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
월화수
월화수


'JAVA > PRIMER' 카테고리의 다른 글

[JAVA] 예외처리  (0) 2018.04.19
[JAVA] HashMap  (0) 2018.04.19
[JAVA] LinkedList  (0) 2018.04.19
[JAVA] ArrayList  (0) 2018.04.19
[JAVA] Wrapper클래스  (0) 2018.04.19
댓글