侧边栏壁纸
  • 累计撰写 793 篇文章
  • 累计创建 1 个标签
  • 累计收到 1 条评论
标签搜索

目 录CONTENT

文章目录

集合

Dettan
2021-04-10 / 0 评论 / 0 点赞 / 165 阅读 / 1,218 字
温馨提示:
本文最后更新于 2022-07-23,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。
stream的 toArray(BlockNode[]::new) 可以传要返回的类型,要不然都是Object类型.
还有一个 toArray(new String[0])
toArray() 里只能转换Object类型, 不能转换基本类型.
包含char的Object 转成String : return Arrays.stream(contents).map(a -> String.valueOf(a.content)).collect(Collectors.joining());
map遍历中删除
删除后再进入下一重循环就会报错
具体错误
java.util.ConcurrentModificationException: null
        at java.util.HashMap$HashIterator.nextNode(HashMap.java:1445) ~[na:1.8.0_262]
        at java.util.HashMap$EntryIterator.next(HashMap.java:1479) ~[na:1.8.0_262]
        at java.util.HashMap$EntryIterator.next(HashMap.java:1477) ~[na:1.8.0_262]
        at com.learn.utils.GlobalCache.clearOld(GlobalCache.java:48) ~[classes!/:0.0.1-SNAPSHOT]
        at com.learn.utils.GlobalCache.remove(GlobalCache.java:42) ~[classes!/:0.0.1-SNAPSHOT]
        at com.learn.controller.UuserController.register(UuserController.java:134) ~[classes!/:0.0.1-SNAPSHOT]
        at com.learn.controller.UuserController$$FastClassBySpringCGLIB$$44b01fc1.invoke(<generated>) ~[classes!/:0.0.1-SNAPSHOT]
        at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[spring-core-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
        at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:771) ~[spring-aop-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
        at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749) ~[spring-aop-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
        at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:56) ~[spring-aop-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:175) ~[spring-aop-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
        at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749) ~[spring-aop-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
        at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:95) ~[spring-aop-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
        at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749) ~[spring-aop-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
        at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:691) ~[spring-aop-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
        at com.learn.controller.UuserController$$EnhancerBySpringCGLIB$$db294a1d.register(<generated>) ~[classes!/:0.0.1-SNAPSHOT]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_262]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_262]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_262]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_262]
用iterator解决,调用iterator的remove方法删除即可. map.getKeySet() 获取的set和map是同步的,删除时也会报异常.
iterator 修改值后再删除还是可能会出异常,详细
public static void clearOld() {
    HashSet<String> watiForDelete = new HashSet<>();
    for (Map.Entry<String, Long> entry : cacheTime.entrySet()) {
        if (new Date().getTime() > entry.getValue()) {
            watiForDelete.add(entry.getKey());
        }
    }
    for (String each : watiForDelete) {
        cache.remove(each);
        cacheTime.remove(each);
    }
}
Iterator<String> iterator = cantFind.keySet().iterator();
while (iterator.hasNext()){
    String k = iterator.next();
    int a = order.indexOf(k);
    order.addAll(a + 1, Arrays.asList(cantFind.get(k)));
    iterator.remove();
}

两个顶级类,Collection 和 Map

集合有序无序是指插入有序,比如,HashSet就是不按输入顺序进行存储,而LinkedHashSet就输入有序.
单列集合
      Collection
            List:(元素有序,可重复)
                    LinkedList:链表结构 , 线程不安全
                    ArrayList:数组结构 ,线程不安全
                    Vector : 数组结构 ,线程安全
            Set:(元素唯一即不可重复)线程不安全,类介绍里写着怎么弄安全了。
                    HashSet:底层为哈希表,可以插入null(彻底无序即不是FIFO,也不排序)
                                    保证元素唯一性:先用hashCode(),然后用equals()
                    LinkedHashSet:底层为链表和哈希表,可以插入null  (FIFO插入有序)
                    TreeSet:底层为红黑树 ,不可以插入null (随时有序)
双列集合
      Map(键唯一,值不唯一,有序都是指键有序)键的唯一性原理与HashSet有关。基本操作(add、contains 和 remove)
            HashMap:底层是哈希表,不同步的,允许null值和null键
            Hashtable:底层是哈希表,同步的,不允许null值和null键 
            LinkedHashMap:底层是哈希表,插入有序,不同步,允许null值和null键
          TreeMap:底层是红黑树结构,对元素自然排序,不允许null键    排序原理与TreeSet一致
    
功能接口
          Iterator:迭代器接口:用于遍历集合
          ListIterator:列表迭代器
  
          Comparable:比较性
          Comparator:比较器  
  
工具类
          Collections
          Arrays

TreeMap和TreeSet在排序时如何比较元素?Collections工具类中的sort()方法如何比较元素?

0

评论区