跳至主要內容
Java 解析 Excel 工具 - EasyExcel

Easy Excel 官网介绍:

EasyExcel是一个基于Java的、快速、简洁、解决大文件内存溢出的Excel处理工具。他能让你在不用考虑性能、内存的等因素的情况下,快速完成Excel的读、写等功能。

16M内存23秒读取75M(46W行25列)的Excel(3.2.1+版本)


cpgege原创小于 1 分钟随笔工具
HashMap 源码分析

put 方法

HashMap put 方法流程图
public V put(K key, V value) {
    // 根据 key 的 hashCode 计算出对应的 hash 值,带入 putVal 方法
    return putVal(hash(key), key, value, false, true);
}

/**
 * 扰动函数,减少 hash 碰撞
 */
static final int hash(Object key) {
    int h;
    // ^:按位异或(不同为 1,相同为 0)
    // >>>:无符号右移,忽略符号位,空位补 0
    return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}

final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
                boolean evict) {
    Node<K,V>[] tab; Node<K,V> p; int n, i;
    // 数组为空吗?
    if ((tab = table) == null || (n = tab.length) == 0)
        // 扩容
        n = (tab = resize()).length;
    // 通过公式 (n - 1) & hash 计算要存放的元素对应的数组下标
    if ((p = tab[i = (n - 1) & hash]) == null)
        // 若该下标位置没有元素,则将该元素构造成节点,存入该数组下标位置
        tab[i] = newNode(hash, key, value, null);
    else {
        // 该下标位置有元素
        Node<K,V> e; K k;
        // 如果待存放元素与下标位置元素的 key 相同,则直接覆盖该位置的 value 值
        if (p.hash == hash &&
            ((k = p.key) == key || (key != null && key.equals(k))))
            e = p;
        // 如果下标位置的元素是一棵红黑树,则将该元素加入红黑树
        else if (p instanceof TreeNode)
            e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
        // 如果下标位置的元素是一个链表
        else {
            // 遍历链表
            for (int binCount = 0; ; ++binCount) {
                if ((e = p.next) == null) {
                    // 如果后继节点为空,则将元素构造成节点,加入链表的尾部
                    p.next = newNode(hash, key, value, null);
                    // 当链表长度 > 8 时,考虑是否要将链表转化为红黑树,以提高元素查找效率
                    if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
                        treeifyBin(tab, hash);
                    break;
                }
                // 如果待存放元素与后继节点的 key 相同,则直接覆盖后继节点的 value 值
                if (e.hash == hash &&
                    ((k = e.key) == key || (key != null && key.equals(k))))
                    break;
                // 当前指向后继节点
                p = e;
            }
        }
        if (e != null) { // existing mapping for key
            V oldValue = e.value;
            if (!onlyIfAbsent || oldValue == null)
                e.value = value;
            afterNodeAccess(e);
            return oldValue;
        }
    }
    ++modCount;
    // size 自增后,若 > 阈值,则扩容
    if (++size > threshold)
        resize();
    afterNodeInsertion(evict);
    return null;
}

cpgege原创大约 2 分钟随笔Java 集合
Maven 插件开发

Maven 插件命名规范

Maven 官方维护的插件通常以 maven-<yourplugin>-plugin 格式命名。如果你自己想开发一个插件,必须遵循 <yourplugin>-maven-plugin 格式。

Maven 插件命名

IDEA 中打开 Maven 面板,可以看到项目中依赖的 Maven 插件,其中圈出来的是非官方的插件,也就是我们的自定义插件。

什么是 Mojo


cpgege原创大约 2 分钟随笔maven插件开发
Spring Boot Cache With Redis

本篇文章将介绍如何使用 Redis 作为数据源,实现 Spring Boot 的方法缓存。

引入相关依赖

要实现方法缓存,首先需要引入 spring-boot-starter-cachespring-boot-starter-data-redis 这两个依赖。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

cpgege原创大约 3 分钟随笔Spring BootCache
延迟任务实现方案之 Redisson RDelayedQueue

RedissonDelayedQueueService

/**
 * Redisson 延迟队列服务
 */
@Service
@RequiredArgsConstructor
public class RedissonDelayedQueueService {

    private final RedissonClient redissonClient;

    /**
     * 向指定的队列添加一条延迟消息
     *
     * @param e         消息对象
     * @param delay     延迟时间
     * @param timeUnit  时间单位
     * @param queueName 队列名
     * @param <E>       消息对象类型
     */
    public <E> void add(E e, long delay, TimeUnit timeUnit, String queueName) {
        RBlockingDeque<E> blockingDeque = redissonClient.getBlockingDeque(queueName);
        RDelayedQueue<E> delayedQueue = redissonClient.getDelayedQueue(blockingDeque);
        delayedQueue.offer(e, delay, timeUnit);
    }

    /**
     * 从指定队列移除一条延迟消息
     *
     * @param e         待移除的消息
     * @param queueName 队列名
     * @param <E>       消息对象类型
     */
    public <E> void remove(E e, String queueName) {
        RBlockingDeque<E> blockingDeque = redissonClient.getBlockingDeque(queueName);
        RDelayedQueue<E> delayedQueue = redissonClient.getDelayedQueue(blockingDeque);
        delayedQueue.remove(e);
    }

    /**
     * 从指定队列移除一条延迟消息
     *
     * @param filter    过滤器
     * @param queueName 队列名
     * @param <E>       消息对象类型
     */
    public <E> void removeIf(Predicate<? super E> filter, String queueName) {
        RBlockingDeque<E> blockingDeque = redissonClient.getBlockingDeque(queueName);
        RDelayedQueue<E> delayedQueue = redissonClient.getDelayedQueue(blockingDeque);
        delayedQueue.removeIf(filter);
    }

    /**
     * 获取指定的阻塞队列
     * 消费端使用
     *
     * @param queueName 队列名
     * @param <E>       队列中每条消息对象的类型
     * @return 指定的阻塞队列
     */
    public <E> RBlockingDeque<E> getBlockingQueue(String queueName) {
        return redissonClient.getBlockingDeque(queueName);
    }

}

cpgege原创大约 2 分钟随笔延时队列