java中集合總結(二)

java中使用集合是的一些技巧:


看到Array就是數組結構,有角標,查詢速度很快。

看到link就是鏈表結構:增刪速度快,而且有特有方法。addFirst; addLast;removeFirst(); removeLast();getFirst();getLast();

看到hash就是哈希表,就要想要哈希值,就要想到唯一性,就要想到存入到該結構的中的元素必須覆蓋hashCode,equals方法。

看到tree就是二叉樹,就要想到排序,就想要用到比較。

比較的兩種方式:

一個是繼承Comparable接口:覆蓋compareTo方法;

一個是使用比較器Comparator:覆蓋compare方法。

TreeSet:可以對Set集合中的元素進行排序。底層數據結構是二叉樹。保證元素唯一性的依據:compareTo方法return 0.
TreeSet排序的第一種方式:讓元素自身具備比較性。元素需要實現Comparable接口,覆蓋compareTo方法。也種方式也成爲元素的自然順序,或者叫做默認順序。

class Student implements Comparable//該接口強制讓學生具備比較性。
{
	private String name;
	private int age;

	Student(String name,int age)
	{
		this.name = name;
		this.age = age;
	}

	public int compareTo(Object obj)
	{
		
		if(!(obj instanceof Student))
			throw new RuntimeException("不是學生對象");
		Student s = (Student)obj;
		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;
        }
}
class TreeSet 
{
	public static void main(String[] args) 
	{
		TreeSet ts = new TreeSet();

		ts.add(new Student("lisi02",22));
		ts.add(new Student("lisi02",21));
		ts.add(new Student("lisi007",20));
		ts.add(new Student("lisi09",19));
		ts.add(new Student("lisi06",18));
		ts.add(new Student("lisi06",18));
		

		Iterator it = ts.iterator();
		while(it.hasNext())
		{
			Student stu = (Student)it.next();
			System.out.println(stu.getName()+"..."+stu.getAge());
		}
	}
}


TreeSet的第二種排序方式。當元素自身不具備比較性時,或者具備的比較性不是所需要的。這時就需要讓集合自身具備比較性。在集合初始化時,就有了比較方式。

class Student 
{
	private String name;
	private int age;

	Student(String name,int age)
	{
		this.name = name;
		this.age = age;
	}
      public String getName()
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>return name;


<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>public int getAge()
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>return age;
<span style="white-space:pre">	</span>}
}
class TreeSet 
{
	public static void main(String[] args) 
	{
		TreeSet ts = new TreeSet(new <span style="font-family: Arial, Helvetica, sans-serif;">MyCompare()</span>);

		ts.add(new Student("lisi02",22));
		ts.add(new Student("lisi02",21));
		ts.add(new Student("lisi007",20));
		ts.add(new Student("lisi09",19));
		ts.add(new Student("lisi06",18));
		ts.add(new Student("lisi06",18));
		
		Iterator it = ts.iterator();
		while(it.hasNext())
		{
			Student stu = (Student)it.next();
			System.out.println(stu.getName()+"..."+stu.getAge());
		}
	}
}

class MyCompare implements Comparator //定義比較器
{
	public int compare(Object o1,Object o2)
	{
		Student s1 = (Student)o1;
		Student s2 = (Student)o2;

		int num = s1.getName().compareTo(s2.getName());
		if(num==0)
		{
                  return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
			
		}

		
		return num;

	}
}

LinkedHashSet,LinkedHashMap:這兩個集合可以保證哈希表有存入順序和取出順序一致,保證哈希表有序。




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章