目录
一、List
List:数据是有顺序(添加的先后顺序))的,数据是可以重复。
- ArrayList:内部结构是数组。比较适合做高频率的查找,遍历
- LinkedList:双向链表。比较适合做高频率的新增和删除
- vector:和Arraylist几乎一模一样。(向量)
面试题:
1.collection和Map接口的区别
- Collection接口,包含list和set子接口
- Collection和Map接口之间的主要区别在于:Collection中存储了一组对象,而Map存储关键字/值对。
- 在Map对象中,每一个关键字最多有一个关联的值。
2.ArrayList和LinkedList的区别
- ArrayList是实现了基于动态数组的数据结构,而LinkedList是基于链表的数据结构;
- 对于随机访问get和set,ArrayList要优于LinkedList,因为LinkedList要移动指针;
- 对于添加和删除操作add和remove,一般LinkedList要比ArrayList快
3.ArrayList和NVector的区别
- Vector中的public方法多数添加了synchronized关键字,以确保方法同步,也即是Vector线程安全,ArrayList线程不安全。
- ArrayList有两个属性,存储数据的数组elementData,和存储记录数目的size。
- Vector有三个属性,存储数据的数组elementData,存储记录数目的elementCount,还有扩展数组大小的扩展因子capacityIncrement。
public class Ch01 {
public static void main(String[] args) {
LinkedList<String> list=new LinkedList<>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
//我们在创建对象的时候用的是多态
// 父类对象---子类引用
//我们创建出来的对象只能够调用父类和子类中都有的方法
list.addFirst("z");
list.addLast("x");
list.removeFirst();
list.removeLast();
//根据下标索引
System.out.println(list.get(2));
System.out.println(list);
}
}
二、collection接口:
List:有顺序,元素可以重复,顺序指的是添加的先后顺序
set:没有顺序,元素不可以重复,顺序指的是添加的先后顺序
set其实是有顺序,内部有一个专门排序的算法。
1.所谓的无序不等于随机
2.所谓的无序指的是没有按照添加的先后顺序,其实内部是做了排序的。
面试题:
1.List和Set的区别
- List和Set之间很重要的一个区别是是否允许重复元素的存在,在List中允许插入重复的元素,而在Set中不允许重复元素存在。
- 与元素先后存放顺序有关,List是有序集合,会保留元素插入时的顺序,Set是无序集合。
- List可以通过下标来访问,而Set不能。
2.HashSet和LinkedHashSet的区别和联系
HashSet类是LinkedHashSet类的父级。
实现HashSet的基础数据结构是Hashtable。
在HashSet插入中,不保留顺序,这意味着元素的插入顺序不需要与元素的检索顺序相同。
此HashSet类在Java 1.2的早期版本中引入。
如果元素的插入顺序不重要,则应使用HashSet。
LinkedHashset:在添加数据的同时维护数据的添加顺序效率要比Hashset略低一些。
public class Ch02 {
public static void main(String[] args) {
Set<Integer> set = new HashSet<>();
set.add(1);
set.add(54);
set.add(6);
set.add(34);
set.add(67);
set.remove(34);
set.add(66);
System.out.println(set);
}
}
3. set集合如何确保数据的不重复?
保证数据类型的类要重写hashcode和equals方法。
HashSet:
public class Person {
public boolean getId;
private String name;
private String id;
public Person(String name, String id) {
this.name = name;
this.id = id;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
if (!name.equals(person.name)) return false;
return id.equals(person.id);
}
@Override
public int hashCode() {
int result = name.hashCode();
result = 31 * result + id.hashCode();
return result;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", id='" + id + '\'' +
'}';
}
public class Ch03 {
public static void main(String[] args) {
Set<Person> set = new HashSet<>();
set.add(new Person("zz","123"));
set.add(new Person("aa","234"));
set.add(new Person("dd","345"));
set.add(new Person("ff","456"));
System.out.println(set);
}
}
TreeSet:
public class Ch04 {
public static void main(String[] args) {
Set<Integer> tree=new TreeSet<>();
tree.add(-100);
tree.add(100);
tree.add(10);
tree.add(50);
Set<String> tree1=new TreeSet<>();
tree1.add("1");
tree1.add("a");
tree1.add("aaaaa");
tree1.add("哈哈哈");
tree1.add("&");
System.out.println(tree);
System.out.println(tree1);
}
}
ArrayList:
public static void main(String[] args) {
List<Person> list = new ArrayList<>(16);
list.add(new Person("张三", "1234"));
list.add(new Person("李四", "4567"));
list.add(new Person("历三", "3423"));
list.add(new Person("看三", "1266"));
System.out.println(list);
}
HashMap(可以返回空)、Hashtable:
public static void main(String[] args) {
Map<String,String> map=new HashMap<>();
map.put(null,null);
System.out.println(map);
Hashtable<String,String> hashtable=new Hashtable<>();
hashtable.put(null,null);
System.out.println(hashtable);
}
List集合的遍历:
public static void main(String[] args) {
List<String> list= new ArrayList<>();
list.add( "a");
list.add("b");
list.add( "c ");
list.add( "d" );
// 1.for循环
for (int i = 0; i < list.size( ); i++) {
System.out.println(list.get(i));
}
// 2.foreach语句
for (String s : list) {
System.out.println( s);
}
//3.迭代器
Iterator<String> iterator = list.iterator();
while(iterator.hasNext()) {
String s = iterator.next();
System.out.println(s);
}
}
4. Map接口:
1.存储对值K-v key-value
2.key不能重复,value是可以重复的
3.没有顺序(添加的先后顺序)
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put("张三", "1234");
map.put("李四", "4567");
map.put("张三", "124");
map.put("历三", "3423");
map.put("三", "3423");
//4.迭代
Set<String> strings = map.keySet();
Iterator<String> iterator = strings.iterator();
while (iterator.hasNext()) {
String s = iterator.next();
System.out.println(s + "->" + map.get(s));
}
//3.增强for循环
// Entry是hashmap的一个内部类//每一组键值对就是一个Entry对象
Set<Map.Entry<String, String>> entries = map.entrySet();
for (Map.Entry<String, String> entry : entries) {
System.out.print(entry.getKey() + "->");
System.out.println(entry.getValue());
}
}
5. HashMap内部存储结构:
jdk1.7之前:链表+二叉树
jdk1.7及之后;链表+数组+红黑树
HashMap面试90%问原理!!!
6. Properties:属性
Properties是Hashtable的子类,更多地是用来操作属性文件。
迭代中删除元素
public static void main(String[] args) {
List<String> names = new ArrayList<>();
names.add("tom");
names.add("lucy");
names.add("lucy");
names.add("lucy");
names.add("jerry");
for (int i = 0; i < names.size(); i++) {
if (Objects.equals(names.get(i), "lucy")) {
names.remove(i);
//1.回调指针
i--;
}
}
//2.逆序遍历
for (int i = names.size()-1; i >=0; i--) {
if (Objects.equals(names.get(i), "lucy")) {
names.remove(i);
}
}
//3.使用迭代器
Iterator<String> iterator = names.iterator();
while (iterator.hasNext()) {
String s = iterator.next();
if (Objects.equals(s, "lucy")) {
iterator.remove();
}
}
//4.增强for循环
for (String s1 : names) {
if (Objects.equals(s1, "lucy")) {
names.indexOf(s1);
}
}
System.out.println(names);
}
7. 其他的集合:
1.LinkedHashMap,在HashMap的基础上维护了一个双向链表。
2.TreeMap:天然支持排序
3.collections: collections是一个工具类
public class Ch05 {
public static void main(String[] args) {
LinkedHashSet<String> set=new LinkedHashSet<>();
set.add("1");
set.add("a");
set.add("aaaaa");
set.add("哈哈哈");
set.add("&");
System.out.println(set);
}
}
8.面试题
线程安全问题:
迭代器是依赖于集合而存在,在判断成功以后,集合中新增了元素,
迭代器不知道,所以就报错。
解决:
1.迭代器遍历元素,迭代器删除元素
2.普通for循环遍历,集合删除
public static void main(String[] args) {
List<String> names = new ArrayList<>();
names.add ( "tom" );
names.add( "lucy" ) ;
names.add( "lucy" );
names.add ( "lucy" ) ;
names.add( "jerry" );
for (String s : names) {
if(Objects.equals(s,"lucy")){
// names.remove(s);
names.add(s);
}
}
}
三、集合需要掌握的:
1.如何创建需要的集合。多态
2.主要用到的是List和Map
3.各种区别?
4.各种集合API的调用
5.比较接口
6.各种集合的特点, 从接口层面,到实现类层面
7.重点集合的内部结构。ArrayList,HashSet,HashMap
8.各种集合的遍历
9.并发问题
最重要的集合:
ArrayList、HashMap
积压的问题:
1.synchronize原理
2.ReentrantLock原理
3.ArrayList原理
4.LinkedList原理
5.HashMap原理*************
6.Hashset原理List->Map->Set
四、心得体会
迭代器有点不明白!!!