Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7553796
  • 博文数量: 961
  • 博客积分: 15795
  • 博客等级: 上将
  • 技术积分: 16612
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-07 14:23
文章分类

全部博文(961)

文章存档

2016年(1)

2015年(61)

2014年(41)

2013年(51)

2012年(235)

2011年(391)

2010年(181)

分类: Java

2012-04-24 21:46:19

点击(此处)折叠或打开

  1. import java.util.*;

  2. public class BasicContainer {
  3.     public static void main(String[] args) {
  4.         Collection c = new HashSet();
  5.         c.add("hello");
  6.         c.add(new Name("f1","l1"));
  7.         c.add(new Integer(100));
  8.         c.remove("hello");
  9.         c.remove(new Integer(100));
  10.         System.out.println
  11.                   (c.remove(new Name("f1","l1")));
  12.         System.out.println(c);
  13.     }


  14. }

  15. class Name implements Comparable {
  16.     private String firstName,lastName;
  17.     public Name(String firstName, String lastName) {
  18.         this.firstName = firstName; this.lastName = lastName;
  19.     }
  20.     public String getFirstName() { return firstName; }
  21.     public String getLastName() { return lastName; }
  22.     public String toString() { return firstName + " " + lastName; }
  23.     
  24.     public boolean equals(Object obj) {
  25.      if (obj instanceof Name) {
  26.      Name name = (Name) obj;
  27.      return (firstName.equals(name.firstName))
  28.      && (lastName.equals(name.lastName));
  29.      }
  30.      return super.equals(obj);
  31.     }
  32.         public int hashCode() {
  33.          return firstName.hashCode();
  34.         }
  35.         
  36.         
  37.         
  38.         public int compareTo(Object o) {
  39.         Name n = (Name)o;
  40.         int lastCmp =
  41.             lastName.compareTo(n.lastName);
  42.         return
  43.              (lastCmp!=0 ? lastCmp :
  44.               firstName.compareTo(n.firstName));
  45.     }
  46.         
  47. }
课件: collection.pdf   

阅读(1962) | 评论(1) | 转发(1) |
0

上一篇:QT T9中文输入法

下一篇:Java Iterator

给主人留下些什么吧!~~

1471893852012-04-25 16:16:03

我也刚在学java,呵呵,感觉比起C++来说JAVA比较好用啊