1 ImportJava.util.*;2 3 Public classBasiccontainer {4 Public Static voidMain (string[] args) {5Collection C =NewHashSet ();6C.add ("Hello");7C.add (NewName ("F1", "L1"));8C.add (NewInteger (100));9C.remove ("Hello"); TenC.remove (NewInteger (100)); One System.out.println A(C.remove (NewName ("F1", "L1"))); - System.out.println (c); - } the - - } - + classNameImplementsComparable { - PrivateString Firstname,lastname; + PublicName (String firstName, String lastName) { A This. firstName = FirstName; This. LastName =LastName; at } - PublicString Getfirstname () {returnFirstName; } - PublicString Getlastname () {returnLastName; } - PublicString toString () {returnFirstName + "" +LastName; } - - Public Booleanequals (Object obj) { in if(objinstanceofName) { -Name name =(Name) obj; to return(Firstname.equals (name.firstname)) +&&(Lastname.equals (Name.lastname)); - } the return Super. Equals (obj); * } $ Public inthashcode () {Panax Notoginseng returnFirstname.hashcode (); - } the + A the Public intcompareTo (Object o) { +Name n =(Name) o; - intLASTCMP = $ Lastname.compareto (n.lastname); $ return -(lastcmp!=0?)lastcmp: - Firstname.compareto (N.firstname)); the } - Wuyi}
Java_ Container Api_collection