聽
----------------------聽android鍩硅銆?/span>java鍩硅銆佹湡寰呬笌鎮ㄤ氦娴侊紒 ----------------------
聽
聽
闆嗗悎2-Set闆嗗悎
聽
聽
聽
Set闆嗗悎鐨勫姛鑳藉拰Collection鏄竴鑷寸殑銆?/p>
|--Set锛氬厓绱犳槸鏃犲簭(瀛樺叆鍜屽彇鍑虹殑椤哄簭涓嶄竴瀹氫竴鑷?锛屽厓绱犱笉鍙互閲嶅銆傘€?/p>
|--HashSet:搴曞眰鏁版嵁缁撴瀯鏄搱甯岃〃銆傛槸绾跨▼涓嶅畨鍏ㄧ殑銆備笉鍚屾銆?/p>
HashSet鏄浣曚繚璇佸厓绱犲敮涓€鎬х殑鍛紵
鏄€氳繃鍏冪礌鐨勪袱涓柟娉曪紝hashCode鍜宔quals鏉ュ畬鎴愩€?/p>
濡傛灉鍏冪礌鐨凥ashCode鍊肩浉鍚岋紝鎵嶄細鍒ゆ柇equals鏄惁涓簍rue銆?/p>
濡傛灉鍏冪礌鐨刪ashcode鍊间笉鍚岋紝涓嶄細璋冪敤equals銆?/p>
聽
娉ㄦ剰,瀵逛簬鍒ゆ柇鍏冪礌鏄惁瀛樺湪锛屼互鍙婂垹闄ょ瓑鎿嶄綔锛屼緷璧栫殑鏂规硶鏄厓绱犵殑hashcode鍜宔quals鏂规硶銆?/p>
聽
聽
|--TreeSet锛氬彲浠ュset闆嗗悎涓殑鍏冪礌杩涜鎺掑簭銆?/p>
聽
搴曞眰鏁版嵁缁撴瀯鏄簩鍙夋爲銆?/p>
淇濊瘉鍏冪礌鍞竴鎬х殑渚濇嵁锛?/p>
compareTo鏂规硶鐩稿悓return 0.
聽
TreeSet鎺掑簭鐨勭涓€绉嶆柟寮忥細璁╁厓绱犺嚜韬叿澶囨瘮杈冩€с€?/p>
鍏冪礌闇€瑕佸疄鐜癈omparable鎺ュ彛锛岃鐩朿ompareTo鏂规硶銆?/p>
涔熺鏂瑰紡涔熸垚涓哄厓绱犵殑鑷劧椤哄簭锛屾垨鑰呭彨鍋氶粯璁ら『搴忋€?/p>
聽
TreeSet鐨勭浜岀鎺掑簭鏂瑰紡銆?/p>
褰撳厓绱犺嚜韬笉鍏峰姣旇緝鎬ф椂锛屾垨鑰呭叿澶囩殑姣旇緝鎬т笉鏄墍闇€瑕佺殑銆?/p>
杩欐椂灏遍渶瑕佽闆嗗悎鑷韩鍏峰姣旇緝鎬с€?/p>
鍦ㄩ泦鍚堝垵濮嬪寲鏃讹紝灏辨湁浜嗘瘮杈冩柟寮忋€?/p>
聽
import java.util.*; class HashSetDemo { public static void sop(Object obj) { System.out.println(obj); } public static void main(String[] args) { HashSet hs = new HashSet();//鍒涘缓set闆嗗悎 sop(hs.add("java01"));//鍦ㄩ泦鍚堜腑娣诲姞鍏冪礌 sop(hs.add("java01")); hs.add("java02"); hs.add("java03"); hs.add("java03"); hs.add("java04"); Iterator it = hs.iterator();//浣跨敤杩唬鍣ㄥ闆嗗悎鍏冪礌杩涜鑾峰彇 while(it.hasNext()) { sop(it.next()); } } }
聽
聽
寰€hashSet闆嗗悎涓瓨鍏ヨ嚜瀹氬璞?/p>
濮撳悕鍜屽勾榫勭浉鍚屼负鍚屼竴涓汉锛岄噸澶嶅厓绱犮€?/p>
import java.util.*; class HashSetTest { public static void sop(Object obj) { System.out.println(obj); } public static void main(String[] args) { HashSet hs = new HashSet();//鍒涘缓涓€涓泦鍚? hs.add(new Person("a1",11));//鍦ㄩ泦鍚堜腑娣诲姞鑻ュ共鍏冪礌 hs.add(new Person("a2",12)); hs.add(new Person("a3",13)); hs.add(new Person("a2",12)); hs.add(new Person("a4",14)); //sop("a1:"+hs.contains(new Person("a2",12))); hs.remove(new Person("a4",13)); Iterator it = hs.iterator();//鑾峰彇杩唬鍣? while(it.hasNext()) { Person p = (Person)it.next();//鍥犱负瑕佷娇鐢ㄩ泦鍚堜腑鍏冪礌鐨勭壒鏈夋柟娉曪紝瑕佽繘琛屽己杞? sop(p.getName()+"::"+p.getAge());//鎵撳嵃鍏冪礌鐨勫睘鎬? } } } class Person//鍒涘缓涓€涓猵erson绫? { private String name;//绫讳腑鏈夎嫢骞叉垚鍛樺睘鎬? private int age; Person(String name,int age) { this.name = name; this.age = age; } //鍥犲瓨鍏ュ埌hashset涓紝鎵€浠ヨ鐩杊ashcode鍜宔quals鏂规硶 public int hashCode() { System.out.println(this.name+"....hashCode"); return name.hashCode()+age*37;//鍝堝笇鍊兼牴鎹鍚嶅拰骞撮緞璁$畻 } public boolean equals(Object obj) { if(!(obj instanceof Person))//鍒ゆ柇鏄惁鏄悓涓€绫诲瀷 return false; Person p = (Person)obj; System.out.println(this.name+"...equals.."+p.name); return this.name.equals(p.name) && this.age == p.age;//濡傛灉濮撳悕鍜屽勾榫勭浉鍚岋紝瑙嗕负鍚屼竴涓汉 } //瀹氫箟鑾峰彇灞炴€х殑鏂规硶 public String getName() { return name; } public int getAge() { return age; } }
聽
聽
聽
聽
Treeset锛?/p>
聽
聽
闇€姹傦細
寰€TreeSet闆嗗悎涓瓨鍌ㄨ嚜瀹氫箟瀵硅薄瀛︾敓銆?/p>
鎯虫寜鐓у鐢熺殑骞撮緞杩涜鎺掑簭銆?/p>
聽
聽
璁颁綇锛屾帓搴忔椂锛屽綋涓昏鏉′欢鐩稿悓鏃讹紝涓€瀹氬垽鏂竴涓嬫瑕佹潯浠躲€?/p>
聽
聽
褰撳厓绱犺嚜韬笉鍏峰姣旇緝鎬э紝鎴栬€呭叿澶囩殑姣旇緝鎬т笉鏄墍闇€瑕佺殑銆?/p>
杩欐椂闇€瑕佽瀹瑰櫒鑷韩鍏峰姣旇緝鎬с€?/p>
瀹氫箟浜嗘瘮杈冨櫒锛屽皢姣旇緝鍣ㄥ璞′綔涓哄弬鏁颁紶閫掔粰TreeSet闆嗗悎鐨勬瀯閫犲嚱鏁般€?/p>
聽
褰撲袱绉嶆帓搴忛兘瀛樺湪鏃讹紝浠ユ瘮杈冨櫒涓轰富銆?/p>
聽
瀹氫箟涓€涓被锛屽疄鐜癈omparator鎺ュ彛锛岃鐩朿ompare鏂规硶銆?/p>
聽
class TreeSetDemo { public static void main(String[] args) { TreeSet ts = new TreeSet();//鍒涘缓涓€涓猼reeSet闆嗗悎 ts.add(new Student("lisi02",22));//鍦ㄩ泦鍚堜腑娣诲姞鑻ュ共鍏冪礌 ts.add(new Student("lisi007",20)); ts.add(new Student("lisi09",19)); ts.add(new Student("lisi08",19)); //ts.add(new Student("lisi007",20)); //ts.add(new Student("lisi01",40)); Iterator it = ts.iterator();//鑾峰彇杩唬鍣? while(it.hasNext()) { Student stu = (Student)it.next();//寮鸿浆 System.out.println(stu.getName()+"..."+stu.getAge());//鑾峰彇鍏冪礌鐨勬垚鍛樺睘鎬? } } } class Student implements Comparable//璇ユ帴鍙e己鍒惰瀛︾敓鍏峰姣旇緝鎬с€? { private String name; private int age; Student(String name,int age) { this.name = name; this.age = age; } //瑕嗙洊compareTo鏂规硶锛屾槸璇ョ被鍏锋湁涓€瀹氱殑姣旇緝瑙勫垯 public int compareTo(Object obj) { if(!(obj instanceof Student))//鍒ゆ柇姣旇緝鐨勬槸鍚︽槸鍚岀被瀵硅薄 throw new RuntimeException("涓嶆槸瀛︾敓瀵硅薄"); Student s = (Student)obj; System.out.println(this.name+"....compareto....."+s.name); if(this.age>s.age)//姣旇緝骞撮緞 return 1; if(this.age==s.age) { return this.name.compareTo(s.name);//濡傛灉骞撮緞鐩稿悓鎸夌収濮撳悕鎺掑簭 } return -1; /**/ } public String getName() { return name; } public int getAge() { return age; } }
聽
聽
聽
聽
----------------------聽android鍩硅銆?/span>java鍩硅銆佹湡寰呬笌鎮ㄤ氦娴侊紒 ----------------------