//list集合可以存储重复元素,
//set集合不可以存储重复元素,
//=====================Collection(单列集合的根接口)集合类===================================
//基本功能
ArrayList c = new ArrayList();
boolean b1 = c.add("abc");//boolean add(E e);添加集合,任意类型,自动装箱
boolean b2 = c.add(true);
boolean b3 = c.remove("abc");//boolean remove(object o);
boolean b4 = c.add("abc");
boolean b5 = c.contains("a");//boolean contains(object o);是否包含
boolean b6 = c.isEmpty();//boolean isEmpty();
int num = c.size();//int size();集合元素个数
c.clear();//void clear();
//=====================集合转换素组遍历===================================
Collection<String> c1 = new ArrayList();
c1.add("a");
c1.add("b");
c1.add("c");
c1.add("d");
Object[] arr = c1.toArray();//集合转换成数组
for (int i = 0; i < arr.length; i++) {
// System.out.println(arr[i]);
}
Collection c2 = new ArrayList();
c.add(new Student("张三",23));
c.add(new Student("李四",24));
c.add(new Student("王五",25));
Object[] arr2 = c2.toArray();
for (int i = 0; i < arr2.length; i++) {
Student s = (Student)arr2[i];
// System.out.println(s.getName());
}
/**
* boolean addAll(Collection c);//合并两个集合
* boolean removeAll(Collection c);清空集合
* boolean containsAll(Collection c);//是否包含
* boolean retainAll(Collection c);//取交集
*/
Collection c3 = new ArrayList();
c3.add("a");
c3.add("c");
c3.add("d");
Collection c4 = new ArrayList();
c4.add("e");
c4.add("f");
c3.addAll(c4);//[a, c, d, e, f]
c3.add(c4);//[a, c, d, e, f, [e, f]]
c3.removeAll(c4);//[a, c, d, [e, f]]
boolean b7 = c3.contains(c4);
boolean b8 = c3.retainAll(c4);
//=====================集合转换素组遍历(迭代遍历)===================================
Collection c5 = new ArrayList();
c5.add("a");
c5.add("b");
c5.add("c");
Iterator it = c5.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
//=====================List集合===================================
/**
* void add(int index,E element);//添加
* E remove(int index);//删除
* E get(int indx);获取
* E set(int index,E element);修改
*/
List<Student> list = new ArrayList<Student>();
list.add(new Student("张三",23));
list.add(new Student("李四",24));
list.add(new Student("王五",25));
list.set(1, new Student("赵六",25));
list.remove(1);
// for (int i = 0; i < list.size(); i++) {
// Student s = (Student)list.get(i);
// System.err.println(s.getName());
// }
//=====================并发修改异常产生的原因===================================
List<String> list2 = new ArrayList<String>();
list2.add("a");
list2.add("world");
list2.add("a");
/*Iterator<String> it1 = list2.iterator();
while (it1.hasNext()) {
String str = (String)it1.next();
if (str.equals("world")) {
list2.add("javaee");
//这里会抛出ConcurrentModificationException并发修改异常
}
}*/
/**解决办法:
* * A:迭代器迭代元素,迭代器修改元素(ListIterator的特有功能add)
* * B:集合遍历元素,集合修改元素
*/
ListIterator<String> lit = list2.listIterator();
while (lit.hasNext()) {
String str = (String)lit.next();
if (str.equals("world")) {
lit.add("javaee");
}
}
//=====================数据结构之数组和链表===================================
/**
* 数组
* 查询、修改快
* 增删慢
* 链表
* 查询、修改慢
* 增删快
*
*
* * A:List的三个子类的特点
*
ArrayList:
底层数据结构是数组,查询快,增删慢。
线程不安全,效率高。
Vector:
底层数据结构是数组,查询快,增删慢。
线程安全,效率低。
Vector相对ArrayList查询慢(线程安全的)
Vector相对LinkedList增删慢(数组结构)
LinkedList:
底层数据结构是链表,查询慢,增删快。
线程不安全,效率高。
Vector和ArrayList的区别
Vector是线程安全的,效率低
ArrayList是线程不安全的,效率高
共同点:都是数组实现的
ArrayList和LinkedList的区别
ArrayList底层是数组结果,查询和修改快
LinkedList底层是链表结构的,增和删比较快,查询和修改比较慢
共同点:都是线程不安全的
* B:List有三个儿子,我们到底使用谁呢?
查询多用ArrayList
增删多用LinkedList
如果都多ArrayList
*/
java---Collection(单列集合的根接口)
最新推荐文章于 2022-08-12 01:50:33 发布