写在前边
大家好,今天我又来更新干货了,两天没更新了。这几天我在收集和整理公众号要接下来要更新的知识内容,确保每更新一篇让每一个读者都有收获。
这几天我也尽全力的为最近群里组织的企业项目做准备,确保第一批参与的每一位小伙伴能够在实际项目中真正的提高技能和收获项目经验。所以公众号就耽搁了两天。我估计今天再不更新干货小伙伴们都要取关了,哈哈!
今天带来整理的内容是「Java 集合框架之间的比较」。也是面试中面试官最喜欢提问到的,今天的内容希望每个小伙伴都要好好掌握,重中之重。
JAVA集合框架之间的关系
1. ArrayList 与 HashSet
1.1 是否有顺序
① ArrayList: 有顺序
② HashSet: 无顺序
代码:
1ArrayList numberList =new ArrayList();
2//List中的数据按照插入顺序存放
3System.out.println("----------List----------");
4System.out.println("向 List 中插入 9 5 1");
5numberList.add(9);
6numberList.add(5);
7numberList.add(1);
8System.out.println("List 按照顺序存放数据:");
9System.out.println(numberList);
10System.out.println("----------Set----------");
11HashSet numberSet =new HashSet();
12System.out.println("向Set 中插入9 5 1");
13//Set中的数据不是按照插入顺序存放
14numberSet.add(9);
15numberSet.add(5);
16numberSet.add(1);
17System.out.println("Set 不是按照顺序存放数据:");
18System.out.println(numberSet);
1.2 能否重复
① List 中的数据可以重复
② Set 中的数据不能够重复
重复判断标准是 :
① 首先看 hashcode 是否相同
② 如果 hashcode 不同,则认为是不同数据
③ 如果 hashcode 相同,再比较 equals,如果 equals 相同,则是相同数据,否则是不同数据
代码:
1ArrayList numberList = newArrayList();
2//List中的数据可以重复
3System.out.println("----------List----------");
4System.out.println("向List 中插入 9 9");
5numberList.add(9);
6numberList.add(9);
7System.out.println("List 中出现两个9:");
8System.out.println(numberList);
9System.out.println("----------Set----------");
10HashSet numberSet =newHashSet();
11System.out.println("向Set 中插入9 9");
12// Set中的数据不能重复
13numberSet.add(9);
14numberSet.add(9);
15System.out.println("Set 中只会保留一个 9:");
16System.out.println(numberSet);
ArrayList 与 LinkedList
2.1 ArrayList 和 LinkedList 的区别
① ArrayList 插入,删除数据慢。
② LinkedList 插入,删除数据快
③ ArrayList 是顺序结构,所以定位很快,指哪找哪。 就像电影院位置一样,有了电影票,一下就找到位置了。LinkedList 是链表结构,就像手里的一串佛珠,要找出第 99 个佛珠,必须得一个一个的数过去,所以定位慢。
2.2 插入数据
代码:
1public static void main(String[] args) {
2 List l;
3 l = new ArrayList<>();
4 insertFirst(l, "ArrayList");
5 l = new LinkedList<>();
6 insertFirst(l, "LinkedList");
7}
8private static void insertFirst(List l, String type) {
9 int total = 1000 * 100;
10 final int number = 5;
11 //获取当前系统时间
12 long start = System.currentTimeMillis();
13 for (int i = 0; i < total; i++) {
14 l.add(0, number);
15}
16 long end = System.currentTimeMillis();
17 System.out.printf("在%s 最前面插入%d条数据,总共耗时 %d 毫秒 %n", type, total, end - start);
18}
2.3 定位数据
代码:
1public static void main(String[] args) {
2 List l;
3 l = new ArrayList<>();
4 modify(l, "ArrayList");
5 l = new LinkedList<>();
6 modify(l, "LinkedList");
7}
8private static void modify(List l, String type) {
9 int total = 100 * 1000;
10 int index = total/2;
11 final int number = 5;
12 //初始化
13 for (int i = 0; i < total; i++) {
14 l.add(number);
15 }
16 long start = System.currentTimeMillis();
17 for (int i = 0; i < total; i++) {
18 int n = l.get(index);
19 n++;
20 l.set(index, n);
21 }
22 long end = System.currentTimeMillis();
23 System.out.printf("%s总长度是%d,定位到第%d个数据,取出来,加1,再放回去%n 重复%d遍,总共耗时 %d 毫秒 %n", type,total, index,total, end - start);
24 System.out.println();
25}
HashMap 与 HashTable
3.1 HashMap和Hashtable的区别
共同点:HashMap 和 Hashtable 都实现了 Map 接口,都是键值对保存数据的方式
不同点:
区别 1 :
① HashMap 可以存放 null
② Hashtable 不能存放 null
区别 2 :
① HashMap 不是线程安全的类
② Hashtable 是线程安全的类
代码:
1//HashMap和Hashtable都实现了Map接口,都是键值对保存数据的方式
2HashMap hashMap = newHashMap();
3//HashMap可以用null作key,作value
4hashMap.put(null, "123");
5hashMap.put("123", null);
6Hashtable hashtable = newHashtable();
7//Hashtable不能用null作key,不能用null作value
8hashtable.put(null, "123");
9hashtable.put("123", null);
set
4.1 set 种类
① HashSet: 无序
② LinkedHashSet: 按照插入顺序
③ TreeSet: 从小到大排序
代码:
1HashSet numberSet1 =newHashSet();
2//HashSet中的数据不是按照插入顺序存放
3numberSet1.add(88);
4numberSet1.add(8);
5numberSet1.add(888);
6System.out.println(numberSet1);
7LinkedHashSet numberSet2 =newLinkedHashSet();
8//LinkedHashSet中的数据是按照插入顺序存放
9numberSet2.add(88);
10numberSet2.add(8);
11numberSet2.add(888);
12System.out.println(numberSet2);
13TreeSet numberSet3 =newTreeSet();
14//TreeSet 中的数据是进行了排序的
15numberSet3.add(88);
16numberSet3.add(8);
17numberSet3.add(888);
18System.out.println(numberSet3);