java se基础复习3

 

第七章  容器
容器的概念:JAVA API 所提供的一系列类的实例,用于在程序中存放对象.
容器API
Collection接口
Iterator接口
增强的for循环
Set接口
List接口和Comparable接口
Collections类
Map接口
自动打包/解包
泛型(JDK1.5新增)
J2SDK所提供的容器API都在java.util包内
Collection
Set( 没顺序, 不可以重复) list( 有顺序, 可以重复)
HashSet LinkedList ArrayList(底层是数组)实现接口的类
 
Map( 成对装)
HashMap
Collection接口定义了存取一组对象的方法,其子接口Set和List分别定义了存储方式
 Set中的数据对象没有顺序且不可以重复
 List中的数据对象有顺序且可以重复(重复—两个之间互相equals)
Map接口定义了存储”键(key)----值(value)映射对”的方法
Collection接口
Collection接口中所定义的方法:
Int size() //元素个数
Boolean isEmpty()是否为空
Void clear()//清空
Boolean contains(Object element)//是否包含那个对象(equals就包含)
Boolean add(Object element)添加
Boolean remove(Object element)去除
Iterator iterator();
Boolean containsAll(Collection c)是否包含另一个集合里的所有元素
Boolean addAll(Collection c)
Boolean removeAll(Collection c)
Boolean retainAll(Collection c)求交集
Object[] toArray();把对象转换成对象类型的数组
Eg 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);
}
}
输出结果:3
         [hello,f1 11,100]
容器类对象在调用remove,contains等方法时需要比较对象是否相等,这会涉及到对象类型的equals方法和hashCode方法;对于自定义的类型,需要重写equals和hashCode(当对象用在map接口作为键)方法以实现自定义的对象相等规则.
注意:相等的对象应该具有相等hash codes
重写equals 方法必须重写hashCode 方法
Eg 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();
}
Iterator 接口
所有实现了collection接口的容器类都有一个iterator方法用以返回一个实现了Iterator接口的对象
Iterator对象称作迭代器,用以方便的实现对容器内元素的遍历操作
Iterator接口定义了如下方法:
Boolean HasNext()//判断游标右边是否有元素
Objet next() //返回游标右边的元素并将游标移动到下一个位置
Void remove()//删除游标左面的元素,在执行完next之后,该操作只能执行一次
Eg import java.util.*;
   Public class test{
 Public static void main(String[] args){
 Collection c = new HashSet();
 c.add(new Name(“f1”,”l1’));
 c.add(new Name(“f2”,”l2”));
 c.add(new Name(“f3”,”l3”));
Iterator I = c.iterator();
While(i.hasNext()){
 //next()的返回值为object类型,需要转换为相应类型
 Name n =(Name)i.next();
 System.out.println(n.gerFirstName()+””);
}
}
}
输出结果:f2,f1,f3(顺序不一定,因为hashSet没顺序)
Iterator方法举例
Iterator对象的remove方法是在迭代过程中删除元素的唯一的安全方法
Eg Collection c = new HashSet();
   c.add(new Name(“fff1”,”l111”));
   c.add(new Name(“f2”,”l2”));
c.add(“new Name(“fff3”,”l113”));
for(Iterator i=c.iteator();i.hasNext();){
 Name name = (Name)i.next();
 If(name.getFirstName().length()<3){
 i.remove();
 //如果换成c.remove(name);会产生例外
}
}
System.out.println(c);
输出结果:[fff3 l113,fff1 l111]
JDK1.5增强的for循环
增强的for循环对于遍历array或Collection的时候相当简便
缺陷:数组 不能方便的访问下标值
     集合 与使用Iterator相比,不能方便的删除集合中的内容
          在内部也是调用Iterator
总结:除了简单遍历并读出其中的内容外,不建议使用增强的for循环
Eg import java.util.*;
   Public class EnhancedFor{
 Public static void main(String[] args){
 Int[] arr={1,2,3,4,5};
 For(int i:arr){
System.out.println(i);
}
Collection c=new ArrayList();
c.add(new String(“aaa”));
c.add(new String(“bbb”));
c.add(new String(“ccc”));
for(Object o:c){
 System.out.println(o);
}
}
}
Set 接口
Set接口是Collection的子接口,Set接口没有提供额外的方法,但实现Set接口的容器类中的元素是没有有顺序的,而且不可以重复.
Set容器可以与数学中的集合的概念相对应
J2SDK API所提供的Set容器类有HashSet,TreeSet.
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和List容器类都具有Constructor(Collection c)
//构造方法用以初始化容器类
 
}
List 接口
List接口是collection的子接口,实现list接口的容器类中的元素是有顺序的,而且可以重复.
List容器中的元素都对应一个整数型的序号记载其在容器中的位置,可以根据序号存取容器中的元素.
J2SDK所提供的List容器类有ArrayList,LinkList等.
Object get(int index)
Object set(int index,object element)
Void add(int index,object element)
Object remove(int index)
Int indexOf(Object o)
Int lastIndexOf(object o)
List 接口
List l1 = new LinkedList();
For(int i=0;i<=5;i++){
 L1.add(“a”+i);
}
System.out.println(l1);
L1.add(3,”a100”);
System.out.println(l1);
L1.set(6,”a200”);
System.out.println(l1);
System.out.print((String)l1.get(2))+” ”;
System.out.println(l1.indexOf(“a3”));
L1.remove(1);
System.out.println(l1);
输出结果:[a0,a1,a2,a3,a4,a5]
         [a0,a1,a2,a100,a3,a4,a5]
         [a0,a1,a2,a100,a3,a4,a200]
         a2 4
         [a0,a2,a100,a3,a4,a200]
List常用算法
类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 List容器内容拷贝到dest List容器
Int binarySearch(List,object) 对于顺序的List容器,采用折半查找的方法查找特定对象
Eg 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.pintlnln(Collections.binarySearch(l1,”a5”));//折半查找
输出结果:[a0,a1,a2,a3,a4,a5,a6,a7,a8,a9]
         [随机]
         [上面结果逆序]
         [由低到高,与第一个结果相同]
Comparable接口
上面的算法根据什么确定容器中对象的大小顺序?
所有可以排序的类都实现了java.lang.Comparable接口,Comparable接口中只有一个方法
Public int compareTo(Object obj);该方法:
 返回0表示this==obj
 返回正数表示this>obj
 返回负数表示this<obj
实现了Comparable接口的类通过实现comparaTo方法从而确定该类对象的排序方式
改写Name类让其实现Comparable接口,其comparaTo方法定义为:
Class Name implements Comparable{
 Public int compareTo(Object o){
 Name n = (Name)o;
int lastCmp=lastName.compareTo(n.lastName);
return
 (lastCmp!=0?lastCmp:firstName.compareTo(n.firstName));
}
}
如何选择数据结构
衡量标准:读的效率和改的效率
Array读快改慢
Linked改快读慢
Hash两者之间
Map 接口
实现Map接口的类用来存储键值对
Map接口的实现类有HashMap和TreeMap等
Map类中存储的键值对通过键来标识,所以键值不能重复
Object put(object key,object value) //返回被替换掉的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()
Eg:import java.util.*;
   Public class Test{
 Public static void main(String args[]){
 Map m1=new HashMap();
 Map m2=new TreeMap();
 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);
M3.putAll(m2);
System.out.println(m3);
}
}
Auto-boxing/unboxing
在合适的时候自动打包,解包 jdk1.5/jdk5.0后可以
 自动将基础类型转换为对象 打包
 自动将对象转换为基础类型 解包
Eg 记录后面的单词出现的次数
Import java.util.*;
Public class TestArgsWords{
 //Private static final Integer ONE=new Integer(1);
Private static final int ONE=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]);
Int freq=(Integer)m.get(args[i])==null?0(integer)m.get(args[i]);
                  //m.put(args[i],(freq==null?ONE:new Integer(freq.intValut()+1)));
 m.put(args[i],freq==0?ONE:freq+1);
}
System.out.println(m.size()+”dsistinct words detected:”);
System.out.println(m);
}
}
JDK1.5 泛型
起因:jdk1.4以前类型不明确:
     装入集合的类型都被当做object对待,从而失去自己的实际类型
     从集合中取出时往往需要转型,效率低,容易产生错误
解决办法:在定义集合的的时候同时定义集合中对象的类型
好处:增强程序的可读性和稳定性
Eg
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++){
 String s=c.get(i);
System.out.println(s);
}
Collection<String> c2=new HashSet<String>();
C2.add(“aaa”);
C2.add(“bbb”);
C2.add(“ccc”);
for(Iterator<String> it=c2.iterator();it.hasNext();)//314338784
{
 String s=it.next();
 System.out.println(s);
}
}
}
Class MyName implements comparable<MyName>{
 Int age;
 Public int compareTo(MyName mn){
 If(this.age>mn.age) return 1;
 Else if(this.age<mn.age) return -1;
 Else return 0;
}
}
Import java.util.*;
Public class TestMap2{
 Public static void main(String args[]){
 Map<String,Integer> m1=new HashMap<String,Integer>();
 M1.put(“one”,1);
 M1.put(“two”,2);
 M1.put(“three”,3);
System.out.println(m1.size());
System.out.println(m1.containsKey(“one”));
If(m1.containsKey(“two”));
{
Int i=m1.get(“two”)
Systetm.out.println(i);
}
}
}
Import java.util.*;
Public class TestArgsWords2{
 Private static final int ONE=1;
 Public static void main(String args[]){
 Map<String,Integer> m=new HashMap<String,Integer>();
 For(int i=0;i<args.length;i++){
 If(!m.containsKey(args[i])){
 m.put(args[i],ONE);
}
Else{
 Int freq=m.get(args[i]);
 m.put(args[i],freq+1);
}
System.out.println(m.size()+”distinct words detected”);
System.out.println(m);
}
}
}
总结 1136
 一个图
 一个类
 Collections
 三个知识点
   For
   Generic
   Auto-boxing/unboxing
六个接口

你可能感兴趣的:(集合,容器)