package com.wlh.collection; import java.util.ArrayList; import java.util.Collection; import java.util.List; public class TestList { public static void main(String[] args) { Collection c = new ArrayList(); //可以放入不同类型的对象 c.add("hello"); c.add(new Name("f1", "l1")); c.add(new Name("f1", "l1")); c.add(11);//整型11将自动打包为对象new Integer(11); c.add(22);//整型12将自动打包为对象new Integer(11); //返回包含此 collection 中所有元素的数组 Object [] obj=c.toArray(); for(int i=0;i<obj.length;i++){ System.out.println("obj"+i+":"+obj[i]); } Collection cc = new ArrayList(); cc.add("hello"); cc.add(11); cc.add("hello world"); //contains方法:如果此 collection 包含指定的元素,则返回 true System.out.println(c.contains("hello"));//true //containsAll: 如果此collection 包含指定 collection 中的所有元素,则返回 true。 System.out.println(c.containsAll(cc));//false //addAll: 将指定 collection 中的所有元素都添加到此 collection 中 System.out.println(c.addAll(cc)); //retainAll:交集,仅保留此 collection 中那些也包含在指定 collection 的元素(可选操作)。 //System.out.println(c.retainAll(cc)); //removeAll: 移除此 collection 中那些也包含在指定 collection 中的所有元素(可选操作)。 System.out.println(c.removeAll(cc)); //ArrayList为什么有两个remove方法? //remove(int index) 是子类新添加的! //如果是父类引用指向子类对象,则找不到子类中新添加的方法。 //要想调用子类新添加的方法,需要先转型 //是优先自动打包呢?还是先去匹配方法? c.remove("hello1"); System.out.print(c); //要想调用子接口中新添加的方法,需要先转型 List a = (List) c; a.remove(1); System.out.println(c.size()); System.out.println(c); //clear:移除此 collection 中的所有元素(父接口Collection中的方法clear) c.clear(); System.out.println(c); } } class Name { private String firstName, lastName; public Name(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } //重写了tostring方法 public String toString() { return firstName + "" + lastName; } } package com.wlh.collection; //为什么调用两次c.add("hello");只加入一个hello? //可见HashSet中是采用了比较元素hashCode的方法来判断元素是否相同(duplicate), //而不是采用其他类似equals之类的东东来判断。 //一个对象被当作Map里面的key的时候,hashCode用来比较两个东东是不是相等 //hashCode非常适合用来做索引 //重写equals方法,通常需要重写hashCode方法.因为你的本意是想让他相等的.但hashCode如果不重写,不同的对象就不会相等 import java.util.*; public class BasicContainer { public static void main( String[] args) { Collection c = new HashSet(); c.add("hello"); c.add("hello"); c.add(new Name3("f1","l1")); c.add(new Name3("f1","l1")); System.out.println(new String("f1").hashCode()==new String("f1").hashCode()); c.add(new Integer(100)); c.remove("hello"); c.remove(new Integer(100)); System.out.println(c.remove(new Name3("f1","l1"))); //System.out.println(c); System.out.println(new Name3("f1","l1").hashCode()); System.out.println(new Name3("f1","l1").hashCode()); System.out.println(c); List list = new ArrayList(); list.add(new Name3("f2", "l2")); list.add(new Name3("f1", "l1")); list.add(new Name3("f5", "l5")); list.add(new Name3("f3", "l3")); Collections.sort(list); System.out.println(list); } } class Name3 implements Comparable { public String firstName,lastName; public Name3(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } @Override public String toString() { return firstName + " " + lastName; } @Override public boolean equals(Object obj) { if (obj instanceof Name) { Name3 name = (Name3) obj; return (firstName.equals(name.firstName)) && (lastName.equals(name.lastName)); } return super.equals(obj); } @Override public int hashCode() { System.out.println(firstName.hashCode()); return firstName.hashCode();//这个对象当作为索引的时候会使用 } public int compareTo(Object o) { Name3 n = (Name3)o; int lastCmp = lastName.compareTo(n.lastName); return (lastCmp!=0 ? lastCmp : firstName.compareTo(n.firstName)); } } package com.wlh.collection; import java.util.Collection; import java.util.HashSet; public class TestSet { /** * Set通过验证hashcode来判断2元素是否相等,先调用hashcode方法然后调用equals方法 */ public static void main(String[] args) { Collection c = new HashSet(); c.add("hello"); c.add("hello"); c.add(new Name4("f1","l1")); c.add(new Name4("f1","l1")); System.out.println(c); } } class Name4 //implements Comparable { public String firstName,lastName; public Name4(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } @Override public String toString() { return firstName + " " + lastName; } @Override public boolean equals(Object obj) { if (obj instanceof Name4) { Name4 name = (Name4) obj; System.out.println(firstName.equals(name.firstName)); return (firstName.equals(name.firstName)); } return super.equals(obj); } @Override public int hashCode() { System.out.println(firstName.hashCode()); return firstName.hashCode();//这个对象当作为索引的时候会使用 } /*public int compareTo(Object o) { Name4 n = (Name4)o; int lastCmp = lastName.compareTo(n.lastName); return (lastCmp!=0 ? lastCmp : firstName.compareTo(n.firstName)); }*/ } package com.wlh.collection; import java.util.HashSet; import java.util.Set; public class SetTest5 { public static void main(String[] args){ Set set = new HashSet(); set.add(new SetElement5("aa")); set.add(new SetElement5("aa")); set.add(new SetElement5("bb")); System.out.println(set); } static class SetElement5{ String s; public SetElement5(String s){ this.s = s; } public String toString(){ return s; } public boolean equals(Object obj) { System.out.println(s.equals(((SetElement5)obj).s)); return s.equals(((SetElement5)obj).s); } public int hashCode() { System.out.println(s.hashCode()); return s.hashCode(); } } } package com.wlh.collection; import java.util.Set; import java.util.*; /** * TreeSet中是采用Comparable接口中的compareTo方法来判断元素是否相同(duplicate), * 而不是采用其他类似equals之类的东东来判断。 * @author Administrator * */ public class TreeSetTest { public static void main(String[] args){ Set set = new TreeSet(); set.add(new SetElement2("aa")); set.add(new SetElement2("aa")); set.add(new SetElement2("bb")); System.out.println(set); } static class SetElement2 implements Comparable{ String s; public SetElement2(String s){ this.s = s; } public String toString(){ return s; } public int compareTo(Object o){ return s.compareTo(((SetElement2)o).s); } public boolean equals(Object obj) { return s.equals(((SetElement2)obj).s); } } } package com.wlh.collection; import java.util.*; class TestMap2 { public static void main(String args[]) { Map m1 = new HashMap(); m1.put("one",new Integer(1)); m1.put("one",new Integer(7)); m1.put("two",new Integer(2)); m1.put("three",new Integer(3)); m1.put(new Name2("f1","l1"),"aaa"); m1.put(new Name2("f1","l1"),"bbb"); m1.put(new Name2("f1","l1"),"bbb"); //m1.remove(new Name("f1","l1")); //System.out.println(m1); //System.out.println(m1.size()); //System.out.println(m1.containsKey("one")); if(m1.containsKey("two")) { int i = ((Integer)m1.get("two")).intValue(); //System.out.println(i); } Map m2 = new TreeMap(); //m2.put("B","aaa"); m2.put(new Name2("f1","l1"),"aaa"); m2.put(new Name2("f1","l1"),"aaa"); System.out.println(m2); //System.out.println(m2.containsValue("aaa")); Map m3 = new HashMap(m1); m3.putAll(m2); System.out.println(m3); } } class Name2 implements Comparable { public String firstName,lastName; public Name2(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } @Override public String toString() { return firstName + " " + lastName; } @Override public boolean equals(Object obj) { System.out.println("equals....."); if (obj instanceof Name) { Name2 name = (Name2) obj; return (firstName.equals(name.firstName)) && (lastName.equals(name.lastName)); } return super.equals(obj); } @Override public int hashCode() { //System.out.println("==="+firstName.hashCode()); System.out.println("hashCode....."); return firstName.hashCode();//这个对象当作为索引的时候会使用 } public int compareTo(Object o) { System.out.println("compareTo....."); Name2 n = (Name2)o; int lastCmp = lastName.compareTo(n.lastName); return (lastCmp!=0 ? lastCmp : firstName.compareTo(n.firstName)); } }