java常用集合類詳解(有例子,集合類糊塗的來看!)
阿新 • • 發佈:2018-12-25
TreeSet:
TreeSet是依靠TreeMap來實現的.
TreeSet是一個有序集合,TreeSet中元素將按照升序排列,
預設是按照自然排序進行排列,意味著TreeSet中元素要
實現Comparable介面.
我們可以在構造TreeSet物件時,傳遞實現了Comparator介面
的比較器物件.
java.util包當中TreeSet類
import java.util.*;
public class TreeSetTest {
//如果自定義類物件要加入TreeSet要實現Comparable介面
public static void main(String []args)
{
TreeSet ts=new TreeSet(new Student.StudentComparator());
/* ts.add("winsun");
ts.add("weixin");
ts.add("mybole");
*/
ts.add(new Student(2,"lisi"));
ts.add(new Student(1,"wangwu"));
ts.add(new Student(3,"zhangsan"));
ts.add(new Student(3,"mybole"));
Iterator it=ts.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
}
class Student implements Comparable
{
int num;
String name;
static class StudentComparator implements Comparator
{
public int compare(Object o1,Object o2)
{
Student s1=(Student)o1;
Student s2=(Student)o2;
int result=s1.num>s2.num ? 1 : (s1.num==s2.num ? 0 : -1);
if(result==0)
{ //String類實現了compareTo()方法.
result=s1.name.compareTo(s2.name);
}
return result;
}
}
public static void printElements(Collection c)
{
Iterator it=c.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
Student(int num,String name)
{
this.name=name;
this.num=num;
}
public int HashCode()
{
return num*name.hashCode();
}
public boolean equals(Object o)
{
Student s=(Student)o;
return num==s.num && name.equals(s.name);
}
public int compareTo(Object o)
{
Student s=(Student)o;
return num>s.num?1:(num==s.num?0:-1);
}
public String toString()
{
return num+":"+name;
}
}
**
HashSet是基於Hash演算法實現的,其效能通常優於TreeSet.
通常都應該使用HashSet,在需要排序的功能時,才使用
TreeSet.
**
TreeSet是依靠TreeMap來實現的.
TreeSet是一個有序集合,TreeSet中元素將按照升序排列,
預設是按照自然排序進行排列,意味著TreeSet中元素要
實現Comparable介面.
我們可以在構造TreeSet物件時,傳遞實現了Comparator介面
的比較器物件.
java.util包當中TreeSet類
import java.util.*;
public class TreeSetTest {
//如果自定義類物件要加入TreeSet要實現Comparable介面
public static void main(String []args)
{
TreeSet ts=new TreeSet(new Student.StudentComparator());
/* ts.add("winsun");
ts.add("weixin");
ts.add("mybole");
*/
ts.add(new Student(2,"lisi"));
ts.add(new Student(1,"wangwu"));
ts.add(new Student(3,"zhangsan"));
ts.add(new Student(3,"mybole"));
Iterator it=ts.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
}
class Student implements Comparable
{
int num;
String name;
static class StudentComparator implements Comparator
{
public int compare(Object o1,Object o2)
{
Student s1=(Student)o1;
Student s2=(Student)o2;
int result=s1.num>s2.num ? 1 : (s1.num==s2.num ? 0 : -1);
if(result==0)
{ //String類實現了compareTo()方法.
result=s1.name.compareTo(s2.name);
}
return result;
}
}
public static void printElements(Collection c)
{
Iterator it=c.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
Student(int num,String name)
{
this.name=name;
this.num=num;
}
public int HashCode()
{
return num*name.hashCode();
}
public boolean equals(Object o)
{
Student s=(Student)o;
return num==s.num && name.equals(s.name);
}
public int compareTo(Object o)
{
Student s=(Student)o;
return num>s.num?1:(num==s.num?0:-1);
}
public String toString()
{
return num+":"+name;
}
}
**
HashSet是基於Hash演算法實現的,其效能通常優於TreeSet.
通常都應該使用HashSet,在需要排序的功能時,才使用
TreeSet.
**