榛戦┈绋嬪簭鍛?闆嗗悎二-Set闆嗗悎

榛戦┈绋嬪簭鍛?闆嗗悎2-Set闆嗗悎

----------------------聽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鍩硅銆佹湡寰呬笌鎮ㄤ氦娴侊紒 ----------------------