关于集合类,equals,HashCode,Comparable,Iterator总结

    import java.math.*;
import java.util.*;
public class TestInteger {
    
    private static ArrayList al = new ArrayList();
    private static HashSet hs = new HashSet();
   
    public static void main( String[] args ) {
//        int i = Integer.valueOf( 200 );
//        BigInteger bi = new BigInteger(null, i);
//        BigInteger j = bi.add( new BigInteger(null , i) );
//        System.out.println( j );
//        testVector();
//        testArrayListSort();
        testArrayListRemove();
    }
   
    public static void testArrayListSort() {
//        al.add( new String("first datum") );
        al.add( new User(  13 , "Tom"));
        al.add( new User( 16 , "Lucy" ) );
        al.add( new User( 12 , "Gorige") );
        Collections.sort( al );
        System.out.println(al);
    }
   
    public static void testArrayListRemove() {
        al.add( new Company(  "Tom" ));
        al.add( new Company( "Lucy" ) );
        al.add( new Company( "Gorige" ) );
        //为Company重写equals方法,才能实现删除
//        al.remove( new Company( "Tom" ) );
        Iterator it = al.iterator();
        Company c;
        while ( it.hasNext() ) {
//            System.out.println( it.next() );
            c = (Company)it.next();
            if ( c.equals( new Company(  "Tom" )) ) {
                it.remove();
            }
        }

        System.out.println(al);
    }
   
    public static void testVector() {
        Vector v = new Vector();
        v.add( new String("sdfsdf") );
        VectorSon vs1 = new VectorSon();
        ArrayListSon al1 = new ArrayListSon();
        al1.add( "abcdefghijk" );
        vs1.add( "123123" );
        v.addAll( vs1 );
        v.addAll( al1 );
//        System.out.println(v);
        Iterator it = v.iterator();
       
        while ( it.hasNext() ) {
            System.out.println( it.next() );
        }
    }

}

class User implements Comparable {
    private int age;
    private String name;
   
    public User( int iAge , String sName ) {
        age = iAge; name = sName;
    }
   
    public String toString() {
        return "This is"+name+",age is"+age;
    }
   
    public boolean equals( Object o ) {
        if ( o instanceof User ) {
            User u = (User)o;
            if ( u.age == this.age && u.name.equals( this.name ) )
                return true;
        }
        return false;
    }
   
    public int compareTo( Object o ) {
        User u = (User)o;
        int i = this.age - u.age;
        return i !=0 ? i : this.name.compareTo( u.name );
    }
}

class Company {
    private String employee;
   
    public Company( String ep ) {
        employee = ep;
    }
   
    public String toString() {
        return employee;
    }
   
    public boolean equals( Object o ) {
        if ( o instanceof Company ) {
            Company c = (Company)o;
            if ( c.employee.equals( this.employee ) )
                return true;
        }
        return false;
    }
}

import java.util.*;

@SuppressWarnings("unchecked")
public class TestSort {
   
    private static HashSet hs = new HashSet();
    private static LinkedList ll = new LinkedList();
    private static HashMap hm = new HashMap();
   
    //Set由于存储无序,所以不能排序
    public static void testHashSetSort() {
        hs.add( new Point( 1,2 ) );
        hs.add( new Point( 4,5 ) );
        hs.add( "91010sdfsf" );
        System.out.println( hs );
    }
   
    //Map尽管他有键来查找,但由于存储无序,所以也不能排序
    public static void testHashMapSort() {
        hm.put( new Point( 1,2 ) , "abc" );
        hm.put( new Point( 4,5 ) , "cde" );
        System.out.println( hm );
    }
   
    //list
    public static void testLinkedListSort() {
        ll.add( new Point( 41,15 ) );
        ll.add( new Point( 21,12 ) );
        ll.add( new Point( 54,5 ) );
        ll.add( new Point( 10,12 ) );
        Collections.sort( ll );
       
        System.out.println( ll );
    }
   
    public static void main( String[] args ) {
//        testHashSetSort();
//        testHashMapSort();
        testLinkedListSort();
    }

}

class Point implements Comparable {
    private int x,y;
    public Point( int _x , int _y ) {
        x = _x; y = _y;
    }
    public String toString() {
        return "x="+x+",y="+y;
    }
    public int compareTo( Object o ) {
        Point p = (Point)o;
        int i = this.x - p.x;
        int j = this.y - p.y;
        return i != 0 ? i : j;
    }
}


1。只有实现了Collection的类才有iterator方法。所以Map没有
2。当使用无序的Set时候,如果只用默认的remove删除元素,必须要重写equals和hashCode方法。
3。使用有序的list时候,无需重写hashCode方法,但仍然要重写equals方法。
4。Map依靠key来查找value,根据key来remove时如果key为对象就必须要重写hashCode方法和equals方法。
5。Set和Map是不能排序的,因为它的存储是无序的,list是可以的,因为它是有序的,list如果元素是对象,要对其排序需要实现Comparable接口和其中的方法compareTo,如果要排序的话,list中元素类型必须一致,否则执行时异常,不排序不一致没关系。
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值