Collection 和 List
package day18集合;import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;import org.junit.Test;/** 一、数据(对象)在内存中的存储和管理:①数组 ②集合* * 二、数组:特点: ① 一旦初始化,其长度就是确定的,不可变。* ②数组声明的类型,就决定了可以添加的数据的类型。* * 弊端:① 长度是固定的,不能很好的扩容。* ② 数组的属性和方法比较少。比如:插入、删除、存储的通过显式初始化赋值的数据的个数* ③ 数组只能用来存储有序的、可重复的数据。对于无序的、不可重复的数据,数组无能为力。* * 三、生活中数据的特点: 1.存储有序的、可重复:存储学生的成绩* 2.无序的、不可重复的:超市的购物车;机场的禁飞名单* * 四、Collection接口集合框架* Collection:单列集合跟接口* |-----List:存储有序的、可重复的数据* |-----ArrayList、LinkedList、Vector* |-----Set:存储无序的、不可重复的数据* |-----HashSet、LinkedHashSet、TreeSet* 五、Collection接口中的常用方法* * 结论:存储在Collection中的自定义类的对象,要求此对象所在的类,要重写equals()* */
public class CollectionTest {@Testpublic void test3(){Collection coll = new ArrayList();coll.add(new String("AA"));coll.add(new Person("Tom",12));coll.add(new Person("Tom",12));coll.add("BB");coll.add(123);coll.add(456);//8.retainAll(Collection coll):交集:获取当前集合和coll集合的相同的元素,并返回给当前集合
// Collection coll1 = Arrays.asList(new Integer[]{123,456});//数组--->集合
// coll.retainAll(coll1);
// System.out.println(coll);//9.remove(Object obj):删除:从当前集合中将obj元素删除。返回值为true:删除成功。反之则反。
// boolean isDelete = coll.remove(1234);
// coll.remove(new Person("Tom",12));
// System.out.println(coll);
// System.out.println(isDelete);//10.removeAll(Collection coll):差集。从当前集合中删除其与coll共有的元素,并将结果返回给当前的集合
// Collection coll1 = Arrays.asList(new Integer[]{123,456,125});
// coll.removeAll(coll1);
// System.out.println(coll);//11.equals(Object obj):如果当前集合与obj中的元素完全相同,则返回true.Collection coll2 = new ArrayList();coll2.add(new String("AA"));coll2.add(new Person("Tom",12));coll2.add(new Person("Tom",12));coll2.add("BB");coll2.add(123);
// coll2.add(456);System.out.println(coll.equals(coll2));//12.hashCode():返回当前集合的哈希值。System.out.println(coll.hashCode());//13.toArray():集合--->数组System.out.println(coll);Object[] objs = coll.toArray();for(int i = 0;i < objs.length;i++){System.out.println(objs[i]);}//14.iterator():遍历集合元素 (见IteratorTest.java)}@Testpublic void test2(){Collection coll = new ArrayList();coll.add(new String("AA"));coll.add(new Person("Tom",12));coll.add("BB");coll.add(123);//自动装箱//6.contains(Object obj):判断集合中是否包含obj元素。判断标准:调用obj所在类的:equals()boolean isExist = coll.contains(new String("AA"));System.out.println(isExist);isExist = coll.contains(new Person("Tom",12));System.out.println(isExist);//false--->true//7.containsAll(Collection coll1):当前集合中,是否包含coll集合中的所有元素Collection coll1 = new ArrayList();coll1.add(123);coll1.add("BB");System.out.println("7." + coll.containsAll(coll1));}@Testpublic void test1(){Collection coll = new ArrayList();//1.add(Object obj):将obj添加到集合中coll.add("AA");coll.add("BB");coll.add(123);//自动装箱coll.add(new Person("Tom",12));//2.size():获取集合中存储的元素的个数System.out.println(coll.size());System.out.println(coll);//3.addAll(Collection coll):将coll中的元素添加到当前的集合中。Collection coll1 = new ArrayList();coll1.add(111);coll1.add("MM");coll1.add("GG");coll.addAll(coll1);
// coll.add(coll1);System.out.println(coll);//5.clear():清空集合元素coll.clear();//4.isEmpty():判断集合是否为空System.out.println(coll.isEmpty());}
}
package day18集合;import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;import org.junit.Test;/** 测试集合的遍历操作:* 方式一:调用Collection中的iterator(),返回Iterator迭代器接口实现类* 方式二:使用foreach(增强for)循环实现遍历集合。* 同时,增强for还可以用来遍历数组*/
public class IteratorTest {//增强for循环的笔试题@Testpublic void test5(){Person[] pers = new Person[]{new Person("Tom",12),new Person("Jerry",22)};for(Person p : pers){p = new Person("hanmeimei",33);
// p.setAge(44);}for (int i = 0; i < pers.length; i++) {System.out.println(pers[i]);}}//笔试题@Testpublic void test4(){String[] arr = new String[]{"MM","GG","JJ","DD"};//测试一:
// for(int i = 0;i < arr.length;i++){
// arr[i] = "Tom";
// }//测试二:s作为一个新的局部变量,改变其值,不影响原有数组的值
// for(String s : arr){
// s = "Tom";
// }//遍历for(int i = 0;i < arr.length;i++){System.out.println(arr[i]);}}@Testpublic void test3(){Collection coll = new ArrayList();coll.add(new String("AA"));coll.add(new Person("Tom",12));coll.add("BB");coll.add(123);coll.add(456);// for(;;){
//
// }//增强for循环for(Object obj : coll){System.out.println(obj);}System.out.println("***********************");int[] arr = new int[]{3,5,5,3,5,7,7,43};for(int a : arr){System.out.println(a);}}@Testpublic void test2(){Collection coll = new ArrayList();coll.add(new String("AA"));coll.add(new Person("Tom",12));coll.add("BB");coll.add(123);coll.add(456);//错误一。//next():①指针下移 ②将下移以后位置上的元素返回
// Iterator iterator = coll.iterator();
// while((iterator.next()) != null){
// System.out.println(iterator.next());
// }//错误二:死循寰
// while(coll.iterator().hasNext()){
// System.out.println(coll.iterator().next());
// }}@Testpublic void test1(){Collection coll = new ArrayList();coll.add(new String("AA"));coll.add(new Person("Tom",12));coll.add(new Person("Tom",12));coll.add("BB");coll.add(123);coll.add(456);Iterator iterator = coll.iterator();//方式一:
// System.out.println(iterator.next());
// System.out.println(iterator.next());
// System.out.println(iterator.next());
// System.out.println(iterator.next());
// System.out.println(iterator.next());
// System.out.println(iterator.next());//报NoSuchElementException异常。
// System.out.println(iterator.next());//方式二:不推荐
// for(int i = 0 ;i < coll.size();i++){
// System.out.println(iterator.next());
// }//方式三:使用iterator的hasNext()、next().推荐while(iterator.hasNext()){System.out.println(iterator.next());}}
}
package day18集合;import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;import org.junit.Test;/** Collection:单列集合* |-----List:存储有序的、可重复的数据* |-----ArrayList|-----LinkedList|-----Vector* |-----Set:存储无序的、不可重复的数据* |-----HashSet、LinkedHashSet、TreeSet* * [面试题]ArrayList、LinkedList、Vector三者的异同?* ① 都是作为List接口的实现类,可以用来存储有序的、可以重复的数据* ②ArrayList:作为List接口的主要实现类;ArrayList线程不安全的,效率高;* (使用Collections类的方法转为线程安全的。);底层使用数组实现* * Vector:作为List接口的古老类;Vector线程安全的,效率低;底层使用数组实现* * LinkedList:底层使用双向链表实现的;对于频繁的删除、插入操作,建议使用LinkedList.* * ArrayList的处理数据的操作:* List list = new ArrayList();// new Object(10).* list.add(123);* ...* list.size();* list.get(1);* * 默认空参的构造器底层提供了一个长度为10的数组,当向ArrayList中添加到第11个元素时,就* 需要扩容,数组扩容为原来的1.5倍,需要将原有的数组拷贝到新数组中,继续添加元素即可。* 开发中,建议根据数据的长度,创建合适长度的ArrayList. new ArrayList(int capacity).*/
public class ListTest {/** * void add(int index, Object ele):插入* boolean addAll(int index, Collection eles):将eles中的所有元素从当前集合的指定index位置插入* Object get(int index):查询* int indexOf(Object obj):返回obj元素在当前集合中首次出现的位置。如果不存在,返回-1.* int lastIndexOf(Object obj):返回obj元素在当前集合中末次出现的位置。如果不存在,返回-1.* Object remove(int index):删除指定索引位置的元素。并将此位置上的元素返回* Object set(int index, Object ele):修改指定索引位置上的值,修改为ele* List subList(int fromIndex, int toIndex):截取当前集合从fromIndex到toIndex的左闭右开子集合* * 总结:* 增:add(Object obj)* 删:remove(Object obj) / remove(int index)* 改:set(int index, Object ele)* 查:get(int index)* 插:add(int index ,Object obj)* 长度:size()* 遍历:iterator() / for* */@Testpublic void test3(){List list = new ArrayList();list.add(123);list.add("AA");list.add("AA");list.add(new Person("Tom",12));//方式一:
// Iterator iterator = list.iterator();
// while(iterator.hasNext()){
// System.out.println(iterator.next());
// }//方式二:foreach
// for(Object obj : list){
// System.out.println(obj);
// }//方式三:for(int i = 0;i < list.size();i++){System.out.println(list.get(i));}}@Testpublic void test2(){List list = new ArrayList();list.add(123);list.add("AA");list.add("AA");list.add(new Person("Tom",12));
// System.out.println(list.indexOf("AA"));
// System.out.println(list.lastIndexOf("AA"));
// //删除
// Object value = list.remove(3);value = list.remove(4);
// System.out.println(value);
// //修改
// list.set(1, "MM");
// System.out.println(list);//获取子ListList subList = list.subList(0, 2);System.out.println(subList);}@Testpublic void test1(){List list = new ArrayList();list.add(123);list.add("AA");list.add(new Person("Tom",12));System.out.println(list);//1.插入:add(int index, Object ele)list.add(1, "BB");System.out.println(list);//2.Collection coll = Arrays.asList(1,2,3);list.addAll(2, coll);
// list.add(2,coll);System.out.println(list.size());//7System.out.println(list);//3.get()Object obj = list.get(0);System.out.println(obj);}
}
Collection 和 List相关推荐
- java 集合 接口_Java集合之Collection接口
1 - Java集合介绍 /* 1. 一方面, 面向对象语言对事物的体现都是以对象的形式,为了方便对多个对象 的操作,就要对对象进行存储. 2. 另一方面,使用Array存储对象方面具有一些弊 端,而 ...
- java中collection方法_Java 8中的Collector toCollection()方法
toCollection()Java中的Collector类的方法返回一个Collector,该Collector以遇到的顺序将输入元素累积到一个新的Collection中. 语法如下static & ...
- java map collection_java 集合----Map、Collection
接口:红色:实现类:黑色字体 一.Collection集合 Collection |_____Set(HashSet) | |_____SortedSet(TreeSet) |_____Li ...
- Java学习总结:53(单对象保存父接口:Collection)
单对象保存父接口:Collection java.util.Collection是进行单对象保存的最大父接口,即每次利用Collection接口都只能保存一个对象信息.单对象保存父接口定义如下: pu ...
- 【java】兴唐第二十节课(Collection 和 ArrayList)
(一)Collection 1.如果实现 --able 名称的接口则证明该类或其子类有该功能 (1)实现Iterable接口代表具有迭代功能 (2)实现Cloneable接口代表具有克隆功能 (3)实 ...
- 为何 Map接口不继承Collection接口
1.首先Map提供的是键值对映射(即Key和value的映射),而collection提供的是一组数据(并不是键值对映射). 如果map继承了collection接口,那么所有实现了map接口的类到底 ...
- 【JavaSE】day03_Date、SimpleDateFormat、Calendar、Collection
[JavaSE]day03_Date.SimpleDateFormat.Calendar.Collection 1.Date及其经常使用API 1)JAVA 中的时间 Java中的时间使用标准类库的D ...
- (Mirage系列之六)在Mirage里使用Collection
在Mirage中,Collection是包含一个或多个CVD的集合. Collection的主要作用是简化操作.比如我有一百个终端设备需要分配基础层,如果没有Collection,那么管理员需要逐个点 ...
- Collection集合List、Set
Collection集合,用来保存一组数据的数据结构. Collection是一个接口,定义了所有集合都应该包含的特征和行为 Collection派生出了两类集合 List和Set List接口:Li ...
- Oracle 11g Release 1 (11.1) PL/SQL_多维 Collection 类型和其异常
本文内容 多维 Collection Collection 异常 多维 Collection 虽然 collection 只有一维的,但可以模型一个多维的.创建一个 collection,其每个元素也 ...
最新文章
- 算法----------最长上升子序列(Java 版本)
- POJ1942-Paths On a Grid-组合数学
- 亲手完成4大企业实战项目,挑战Python全栈高薪岗位!
- 51单片机怎么显示当前时间_单片机初学者必看
- android开发之shape详解
- 为Cubieboard打造完美Debian系统
- js获取css文件中的样式
- Selenium2 + Python3.6实战(五):生成HTML测试报告 Invalid argument
- java实现求数组中元素第二大的元素
- hadoop无法停止
- Flex builder3 调试弹出窗口Flex builder cannot locate the required version of Flash Player解决办法
- python发微信工资条_我帮公司财务写了个“群发工资条”的Python脚本!
- 浙江大学计算机专业介绍,浙江大学计算机科学与技术专业课程设置
- 【CF724F】Uniformly Branched Trees 动态规划
- Golang面试题整理
- 游戏术语扫盲贴(手游人必懂)
- 【机器学习开放项目】安然公司电子邮件数据集
- fuchsia代码管理
- ode45 求常微分非线性方程
- latex backmatter 是什么意思
热门文章
- 《Grid Tagging Scheme for Aspect-oriented Fine-grained Opinion Extraction》论文阅读
- Flink Forward Asia 2019 PPT 下载
- indy-sdk tutorials数字身份认证(二)
- python 循环列表删除元素
- 【nlp自然语言处理实战】案例---FastText模型文本分类
- 数据库中的图片字段怎么在报表中呈现 (图片字段呈现)
- html怎么使两个div模块并列居中,css怎样让两个div重叠,怎么让整个div居中
- python零基础培训班多少钱
- 美国一男子从移动电话基站跳伞死亡
- 测试计划一般包括什么?