KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > util > HashSet


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package java.util;
17
18 /**
19  * Implements a set in terms of a hash table.
20  */

21 public class HashSet extends AbstractSet JavaDoc implements Set JavaDoc, Cloneable JavaDoc {
22
23   private transient HashMap JavaDoc map;
24
25   public HashSet() {
26     map = new HashMap JavaDoc();
27   }
28
29   public HashSet(Collection JavaDoc c) {
30     map = new HashMap JavaDoc(c.size());
31     addAll(c);
32   }
33
34   public HashSet(int initialCapacity) {
35     map = new HashMap JavaDoc(initialCapacity);
36   }
37
38   public HashSet(int initialCapacity, float loadFactor) {
39     map = new HashMap JavaDoc(initialCapacity, loadFactor);
40   }
41
42   public boolean add(Object JavaDoc o) {
43     Object JavaDoc old = map.put(o, Boolean.valueOf(true));
44     return (old == null);
45   }
46
47   public void clear() {
48     map.clear();
49   }
50
51   public Object JavaDoc clone() {
52     return new HashSet JavaDoc(this);
53   }
54
55   public boolean contains(Object JavaDoc o) {
56     return map.containsKey(o);
57   }
58
59   public boolean isEmpty() {
60     return map.isEmpty();
61   }
62
63   public Iterator JavaDoc iterator() {
64     return map.keySet().iterator();
65   }
66
67   public boolean remove(Object JavaDoc o) {
68     return (map.remove(o) != null);
69   }
70
71   public int size() {
72     return map.size();
73   }
74
75   public String JavaDoc toString() {
76     return map.keySet().toString();
77   }
78
79 }
80
Popular Tags