容器
1.基本方法
remove( )或cotains( )时将参数与Collection中的各个元素进行equals( )比较,基本数据包装类已重写了equals()方法,而类需要对equals( )或hashCode( )手动重写
两个类相等,则两个类的hashcodes相等。重写equals(),就必须重写hashCode( ),在类做索引的情况下调用hashCode( )。(根据hashcode,可以找到类的物理地址,但是hashcode不完全是物理地址)
import java.util.*;
public class BasicCollection {
public static void main(String args[]) {
Collection c = new HashSet();
c.add("hello");
c.add(new Integer(100));
c.add(new Name("fl","ll"));
System.out.println(c);//[hello,100,"fl,ll"]
System.out.println(c.remove("hello"));//true
System.out.println(c.remove(new Integer(100)));//true
System.out.println(c.remove(new Name("fl","ll")));//false
}
}
class Name {
String first,lastname;
Name(String first,String lastname) {
this.first = first;
this.lastname = lastname;
}
public String toString () {
return first + lastname;
}
}
升级:
在Name类中加
public boolean equals(Object obj) {
if(obj instanceof Name) {
Name n = (Name) obj;
return n.first.equals(first) && n.lastname.equals(lastname);
}
return super.equals(obj);//Object中判定equals,相当于==,当指向同一对象时,为true,否则为false
}
public int hashCode() {
return first.hashCode();//有相同hashCode的类,其成员也有相同的hashCode,又String重写了hashCode方法,可直接调用
}
2.基本概念
Collection
Set:hashSet:不可重复,没有顺序
List:LinkedList、ArrayList:可重复,有顺序
Map:HashMap:键值对
3.Iterator
不同的容器有不同的iterator,但实现的方法是相同的。默认iterator存在于元素的左边,删除时删除左边,next时进右,且一次next,只能删除一次
import java.util.*;
public class TestIterator {
public static void main(String args[]) {
Collection c = new HashSet();
c.add(new Name("fff1","l1"));
c.add(new Name("f2","l2"));
c.add(new Name("fff3","l3"));
for(Iterator i = c.iterator(); i.hasNext();) {
Name n = (Name)i.next();
if(n.firstname.length() < 3) {
i.remove();
//利用c.remove(name);会抛出异常因为在使用遍历器时,锁定Collection。只有iterator可处理元素,其他都不可对元素进行处理
}
}
System.out.print(c);
}
}
class Name {
String firstname,nextname;
Name(String firstname,String nextname) {
this.firstname = firstname;
this.nextname = nextname;
}
public boolean equals(Object obj) {
if(obj instanceof Name) {
Name n = (Name) obj;
return n.firstname.equals(firstname) && n.nextname.equals(nextname);
}
return super.equals(obj);
}
public int hashCode() {
return firstname.hashCode();
}
public String toString() {
return firstname + "-" + nextname;
}
}
4.EnhancedFor(JDK1.5增强的for循环)
import java.util.*;
public class TestEnhancedFor {
public static void main(String args[]) {
int[] arr = {1,2,3};
for(int i : arr) {
System.out.println(i);
}
Collection c = new ArrayList();
c.add("aaa");
c.add("bbb");
c.add("ccc");
for(Object o : c) {
System.out.println(o);
}
}
}
优点:语法简便;
缺点:1.对于数组:不能访问确定的数组元素
2.对于集合:内部为Iterator遍历,但不能取得Iterator进行删除,所以只能看不能删
所以,除了简单遍历,不建议使用增强的for循环
5.Set(包括HashSet和TreeSet)无序,不可重复
import java.util.*;
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("a"); s2.add("d"); s2.add("e");
Set sn = new HashSet(s1);
Set su = new HashSet(s2);
sn.retainAll(s2);//求sn与s2的交集
su.addAll(s1);//求su与s1的并集
System.out.println(sn);
System.out.println(su);
}
}
6.List有序,可重复
有相对应的整数编号,可利用编号取或删元素,类似于数组,但不同于数组,容量大小可变
import java.util.*;
public class TestList {
public static void main(String args[]) {
List l = new LinkedList();
for(int i=0; i<5; i++) {
l.add("a"+i);
}
System.out.println(l);
l.add(3,"a100");
System.out.println(l);
l.set(4,"a99");
System.out.println(l);
System.out.println((String)l.get(5)+"");
System.out.println(l.indexOf("a4"));
l.remove("a4");
System.out.println(l);
}
7.java.util.Collections为一个类,里面包含基于List的一系列的静态方法
import java.util.*;
public class TestList2 {
public static void main(String args[]) {
List l = new LinkedList();
for(int i=0; i<6; i++) {
l.add(i,"a"+i);
}
System.out.println(l);
Collections.shuffle(l);//随机排序
System.out.println(l);
Collections.sort(l);//按增值排序
System.out.println(l);
Collections.reverse(l);//求逆排序
System.out.println(l);
System.out.println(Collections.binarySearch(l,"a1"));//二分法查找
}
}
}
详解:如何利用Sort( )将元素进行比较?元素实现了Comparable接口,比较时只用到里面的一种方法,int compareTo( T obj)
在Name类中实现Comparable,加入方法
public int compareTo (Object obj) {
Name n = (Name)obj;
int lastCom = n.lastname.compareTo(lastname);
return lastCom != 0 ? lastCom : n.first.compareTo(first);
}
则在利用Sort( )进行比较时,调用设定的compareTo()进行比较,进而可以拍出顺序。
8.Map接口
有HashMap和TreeMap,储存的键不能重复,其中重复也是根据equals()进行并比较的。因该方法执行的效率较慢,所以直接用HashCode( )方法比较hash表是否相同。所以重写equals( )方法的,必重写了hashCode( )方法
基本方法:
在Object put(Object key,Object value)方法中,如果添加的key已存在,则经value替换原来的value,且返回原来的value
import java.util.*;
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));
System.out.println(m1.size());
System.out.println(m1.containsKey("one"));
System.out.println(m2.containsValue(new Integer(1)));
if(m1.containsKey("two")) {
int i = ((Integer)m1.get("two")).intValue();
System.out.println(i);
}
Map m3 = new HashMap(m1);
m1.putAll(m2);
System.out.println(m3);
}
}
不难发现,在Map中操作的都是类,而在jdk1.5之后,Map可以自动装包\解包(auto-boxing\unboxing),所以可以使用put("one",1);int i = (Integer)map.get("one");如此之类的语句。即自动将基础类型变量打包成包装类。
注意:在解包的时候,要声明将Object转换为Integer,以便于解包
9.泛型
在jdk1.4以前不明确,需要将每个元素作为Object,然后再进行转型,效率低,易出错。所以使用泛型,在定义Collection或使用Iterator时指定。从而增强程序的可读性和稳定性。
import java.util.*;
public class BasicGeneric {
public static void main(String args[]) {
List<String> c = new ArrayList<String> ();
c.add("aaa");
c.add("bbb");
c.add("ccc");
for(int i=0; i<c.size(); i++) {
System.out.println(c.get(i));
}
Collection<String> c2 = new HashSet<String> ();
c2.add("aaa"); c2.add("bbb"); c2.add("ccc");
for(Iterator<String> i=c2.iterator(); i.hasNext();) {
System.out.println(i.next());
}
}
}
class MyName implements Comparable<MyName> {
int age;
public int compareTo(MyName mn) {
if(age > mn.age) return 1;
else if(age < mn.age) return -1;
else return 0;
}
}