Java容器的继承关系
Collection接口
Collection接口中所定义的方法
int size();
boolean isEmpty();
void clear();
boolean contains(Object element);//是否包含某个对象
boolean add(Object element);
Iterator iterator();
boolean containsAll(Collection c);//是否包含另一个集合中的所有元素
boolean addAll(Collection c);
boolean removeAll(Collection c);
boolean retainAll(Collection c);求当前集合类和另一个集合类的交集
Object[] toArray();
复制
Collection 方法举例
import java.util.*;
public class Test{public static void main(String[] args){Collection c = new ArrayList();//可以放入不同类型的对象c.add("hello");c.add(new Integer(100));System.out.println(c.size());//2System.out.println(c);//[hello,100]}
}
复制
这里提出一个我学习过程中问题:第四行代码,为什么不写成如下形式:
ArrayList c = new ArrayList();
//或者
List c = new ArrayList();
复制
后来看了马士兵老师的视频以及网上找了一些博客解释才大概有个了解 首先,利用基类(父类)引用指向子类对象,该引用只能访问子类中父类的属性和方法,不能访问子类中新的属性和方法,当然这里不是父类,而是接口,效果也是一样的。这样做相当于给使用者固定一个模板,只能使用Collection中的一些方法,而且本身程序也只用到Collection中的方法,更加简洁 其次,使用Collection接口的引用更加具有扩展性,比方说下次要将c指向LinkedList对象,就可以直接改,但如果是上述两种方式,就不方便进行转换
Set接口
Set接口中所定义的方法
boolean add(Object o)//如果不存在指定的元素,则添加此元素
boolean contains(Object o)//如果包含指定的元素,则返回true
boolean retainAll(Collection c)//保留包含collection中的元素
boolean addAll(Collection c)//将Collection中的元素添加到Set中(不包括重复的)
复制
Set方法举例
public static void main(String[] args){Set s = new HashSet();s.add("hello");s.add("world");s.add(new Inetger(100));s.add("hello");//相同的元素不会被添加System.out.println(s);//
}
复制
pulic 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);//把s1的内容copy到snSet su = new HashSet(s1);sn.retainAll(s2);//两个集合的交集su.addAll(s2);System.out.println(sn);System.out.println(su);
}
复制
List接口
List接口中所定义的方法
Object get(int index)//返回指定下标位置的元素
Object set(int index,Object element)//用指定的元素替换指定位置的元素,返回被替换的元素
void add(int index,Object element)//在指定位置添加元素,后面的元素依次向后移
Object remove(int index)//去除指定位置的元素
int indexOf(Object o)//返回元素第一次出现在List中的下标
int LastIndexOf(Object o)//返回元素最后一次出现在List中的下标
复制
List方法举例
public static void main(String[] args){List l1 = new LinkedList();for(int i = 0;i <= 5;i++)l1.add("a" + i);System.out.println(l1);//[a0,a1,a2,a3,a4,a5]l1.add(3,"a100");System.out.println(l1);//[a0,a1,a2,a100,a3,a4,a5]l1.set(6,"a200");System.out.println(l1);//[a0,a1,a2,a100,a3,a4,a200]System.out.print((String)l1.get(2) + " ");//a2System.out.println(l1.indexOf("a3"));//4l1.remove(1);System.out.println(l1);//[a0,a2,a100,a3,a4,a200]
复制
上面程序注意一点是,get方法返回值是Object类型,所以要强制转换为String类型
Collections类
类java.util.Collections提供了一些静态方法实现了基于List容器的一些常用算法
void sort(List)//对List容器内的元素排序
void shuffle(List)//对List容器内的元素随机排列
void reverse(List)//将List容器内的元素逆序
void fill(List,Object)//用一个特定的对象重新填充List
void copy(List dest,List src)//将src的内容拷贝到dest
int binarySearch(List,Object)//二分查找特定对象
复制
Collections方法举例
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"));//折半查找
}
复制
Comparable接口
Comparable中只有一个方法:
public int compareTo(Object obj);
//返回 0 表示this == obj
//返回正数表示this > obj
//返回负数表示this < obj
复制
实现Comparable接口的类通过实现compareTo方法,从而确定该类对象的排序方式
Comparable方法举例
import java.util.*;class Person implements Comparable{//必须实现接口String firstName,lastName;Person(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 o) {if(o instanceof Person) {Person p = (Person)o; return p.firstName == this.firstName && p.lastName == this.lastName;}returnsuper.equals(o);}public int hashCode() {return firstName.hashCode();}public int compareTo(Object o) {//实现compareTo方法Person p = (Person)o;int perCmp = this.firstName.compareTo(p.firstName);//调用String类的compareTo方法return (perCmp == 0 ? this.lastName.compareTo(p.lastName) : perCmp);//如果姓相同,比较名}
}public class Test{public static void main(String[] args) {List l1 = new LinkedList();l1.add(new Person("W","M"));l1.add(new Person("X","Z"));l1.add(new Person("Z","S"));l1.add(new Person("W","L"));System.out.println(l1);Collections.sort(l1);System.out.println(l1);}
}
复制
Map接口
- Map接口的实现类用来存储键&值对
- Map接口的实现类有HashMap和TreeMap等
- Map类中存储的键&值对通过键来标识,所以键值不能重复
Object put(Object Key,OBject Value)//如果原本的key存在,
//那么就会把原本的value作为返回值返回,并且把当前的参数传进去
Object get(Object Key)
Object remove(Object Key)
boolean containsKey(Object Key)
boolean containsValue(Object Value)
int size()
boolean isEmpty()
void putAll(Map t)
void clear()
复制
Map方法举例
import java.util.*;
public class Test{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.pritnln(i);}Map m3 = new HashMap(m1);m3.putAll(m2);System.out.println(m3);}
}
复制
import java.util.*;
//记录每个单词出现多少次
public class Test{private static final Integer ONE = new Integer(1);public static void main(String[] args) {Map m = new HashMap();for(int i = 0;i < args.length;i++) {Integer freq = (Integer)m.get(args[i]);m.put(args[i],(freq == null ? ONE : new Integer(freq.intValue()) + 1));}System.out.println(m.size() + "distinct words detected:");System.out.println(m);}
}
复制