编程笔记_J2SE_LinkList&ArrayList&HashMap
来源:互联网 发布:微信小程序 源码下载 编辑:程序博客网 时间:2024/05/17 04:50
集合概述
Collection接口 与之并列的还有一个 Map接口
Iterator 迭代器接口
使用Array存储对象有一些弊端,集合就像一种容器,可以动态地把多个对象的引用放入容器中。
集合可以用于存储数量不等的多个对象,还可用于保存具有映射关系的关联数组。
实线表示 实现 ,虚线表示继承
图1
图2
Collection接口的常用方法
Collection coll1 = Arrays.asList(1,2,3);
coll.addAll(coll1);
打印引用流,查看集合元素
System.out.print(coll)
coll.add(new Person("MM",18));
coll.contains(new Person("MM",18));// 返回false
注:如果需要返回true,需要重写Person类的equals方法
方法: eclipse---》source----》override hashcode() and equals()
Iterator iterator = coll.iterator();
while(iterator.hasNext()){
system.out.print(iterator.next()); // 输出next()的同时,指针会后移
iterator = iterator.next();
}
将形参coll1中包含的所有元素添加到当前集合中
coll.addAll(coll1);
判断集合coll1中的所有元素是不是都在coll中
coll.containsAll(coll1)
coll.removeAll()
hashcode()
toArray()
coll.contains()
判断的依据根据所在类的equals(),如果是自定义类的对象需要重写equals()方法
coll.clear()
coll.isEmpty
coll.size()
元素的个数
coll.add
注:
1、size()返回的是元素的个数, 因为集合的容量是动态的,这里的元素个数就是集合容量,这一点与普通的数组不同。
2、add(Object o) 集合对对象没有一致性要求
集合遍历的两种方法
迭代器的hasNext()配合next()方法,使用next()方法后指针自动加一
增强的for循环实现集合的遍历(补充:也可以实现数组的遍历)
for(Object I : coll){
System.out.println(i);
}
面试题:
输出一下代码的答案
String[] str = new String[]{"AA","BB","CC"}
for(String s: str){
s="MM";
sysout.(s);
}
for(int i = 0; I < str.length; i++){
sysout(str[i]);
}
控制台结果:
MM
MM
MM
AA
BB
CC
错误答案:
MM
MM
MM
MM
MM
MM
分析:此处的s是新定义的局部变量,其值得修改不会对str产生影响
ArrayList及List的常用方法
截图 4:00
ArrayList:List的主要实现类。 List是Collection的主要子接口。
List list = new ArrayList();
List相对于Collection中新增加的方法
增
add( object)
在指定索引位置添加对象
addAll(int index,Collection)
删
remove(int index)
删除指定索引位置的元素
改
Object set(index,Object)
设置指定索引位置的元素
查
get(index)
插
add(index, object)
int indexOf(object)
没有返回-1。 会调用equals()方法
inr lastIndexOf(obj)
没有返回-1。 会调用equals()方法
List subList(fromIndex,toIndex)
LinkedList及Vector简介
ArrayList本质上用数组实现,LinkedList本质上用链表实现,不过LinkedList在查找时仍然是有索引的。
LinkedList因为本质上是链表,所以长于频繁的插入、删除操作。
Vector是一个古老的实现类,使用很少了,特点是线程安全的。什么是线程安全的?
Set的主要实现类HashSet
其他实现类 LinkedHashSet、TreeSet
Set set = new HashSet();
set.add("null"); //HashSet的一个特点,可以添加null
注:无序性不等于随机性。
HashSet不是线程安全的。
Set中的元素是如何存储的?使用了哈希算法,具有良好的查找和存取性能。
当向 HashSet 集合中存入一个元素时,HashSet 会调用该对象的 hashCode() 方法来得到该对象的 hashCode 值,然后根据 hashCode 值决定该对象在 HashSet 中的存储位置。
HashSet 集合判断两个元素相等的标准。
先判断 hashCode() 是否存在,若存在再用 equals() 方法判断值是否相等。
添加进set的类一定要重写equals和hashCode(保证不可重复性)
常用方法
将形参coll1中包含的所有元素添加到当前集合中
coll.addAll(coll1);
判断集合coll1中的所有元素是不是都在coll中
coll.containsAll(coll1)
coll.removeAll()
hashcode()
toArray()
coll.contains()
判断的依据根据所在类的equals(),如果是自定义类的对象需要重写equals()方法
coll.clear()
coll.isEmpty
coll.size()
元素的个数
coll.add
*LinkedHashSet
使用链表维护了添加进集合中的顺序,导致到我们遍历集合元素时,是按照添加进的顺序遍历的。但要注意底层的存储依然是无序的。
*TreeSet
只能添加同一个类的元素。
可以按照添加集合中的元素的指定的顺序遍历。想String、包装类[1]等默认按照从小到大的书序遍历。
包装类 包装类即使把基本类型变成对象类型
像ArrayList这样的集合是不能储存基本类型的只能储存对象 为了方便这些集合的使用所以才有了把基本类型包装成对象类型
ArrayList<Integer> al = new ArrayList<Integer>();
Integer i = 1;
al.add(i);
//直接把数字写进去也可以
al.add(2);
自定义类需要重写compareTo()方法,有两种排序方法(1)自然排序;(2)定制排序
(1)自然排序要求实现java.lang.Comparable接口并重写其comepareTo(Object obj)方法
(2)定制排序:创建一个实现了Compatator接口的的类对象
//1.创建一个实现了Comparator的接口的类的对象
Comparator com = new Comparator(){
@Override
public int compare(Object o1,Object o2){
if(o1 instanceof Customer && o2 instanceof Customer){
Customer c1 = (Customer)01;
Customer c2 = (Customer)o2;
int I = c1.getId().compareTo(c2.getId());
if(i==0){
return c1.getName().compareTo(c2.getName());
}
}
return i;
}
}
//2.将这个Comparator对象作为形参传递给TreeSet的构造器中
TreeSet set = new TreeSet( com );
Map接口 与Collection并列
- 编程笔记_J2SE_LinkList&ArrayList&HashMap
- HashMap,HashSet,Hashtable,Vector,ArrayList 笔记
- arraylist hashmap
- HashTable,HashMap/ArrayList,Vector
- HashMap还是ArrayList?
- java hashMap, ArrayList
- String,arraylist, hashmap初始化
- HashMap、ArrayList删除元素
- Arraylist HashMap HashSet 遍历
- HashMap嵌套ArrayList
- ArrayList嵌套HashMap
- ArrayList&LinkedList;HashMap&HashTable
- 遍历HashMap和ArrayList
- Vector & ArrayList ,ArrayList & LinkedList,Hashtable & HashMap
- Hashtable和HashMap的区别 Vector、ArrayList和List的异同(笔记) 《转》
- 【转】Android学习笔记——hashmap、arraylist(vector)与listview的应用
- ArrayList和Vector的区别,HashMap和Hashtable的区别---小笔记
- ArrayList &HashMap&Vector的使用
- 230 - Borrowers
- 511 - Do You Know the Way to San Jose
- 822 - Queue and A
- PHP中错误控制@运算符
- 1593 - Alignment of Code
- 编程笔记_J2SE_LinkList&ArrayList&HashMap
- 1594 - Ducci Sequence
- 1595 - Symmetry
- 1596 - Bug Hunt
- 1597 - Searching the Web
- 1598 - Exchange
- 10391 - Compound Words
- 10763 - Foreign Exchange
- [CODEVS 3044] 矩形面积求并