Java集合底层结构及常用API

原创
2017/11/18 13:28
阅读数 356

常用集合接口表

 

各集合底层实现原理

http://m.blog.csdn.net/qq_25868207/article/details/55259978

疑问测试:

1.添加完集合元素后,必须获取其中的元素再取相应的属性值。直接返回取出的元素只是一个地址。

Course cr1=new Course(1,"数据结构");
courseToSelect.add(cr1);
System.out.println((Course)courseToSelect.get(0));
Course temp=(Course)courseToSelect.get(0);
System.out.println(temp.getId()+" "+temp.getCourseName());
Course cr2=new Course(2,"离散数学");
courseToSelect.add(cr2);
Course temp1=(Course)courseToSelect.get(1);
System.out.println(temp1.getId()+" "+temp1.getCourseName());

/*
collection.list.student_course.Course@30f39991

1 数据结构
2 离散数学
*/

2.两种add方法中验证:集合有序,确制定位置会把之前添加的到后面;

Course cr1=new Course(1,"数据结构");
courseToSelect.add(cr1);
Course cr2=new Course(2,"离散数学");
courseToSelect.add(0,cr2);
System.out.println((Course)courseToSelect.get(0));
Course temp=(Course)courseToSelect.get(0);
System.out.println(temp.getId()+" "+temp.getCourseName());
Course temp1=(Course)courseToSelect.get(1);
System.out.println(temp1.getId()+" "+temp1.getCourseName());

/*集合有序,确实制定位置会把之前添加的顺序挤到后面;

2 离散数学
1 数据结构
*/

集合概要:

基础接口collection的增

java.lang.collection

 boolean

add(E e) 
          
确保此 collection 包含指定的元素(可操作)。

 boolean

addAll(Collection<? extends E> c) 
          
将指定 collection 中的所有元素都添加到此 collection 中(可操作)。

 void

clear() 
          
移除此 collection 中的所有元素(可操作)。

 boolean

contains(Object o) 
          
如果此 collection 包含指定的元素,返回 true

 boolean

containsAll(Collection<?> c) 
          
如果此 collection 包含指定 collection 中的所有元素,返回 true

 boolean

equals(Object o) 
          
 collection 与指定象是否相等。

 int

hashCode() 
          
返回此 collection 的哈希码值

 boolean

isEmpty() 
          
如果此 collection 不包含元素,返回 true

 Iterator<E>

iterator() 
          
返回在此 collection 的元素上行迭代的迭代器。

 boolean

remove(Object o) 
          
从此 collection 中移除指定元素的例,如果存在的(可操作)。

 boolean

removeAll(Collection<?> c) 
          
移除此 collection 中那些也包含在指定 collection 中的所有元素(可操作)。

 boolean

retainAll(Collection<?> c) 
          
仅保留此 collection 中那些也包含在指定 collection 的元素(可操作)。

 Object[]

toArray() 
          
返回包含此 collection 中所有元素的数

 默认的集合操作中,put元素后,会按照object处理,获取元素的时候也是默认为object,所以如果没有范型留意类型强转。

初始化数组的巧妙写法

 Course[] courses={new Course(5,"Java"),new Course(6,"c"),new Course(7,"C++")};
   for (Course co:courses
       ) {
      System.out.println("id  "+co.getId()+"   name  "+co.getCourseName());
  }


/**初始化数组的巧妙写法
 * id  5   name  Java
 * id  6   name  c
 * id  7   name  C++
 */

细节详解:

ArrayList:

包括 null 在内的所有元素。

为提高这类迭代器的正确性而编写一个依赖于此异常的程序是错误的做法迭代器的快速失为应该仅用于检测 bug

构造方法

构造方法摘要

ArrayList() 
          构造一个初始容量 10 的空列表。

 

ArrayList(Collection<? extends E> c) 
          构造一个包含指定 collection 的元素的列表,些元素是按照 collection 的迭代器返回它序排列的。

 

ArrayList(int initialCapacity) 
          构造一个具有指定初始容量的空列表。

 

方法摘要:

方法摘要

 boolean

addAll(Collection<? extends E> c) 
          按照指定 collection 的迭代器所返回的元素序,将 collection 中的所有元素添加到此列表的尾部。

 boolean

addAll(int index, Collection<? extends E> c) 
          从指定的位置开始,将指定 collection 中的所有元素插入到此列表中。

 Object

clone() 
          返回此 ArrayList 实例的浅表副本

 boolean

contains(Object o) 
          如果此列表中包含指定的元素,返回 true

 void

ensureCapacity(int minCapacity) 
          如有必要,增加此 ArrayList 实例的容量,以确保它至少能够容纳最小容量参数所指定的元素数

 E

get(int index) 
          返回此列表中指定位置上的元素。

 int

indexOf(Object o) 
          返回此列表中首次出的指定元素的索引,或如果此列表不包含元素,返回 -1

 boolean

isEmpty() 
          如果此列表中没有元素,返回 true

 int

lastIndexOf(Object o) 
          返回此列表中最后一次出的指定元素的索引,或如果此列表不包含索引,返回 -1

 E

remove(int index) 
          移除此列表中指定位置上的元素。

 E

set(int index, E element) 
          用指定的元素替代此列表中指定位置上的元素。

迭代器:

java.util.iteraor

iterator:只是接口,依赖于集合存在,旨在遍历的时候作用,没有存储功能。

 boolean

hasNext() 
          如果仍有元素可以迭代,返回 true

 E

next() 
          返回迭代的下一个元素。

 void

remove() 
          从迭代器指向的 collection 中移除迭代器返回的最后一个元素(可操作)。

范型:

不指定范型,集合中的元素原理上,可以是任意类型的对象或者对象的引用。,而范型免去了类型转换的麻烦:

范型针对指定类和制定类的字类型都可以,范型中基本类型要用其封装类。

如果是父类中没有没有隐式构造器,子类中必须手动添加上。

Set:

循环遍历只能用forEach或者itertor,因为set无序,所以不能像List一样用Get()方法。

Map:

操作的是映射关系。  

展开阅读全文
加载中

作者的其它热门文章

打赏
0
0 收藏
分享
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部