• java Collection和Map接口的区别


    一、Collection接口
    Collection是最基本的集合接口,一个Collection代表一组Object,即Collection的元素(Elements)。一些Collection允许相同的元素而另一些不行。一些能排序而另一些不行。

    JavaSDK不提供直接继承自Collection的类,Java SDK提供的类都是继承自Collection的“子接口”如List和Set。

    Collection 接口的接口 对象的集合(单列集合)
    ├——-List 接口:元素按进入先后有序保存,可重复
    │—————-├ LinkedList 接口实现类, 链表, 插入删除, 没有同步, 线程不安全
    │—————-├ ArrayList 接口实现类, 数组, 随机访问, 没有同步, 线程不安全
    │—————-└ Vector 接口实现类 数组, 同步, 线程安全
    │ ———————-└ Stack 是Vector类的实现类
    └——-Set 接口: 仅接收一次,不可重复,并做内部排序
    ├—————-└HashSet 使用hash表(数组)存储元素
    │————————└ LinkedHashSet 链表维护元素的插入次序
    └ —————-TreeSet 底层实现为二叉树,元素排好序
     

    1. public class PracticeCollection {
    2. /**
    3. * 向集合中插入重复的值
    4. * @return
    5. */
    6. public static ArrayList makeList(){
    7. ArrayList testList=new ArrayList<>();
    8. testList.add("tfq");
    9. testList.add("tfq");
    10. testList.add("tfq");
    11. testList.add("tfq1");
    12. testList.add("tfq1");
    13. return testList;
    14. }
    15. /**
    16. * 不能存入重复的值到Set
    17. * @return
    18. */
    19. public static Set makeSet(){
    20. Set testList=new HashSet<>();
    21. testList.add("tfq");
    22. testList.add("tfq");
    23. testList.add("tfq");
    24. testList.add("tfq1");
    25. testList.add("tfq1");
    26. return testList;
    27. }
    28. public static void main(String[] args) {
    29. System.out.println("ArrayList打印:------>");
    30. makeList().stream().forEach(x->{
    31. System.out.println(x);
    32. });
    33. System.out.println("HashSet打印:------>");
    34. makeSet().stream().forEach(x->{
    35. System.out.println(x);
    36. });
    37. }
    38. }
    1. public class PracticMap {
    2. /**
    3. * 接口实现类 ,没有同步, 线程不安全,通过Collections工具类解决集合类HashMap线程不安全问题
    4. *
    5. * @return
    6. */
    7. public static Map makeHashMap() {
    8. Map map = Collections.synchronizedMap(new HashMap<>());
    9. //模拟30个线程,往HashMap集合中添加数据
    10. for(int i = 1; i <= 30; i++) {
    11. new Thread(() -> {
    12. map.put(Thread.currentThread()
    13. .getName(), UUID.randomUUID()
    14. .toString()
    15. .substring(0, 8));
    16. System.out.println(map);
    17. }).start();
    18. }
    19. return map;
    20. }
    21. /**
    22. * 通过JUC包下的并发集合类解决HashMap线程不安全问题,jdk1.8 API中的并发集合类截图如下
    23. * @return
    24. */
    25. public static Map makeConcurrentHashMap() {
    26. Map map =new ConcurrentHashMap<>();
    27. //模拟30个线程,往HashMap集合中添加数据
    28. for(int i = 1; i <= 30; i++) {
    29. new Thread(() -> {
    30. map.put(Thread.currentThread()
    31. .getName(), UUID.randomUUID()
    32. .toString()
    33. .substring(0, 8));
    34. System.out.println(map);
    35. }).start();
    36. }
    37. return map;
    38. }
    39. public static void main(String[] args) {
    40. makeConcurrentHashMap();
    41. }
    42. }

  • 相关阅读:
    rabbitMQ的exchanages类型以及使用场景
    多版本并发控制 MVCC
    kafka优化配置,Kafka 的消费者客户端详解
    安装thinkphp6并使用多应用模式,解决提示路由不存在解决办法
    8年经验面试官详解 Java 面试秘诀
    C++ 关键字
    [⑤ADRV902x]: TES (Transceiver Evaluation Software) 使用
    为什么Vue(默认情况下)比React性能更好
    接口测试学习笔记(基础知识、Jmeter、Postman)
    centos 根目录逻辑卷扩容/home -> /
  • 原文地址:https://blog.csdn.net/developerFBI/article/details/126711217