Java集合框架是Java语言的重要组成部分,它包含了系统而完整的集合层次体系,封装了大量的数据结构的实现,深刻理解Java集合框架的组成结构及其中的实现类和算法,可以极大的提高程序员编码的能力。
import java.util.*; public class Test{ public static void main(String[] args){ Collection c=new ArrayList(); c.add("hello"); c.add(new Name("f1","11")); c.add(new Integer(100)); System.out.println(c.size()); System.out.println(c); } }
import java.util.*; public class BasicContainner{ public static void main(String[] args){ Collection c=new HashSet(); c.add("hello"); c.add(new Name("f1","11")); c.add(new Integer(100)); c.remove(new Integer(100)); System.out.println(c.remove(new Name("f1","11"))); 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;} public String toString(){return firstName + " " + 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(){ return firstName.hashCode(); } }
import java.util.*; public class MyHashMap{ public static void main(String[] args){ HashMap hm=new HashMap(); hm.put(1,'Z'); hm.put(2,'H'); hm.put(3,'O'); hm.put(4,'U'); System.out.print("添加元素后结果为:"); System.out.println(hm); hm.remove(3); hm.put(2,"替换"); System.out.print("删除和替换元素后的结果:"); System.out.println(hm); } }
原文地址:http://blog.csdn.net/zhou2s_101216/article/details/46516147