对字符串的处理:比较是否相同,copy对象属性,copy列表list

系列文章目录


//1.copy 
BeanUtils.copyProperties(A, B);//把a对象copy给b

List<User> users=new ArrayList<>();
List<Person> persons= BeanUtil.copyToList(users, Person.class);

//2.比较两个list
 public static void main(String[] args) {
        List<Person> list1 = Arrays.asList(new Person("Alice", 30), new Person("Bob", 25));
        List<Person> list2 = Arrays.asList(new Person("Alice", 30), new Person("Bob", 25));
        List<Person> list3 = Arrays.asList(new Person("Alice", 30), new Person("Charlie", 25));

        System.out.println(CollectionUtils.areCollectionsEqual(list1, list2)); // true
        System.out.println(CollectionUtils.areCollectionsEqual(list1, list3)); // false
    }

 /**
     * 判断两个集合中所有字段值是否相等
     * @param collection1
     * @param collection2
     * @return
     * @param <T>
     */
    public static <T> boolean areCollectionsEqual(Collection<T> collection1, Collection<T> collection2) {
        if (collection1.size() != collection2.size()) {
            return false;
        }

        T[] array1 = (T[]) collection1.toArray();
        T[] array2 = (T[]) collection2.toArray();

        for (int i = 0; i < array1.length; i++) {
            if (!areObjectsEqual(array1[i], array2[i])) {
                return false;
            }
        }

        return true;
    }

//3.判空
StrUtil.isNotBlank();
ObjectUtil.isNotEmpty();
ObjectUtil.isAllNotEmpty()//多个判空
CollUtil.isNotEmpty()//list判空
Optional.ofNullable(appInfo).orElseThrow(() -> new BizException("xxxx"));//为空抛异常(BizException为自定义方法)

//4.是否包含
StrUtil.contains(aaaaaString, "*");

//5.是否相等
ObjectUtil.equals("1","2")

//hutool工具获取随机值
RandomUtil.randomStringUpper

//雪花算法生成id(保证唯一)
Long id=SnowflakeIdGeneratorUtil.getInstance().nextId()
public class SnowflakeIdGeneratorUtil {
    private static final long twepoch = 1288834974657L;
    private static final long workerIdBits = 10L;
    private static final long sequenceBits = 12L;
    private static final long maxWorkerId = -1L ^ (-1L << workerIdBits);
    private static final long workerIdShift = sequenceBits;
    private static final long timestampLeftShift = sequenceBits + workerIdBits;
    private static final long sequenceMask = -1L ^ (-1L << sequenceBits);

    private long workerId;
    private long sequence = 0L;
    private long lastTimestamp = -1L;

    private static SnowflakeIdGeneratorUtil instance = null;

    public static SnowflakeIdGeneratorUtil getInstance() {
        if (instance == null) {
            synchronized (SnowflakeIdGeneratorUtil.class) {
                if (instance == null) {
                    instance = new SnowflakeIdGeneratorUtil(1);
                }
            }
        }
        return instance;
    }

    private SnowflakeIdGeneratorUtil(long workerId) {
        if (workerId < 0 || workerId > maxWorkerId) {
            throw new IllegalArgumentException("Worker ID can't be greater than " + maxWorkerId + " or less than 0");
        }
        this.workerId = workerId;
    }

    public synchronized long nextId() {
        long timestamp = timeGen();

        if (timestamp < lastTimestamp) {
            throw new RuntimeException("Clock moved backwards. Refusing to generate ID.");
        }

        if (timestamp == lastTimestamp) {
            sequence = (sequence + 1) & sequenceMask;
            if (sequence == 0) {
                timestamp = tilNextMillis(lastTimestamp);
            }
        } else {
            sequence = 0;
        }

        lastTimestamp = timestamp;
        return ((timestamp - twepoch) << timestampLeftShift) | (workerId << workerIdShift) | sequence;
    }

    private long tilNextMillis(long lastTimestamp) {
        long timestamp = timeGen();
        while (timestamp <= lastTimestamp) {
            timestamp = timeGen();
        }
        return timestamp;
    }

    private long timeGen() {
        return System.currentTimeMillis();
    }
}




  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值