写在前边

大家好,今天我又来更新干货了,两天没更新了。这几天我在收集和整理公众号要接下来要更新的知识内容,确保每更新一篇让每一个读者都有收获。

这几天我也尽全力的为最近群里组织的企业项目做准备,确保第一批参与的每一位小伙伴能够在实际项目中真正的提高技能和收获项目经验。所以公众号就耽搁了两天。我估计今天再不更新干货小伙伴们都要取关了,哈哈!

今天带来整理的内容是「Java 集合框架之间的比较」。也是面试中面试官最喜欢提问到的,今天的内容希望每个小伙伴都要好好掌握,重中之重。

JAVA集合框架之间的关系

1. ArrayList 与 HashSet

1.1 是否有顺序

① ArrayList: 有顺序

② HashSet: 无顺序

代码:

 1ArrayList<Integer> numberList =new ArrayList<Integer>(); 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<Integer> numberSet =new HashSet<Integer>();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<Integer> numberList = newArrayList<Integer>(); 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<Integer> numberSet =newHashSet<Integer>();11System.out.println("向Set 中插入9 9");12// Set中的数据不能重复13numberSet.add(9);14numberSet.add(9);15System.out.println("Set 中只会保留一个 9:");16System.out.println(numberSet);
  1. ArrayList 与 LinkedList

2.1 ArrayList 和 LinkedList 的区别

① ArrayList 插入,删除数据慢。

② LinkedList 插入,删除数据快

③ ArrayList 是顺序结构,所以定位很快,指哪找哪。 就像电影院位置一样,有了电影票,一下就找到位置了。LinkedList 是链表结构,就像手里的一串佛珠,要找出第 99 个佛珠,必须得一个一个的数过去,所以定位慢。

2.2 插入数据

代码:

 1public static void main(String[] args) { 2      List<Integer> l; 3      l = new ArrayList<>(); 4      insertFirst(l, "ArrayList"); 5      l = new LinkedList<>(); 6      insertFirst(l, "LinkedList"); 7} 8private static void insertFirst(List<Integer> 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<Integer> l; 3     l = new ArrayList<>(); 4     modify(l, "ArrayList"); 5     l = new LinkedList<>(); 6     modify(l, "LinkedList"); 7} 8private static void modify(List<Integer> 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}
  1. HashMap 与 HashTable

3.1 HashMap和Hashtable的区别

共同点:HashMap 和 Hashtable 都实现了 Map 接口,都是键值对保存数据的方式

不同点:

区别 1 :

① HashMap 可以存放 null

② Hashtable 不能存放 null

区别 2 :

① HashMap 不是线程安全的类

② Hashtable 是线程安全的类

代码:

1//HashMap和Hashtable都实现了Map接口,都是键值对保存数据的方式2HashMap<String,String> hashMap = newHashMap<String,String>();3//HashMap可以用null作key,作value4hashMap.put(null, "123");5hashMap.put("123", null);6Hashtable<String,String> hashtable = newHashtable<String,String>();7//Hashtable不能用null作key,不能用null作value8hashtable.put(null, "123");9hashtable.put("123", null);
  1. set

4.1 set 种类

① HashSet: 无序

② LinkedHashSet: 按照插入顺序

③ TreeSet: 从小到大排序

代码:

 1HashSet<Integer> numberSet1 =newHashSet<Integer>(); 2//HashSet中的数据不是按照插入顺序存放 3numberSet1.add(88); 4numberSet1.add(8); 5numberSet1.add(888); 6System.out.println(numberSet1); 7LinkedHashSet<Integer> numberSet2 =newLinkedHashSet<Integer>(); 8//LinkedHashSet中的数据是按照插入顺序存放 9numberSet2.add(88);10numberSet2.add(8);11numberSet2.add(888);12System.out.println(numberSet2);13TreeSet<Integer> numberSet3 =newTreeSet<Integer>();14//TreeSet 中的数据是进行了排序的15numberSet3.add(88);16numberSet3.add(8);17numberSet3.add(888);18System.out.println(numberSet3);
©著作权归作者所有:来自51CTO博客作者mb5fe1601ede528的原创作品,如需转载,请注明出处,否则将追究法律责任

更多相关文章

  1. WPF核心的技术--数据绑定
  2. 让WebAPI 返回JSON格式的数据实例教程
  3. 怎么在EXECL文件中同时导入数据和插入图片?
  4. 关于.net C# Sql数据库SQLHelper类实例代码
  5. 什么是MVVM架构和数据绑定?
  6. 总结EF通用数据层封装类实例详解
  7. 总结MVC中数据验证实例
  8. 详解VS2015自带LocalDB数据库用法实例
  9. ASP.NET MVC中传参并绑定数据的实例教程

随机推荐

  1. Android组件化导致的代码不生效
  2. java回调C++
  3. 一个 Android 简易的新闻客户端
  4. Android Service Framework分析
  5. Android API教程:人脸检测(上)
  6. Android Studio 从入门到精通(Windows &
  7. Lgame引擎搭建和第一个例子
  8. Android基础控件——ProgressBar自定义的
  9. 【Android】TextView动态设置Drawable资
  10. 解决 “android New package not yet reg