看代码注释即可明白什么是重点。。。
package heng.java.Contain;
import java.util.Collection;
import java.util.HashSet;
/**
* 重写equals时,必须重写hashCode方法
* @author Administrator
*
*/
public class BasicContainer {
public static void main(String[] args) {
Collection c = new HashSet();
c.add("hello");
c.add(new Name("f1","L1"));
c.add(new Integer(100));
c.remove("hello");
c.remove(new Integer(100));//Integer已经重写了equals,so可以remove
System.out.println(c.remove(new Name("f1","L1")));//remove返回值是boolean。Name没有重写equals方法,so不能remove
System.out.println(c);
}
}
class Name {
private String firstName,lastName;
public Name(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public boolean equals(Object obj){
if(obj instanceof Name){
Name name = (Name) obj;
return (firstName.equals(name.firstName)) && (lastName.equals(name.lastName));
}
return super.equals(obj);//交给父类处理,相当于==
}
public int hashCode(){//非常适合索引,很适合map方法
return firstName.hashCode();
//当这个类的对象当做键值来做索引使用时,必须使用hashcode
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String toString() {
return firstName+" "+lastName;
}
}
Set 小代码。。。值得参考。。。
package heng.java.Contain;
import java.util.HashSet;
import java.util.Set;
public class TestSet {
public static void main(String[] args) {
Set s1 = new HashSet();
Set s2 = new HashSet();
s1.add("a");
s1.add("b");
s1.add("c");
s2.add("d");
s2.add("a");
s2.add("b");
Set sn = new HashSet(s1);
sn.retainAll(s2);//求交集
Set su = new HashSet(s1);
su.addAll(s2);//将s2添加到s1里面,其中已存在的不添加
System.out.println(sn);
System.out.println(su);
}
}
List中的几个排序。。。真心好用。。。
package heng.java.Contain;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
public class TestList {
public static void main(String[] args) {
List L1 = new LinkedList();
List L2 = new LinkedList();
for(int i=0; i<=9; i++){
L1.add("a"+i);
}
System.out.println(L1);
Collections.shuffle(L1);//随机排序
System.out.println(L1);
Collections.reverse(L1);//逆序排序
System.out.println(L1);
Collections.sort(L1);//默认从小到大排序
System.out.println(L1);
System.out.println(Collections.binarySearch(L1, "a5"));//二分法查找
}
}
/* 运行结果
[a0, a1, a2, a3, a4, a5, a6, a7, a8, a9]
[a1, a2, a7, a6, a5, a0, a9, a4, a3, a8]
[a8, a3, a4, a9, a0, a5, a6, a7, a2, a1]
[a0, a1, a2, a3, a4, a5, a6, a7, a8, a9]
5
*/
自动打包,自动解包。。。
package heng.java.Contain;
import java.util.HashMap;
import java.util.Map;
public class TestMap {
public static void main(String[] args) {
Map m1 = new HashMap();
Map m2 = new HashMap();
m1.put("one", new Integer(1));
m1.put("two", new Integer(2));
m1.put("three", new Integer(3));
m2.put("A", new Integer(1));
m2.put("B", new Integer(2));//可以m2.put("B", 2)因为jdk1.5之上新加自动解包成基本类型
System.out.println(m1.size());
System.out.println(m1.containsKey("one"));//返回boolean是不是包含这个键
System.out.println(m2.containsValue(new Integer(1)));//返回boolean是不是包含这个值
if(m1.containsKey("two")){
int i=(((Integer) m1.get("two")).intValue());//m1.get()返回的是Object,so先强转成Integer(对象类型)。再转成int(基本类型) 只能简化成:int i=(Integer)m1.get("two");
System.out.println(i);
}
Map m3 = new HashMap(m1);//把m1copy到m3里面
m3.putAll(m2);//把m2添加到m3里面
System.out.println(m3);
}
}
此代码读懂。。。有点罕见。。。
package heng.java.Contain1;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class TestMap {
public static void main(String[] args) {
/*在CMD上运行
Map m = new HashMap();
for(int i=0; i<args.length; i++){
Integer fre = (Integer)m.get(args[i]);
m.put(args[i],(fre==null) ? (new Integer(1)) : new Integer(fre.intValue()+1));
}
System.out.println((m.size()));
System.out.println(m);
*/
Scanner input = new Scanner(System.in);
while(input.hasNext()){
Map m = new HashMap();
int n = input.nextInt();
String [] str = new String [n];
int max = 0;
for(int i=0; i<n; i++){
str[i] = input.next();
}
for(int i=0; i<n; i++){
Integer fre = (Integer)m.get(str[i]);
m.put(str[i],(fre==null) ? (new Integer(1)) : new Integer(fre.intValue()+1));
int fre1 = (Integer)m.get(str[i]);
if(fre1 > max){
max = fre1;
}
}
//System.out.println((m.size()));
//System.out.println(m);
System.out.println(max);
}
}
}