1 30 31 32 package org.hsqldb.lib; 33 34 import org.hsqldb.store.BaseHashMap; 35 36 43 public class HashSet extends BaseHashMap implements Set { 44 45 public HashSet() { 46 this(16, 0.75f); 47 } 48 49 public HashSet(int initialCapacity) throws IllegalArgumentException { 50 this(initialCapacity, 0.75f); 51 } 52 53 public HashSet(int initialCapacity, 54 float loadFactor) throws IllegalArgumentException { 55 super(initialCapacity, loadFactor, BaseHashMap.objectKeyOrValue, 56 BaseHashMap.noKeyOrValue, false); 57 } 58 59 public boolean contains(Object key) { 60 return super.containsKey(key); 61 } 62 63 public Object get(Object key) { 64 65 int lookup = getLookup(key, key.hashCode()); 66 67 if (lookup < 0) { 68 return null; 69 } else { 70 return objectKeyTable[lookup]; 71 } 72 } 73 74 public boolean add(Object key) { 75 76 int oldSize = size(); 77 78 super.addOrRemove(0, 0, key, null, false); 79 80 return oldSize != size(); 81 } 82 83 public boolean addAll(Collection c) { 84 85 int oldSize = size(); 86 Iterator it = c.iterator(); 87 88 while (it.hasNext()) { 89 add(it.next()); 90 } 91 92 return oldSize != size(); 93 } 94 95 public boolean addAll(Object [] keys) { 96 97 boolean changed = false; 98 99 for (int i = 0; i < keys.length; i++) { 100 if (add(keys[i])) { 101 changed = true; 102 } 103 } 104 105 return changed; 106 } 107 108 public boolean remove(Object key) { 109 110 int oldSize = size(); 111 112 super.removeObject(key); 113 114 return oldSize != size(); 115 } 116 117 public Object [] toArray(Object [] a) { 118 119 if (a == null || a.length < size()) { 120 a = new Object [size()]; 121 } 122 123 Iterator it = iterator(); 124 125 for (int i = 0; it.hasNext(); i++) { 126 a[i] = it.next(); 127 } 128 129 return a; 130 } 131 132 public Iterator iterator() { 133 return new BaseHashIterator(true); 134 } 135 136 140 public String toString() { 141 142 Iterator it = iterator(); 143 StringBuffer sb = new StringBuffer (); 144 145 while (it.hasNext()) { 146 if (sb.length() > 0) { 147 sb.append(", "); 148 } else { 149 sb.append('['); 150 } 151 152 sb.append(it.next()); 153 } 154 155 return sb.toString() + ']'; 156 } 157 } 158 | Popular Tags |