TreeSet类学习 java疯狂讲义

来源:互联网 发布:软考初级程序员考什么 编辑:程序博客网 时间:2024/04/28 11:32

import java.util.*;
public class TreeSetTest
{
public static void main(String[] args)
{
TreeSet nums = new TreeSet();
nums.add(5);
nums.add(2);
nums.add(10);
nums.add(4);
nums.add(-9);
System.out.println(nums);
System.out.println(nums.first());
System.out.println(nums.last());
System.out.println(nums.headSet(4));
System.out.println(nums.tailSet(5));
System.out.println(nums.subSet(-3,4));
}
}
=========================================================================
import java.util.*;
class Err
{
}
public class TreeSetErrorTest
{
public static void main(String[] args)
{
TreeSet ts = new TreeSet();
ts.add(new Err());
ts.add(new Err());

}
}
=============================================================================

import java.util.*;
class Err
{
}
public class TreeSetErrorTest
{
public static void main(String[] args)
{
TreeSet ts = new TreeSet();
ts.add(new Err());
ts.add(new Err());

}
}

=====================================================================
import java.util.*;
public class TreeSetErrorTest2
{
public static void main(String[] args)
{
TreeSet ts = new TreeSet();
ts.add(new String("Struts权威指南3"));
// ts.add(new Date());
ts.add(new String("Struts权威指南2"));
System.out.println(ts);
}
}


=======================================================================

import java.util.*;

import java.lang.*;


class Z implements Comparable
{
int age;
public Z(int age)
{
this.age = age;
}
public boolean equals(Object obj)
{
return true;
}
public int compareTo(Object obj)
{
return 1;
}
}


public class TreeSetTest2
{
public static void main(String[] args)
{
TreeSet set = new TreeSet();
Z z1 = new Z(6);
set.add(z1);
System.out.println(set.add(z1));
System.out.println(set);
((Z)(set.first())).age = 9;
System.out.println(((Z)(set.last())).age);
}
}

运行结果:

true
[Z@8c436b, Z@8c436b]
9


import java.util.*;
import java.lang.*;


class R implements Comparable
{
int count;
public R(int count)
{
this.count  = count;
}
public String toString()
{
return "R[count:" + count + "]";
}
public boolean equals(Object obj)
{
if(this == obj)
{
return true;
}
if(obj != null && obj.getClass() == R.class)
{
R r = (R)obj;
if(r.count == this.count)
{
return true;
}
}
return false;
}
public int compareTo(Object obj)
{
R r = (R)obj;
return count > r.count ? 1 : count < r.count ? -1 : 0;
}
}


public class TreeSetTest3
{
public static void main(String[] args)
{
TreeSet ts = new TreeSet() ;
ts.add(new R(5)) ;
ts.add(new R(-3)) ;
ts.add(new R(9)) ;
ts.add(new R(-2)) ;
System.out.println(ts);

R first = (R)ts.first();
first.count = 20 ;
R last = (R)ts.last() ;
last.count = -2;
System.out.println(ts);
System.out.println(ts.remove(new R(-2)));
System.out.println(ts);
System.out.println(ts.remove(new R(5)));
System.out.println(ts);

}
}

运行结果:

[R[count:-3], R[count:-2], R[count:5], R[count:9]]
[R[count:20], R[count:-2], R[count:5], R[count:-2]]
false
[R[count:20], R[count:-2], R[count:5], R[count:-2]]
true
[R[count:20], R[count:-2], R[count:-2]]



0 0
原创粉丝点击