Java??HashSet????
???????????? ???????[ 2015/2/5 13:43:47 ] ????????Java ??????? HashSet
??????? HashSet?????
????HashSet ?????
???????? HashSet ???????????? HashMap ?????HashSet ?????? HashMap ?????????????????? HashSet ???????????? HashSet ?????????????????′??? Java????
????public class HashSet<E> extends AbstractSet<E>
????implements Set<E>?? Cloneable?? java.io.Serializable {
????// ??? HashMap ?? key ???? HashSet ????????? private transient HashMap<E??Object> map;
????// ???????????? Object ??????? HashMap ?? value private static final Object PRESENT = new Object(); ...
????// ????? HashSet????????????? HashMap public HashSet() {
????map = new HashMap<E??Object>(); }
????// ??????? initialCapacity??loadFactor ???? HashSet // ??????????????????? HashMap
????public HashSet(int initialCapacity?? float loadFactor) {
????map = new HashMap<E??Object>(initialCapacity?? loadFactor); }
????public HashSet(int initialCapacity) {
????map = new HashMap<E??Object>(initialCapacity); }
????HashSet(int initialCapacity?? float loadFactor?? boolean dummy) {
????map = new LinkedHashMap<E??Object>(initialCapacity ?? loadFactor); }
????// ???? map ?? keySet ?????????е? key public Iterator<E> iterator()
????{
????return map.keySet().iterator(); }
????// ???? HashMap ?? size() ???????? Entry ????????????? Set ?????????
????public int size() {
????return map.size(); }
????// ???? HashMap ?? isEmpty() ?ж?? HashSet ??????? // ?? HashMap ??????????? HashSet ???? public boolean isEmpty() {
????return map.isEmpty(); }
????// ???? HashMap ?? containsKey ?ж?????????? key // HashSet ?????????????? HashMap ?? key ??????? public boolean contains(Object o) {
????return map.containsKey(o); }
????// ??????????? HashSet ?У????????????? key ???? HashMap public boolean add(E e) {
????return map.put(e?? PRESENT) == null; }
????// ???? HashMap ?? remove ?????????? Entry???????? HashSet ?ж???????
????public boolean remove(Object o) {
????return map.remove(o)==PRESENT; }
????// ???? Map ?? clear ??????????? Entry???????? HashSet ?????????
????public void clear() {
????map.clear(); } ... }
???????????????????????HashSet ?????????????????? HashMap ???????洢???е???????????з??? HashSet ?е?????????????? HashMap ?? key ?????棬?? HashMap ?? value ??洢????? PRESENT?????????????? Object ????
????HashSet ????????????????????? HashMap ??????????????? HashSet ?? HashMap ??????????????????????????
????????HashSet<E>????
????HashSet<E>????????????????
????HashSet<string> s= HashSet<string>;
???????s?????????洢 string ?????????????s ???????add(String s)??????string??????????????????С??????????е?????????????????????????????????????????????b?????????е?????????????set.add(b)????????Ч??? ????HashSet<E>?е???????
?????? public boolean add(E o) —— ??????????????????????????????л?
???????????????????????????????
??????????
????o - ????????????е????? ?????
????????ü????????????????????? true??
?????? public void clear() ——??????????????????κ?????
?????? public boolean contains(Object o) —— ?ж????????????????????????????
??????????
????o - ?????????е?????????????????
?????????
???????????????????????????? true??
?????? public boolean isEmpty()—— ?ж???????????
?????????
??????????????????κ????????? true??
?????? public boolean remove(Object o) ——????????????????????
??????????
????o - ????????????????????????????????
?????????
????????????????????????? true??
?????? public int size () —— ??????????????????
?????????
??????????е????????????????????????
?????? Object [ ] toArray() —— ?????????????????У?????????????顣
?????? boolean containsAll (HashSet set) —— ?ж??????????????????????????
?????? public Object clone () —— ?????????????????????????????????
???????????????????е????????????? ?????
?????????????????
????????????
import java.util.HashSet;
class Student{ String name; int score;
Student(String name?? int score){ this.name = name; this.score = score; } }
public class TestHashSet{
public static void main(String[] args) {
Student zh = new Student("????"??77); Student li = new Student("????"??68); Student wa = new Student("????"??67);
HashSet<Student> set = new HashSet<Student>(); HashSet<Student> subset = new HashSet<Student>();
System.out.println("Before add(zh):"+set.add(zh)); set.add(zh);
System.out.println("After add(zh):"+set.add(zh)); //add????????? set.add(wa); set.add(li);
subset.add(wa); subset.add(li);
System.out.println("Is subset empty? "+subset.isEmpty()); //isempty?????????
if (set.contains(wa)){
System.out.println("????set?к??У?"+wa.name); //contains????????? }
if(set.containsAll(subset)){
System.out.println("????set????????subset"); }
int number = subset.size(); //size????????? System.out.println("????subset????"+number+"??????");
Object s[] = subset.toArray(); //toArray ?????????
for(int i=0; i<s.length; i++){
System.out.printf("??????%s????????%d
"?? ((Student)s[i]).name??((Student)s[i]).score); }
subset.remove(wa); //remove????????? System.out.println("After implement remove on subset");
System.out.println("????subset????"+subset.size()+"??????");
Object a[] = subset.toArray();
for(int i=0; i<a.length; i++){
System.out.printf("??????%s????????%d
"?? ((Student)a[i]).name??((Student)a[i]).score); } } }
??????????н??
????Before add(zh):true After add(zh):false Is subset empty? false ????set?к??У????? ????set????????subset ????subset????2?????? ?????????????????68 ?????????壬??????67
????After implement remove on subset ????subset????1?????? ?????????????????68
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11