-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashsets.java
More file actions
58 lines (49 loc) · 1.93 KB
/
hashsets.java
File metadata and controls
58 lines (49 loc) · 1.93 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*************************************************************************
* Demonstrates working with Hashsets
*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The following are some of the HashSet methods:
set.add(key) -- adds the key to the set.
set.contains(key) -- returns true if the set has that key.
set.iterator() -- returns an iterator over the elements
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
String[] colors = {"white", "pink", "red", "green", "red", "orange","Aa","BB"};
hashset stores red only once, but it stores both Aa and BB
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
hashCode is computed by the following formula
s.charAt(0) * 31n-1 + s.charAt(1) * 31n-2 + ... + s.charAt(n-1)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
strings ""Aa" and "BB" have the same key: .
"Aa" = 'A' * 31 + 'a' = 2112
"BB" = 'B' * 31 + 'B' = 2112
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The order in which elements are returned depends on their hash codes.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Output:
Anmols-MacBook-Pro:leetcode anmolrastogi$ javac hashsets.java
Anmols-MacBook-Pro:leetcode anmolrastogi$ java hashsets
[red, orange, Aa, BB, pink, green, white]
Does it contain green? true
red orange Aa BB pink green white
red orange Aa BB pink green white
Anmols-MacBook-Pro:leetcode anmolrastogi$
*************************************************************************/
import java.util.*;
public class hashsets
{
public static void main(String[] args)
{
String[] colors = {"white", "pink", "red", "green", "red", "orange","Aa","BB"};
HashSet<String> hs = new HashSet<String>();
for(int i = 0; i < colors.length; i++) hs.add(colors[i]);
System.out.println(hs);
System.out.println("Does it contain green? " + hs.contains("green"));
Iterator itr = hs.iterator();
while(itr.hasNext())
System.out.print(itr.next()+" ");
System.out.println();
for(String str : hs)
System.out.print(str +" ");
System.out.println();
}
}