Java的骚操作

1. 将数组转换成集合

使用Arrays.asList()

使用Arrays转换得到的集合为不可修改的状态。

使用Java8的Stream
Arrays.stream(arr).boxed().collect(Collectors.toList());

2.将字符串数组倒置

String[] arr = {"h","e","l","l","o"};
List<String> collect = Arrays.stream(arr)
    .sorted(Comparator.reverseOrder())
    .collect(Collectors.toList());
// 将集合转换为数组
String[] strings = new String[arr.length];
collect.toArray(strings);

3.去重

根据自定义方法去重根据自定义方法去重
public static List<Hero> distinctA(List<Hero> heroes){
        return heroes.stream().filter(distinctByKey(o -> o.去重字段1() + o.去重字段2())).collect(Collectors.toList());
    }
// 自定义的去重方法
private static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
        Map<Object, Boolean> seen = new ConcurrentHashMap<>();
        return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }
使用Stream方法去重
 public static List<Hero> distinctB(List<Hero> list){
        return list.stream().collect(
                collectingAndThen(toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.去重字段1() + o.去重字段2()))),
                        ArrayList::new));
    }
通过转换成Set集合去重
public static List<Hero> distinctC(List<Hero> users) {
        return users.stream().parallel().collect(Collectors.toSet()).stream().parallel().collect(Collectors.toList());
    }

4.使用零拷贝的方式复制文件

/**
     * 使用零拷贝的方式复制文件
     * @param sourcePath 资源路径
     * @param targetPath 目标路径
     */
    public static void copyFileWithChannel(String sourcePath, String targetPath) {
        try {
            RandomAccessFile source = new RandomAccessFile(sourcePath, "r");
            FileChannel sourceChannel = source.getChannel();
            RandomAccessFile target = new RandomAccessFile(targetPath, "rw");
            FileChannel targetChannel = target.getChannel();
            targetChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

5.统计每个元素出现的次数

public static Map<String, Integer> sum(@NotNull List<String> heroList){
        return heroList
                .stream()
                .parallel()
                .collect(Collectors.toConcurrentMap(x -> x, x -> 1, Integer::sum));
    }

6.位运算符判断奇偶数

if((i&1)==0){
    System.out.print("这是一个偶数")
}

7.将List转为Tree

 		// 根据pid进行分组
        Map<Integer, List<Persion>> collect = persions.stream()
                .parallel()
                .collect(Collectors.groupingBy(Persion::getPid));
        // 遍历,判断key值和集合中的id是否相等,如果相等就加入到集合中元素的sons字段上
        for (Integer key : collect.keySet()) {
            persions.forEach(p -> {
                if (p.getId().equals(key)){
                    p.setSons(collect.get(key));
                }
            });
        }
        // 将sons字段为空的元素去除
        return  persions.stream()
                .parallel()
                .filter(p -> p.getPid() == 0)
                .collect(Collectors.toList());
		

8.使用Files.walkFileTree遍历文件夹

 Files.walkFileTree(Paths.get(filePath), new SimpleFileVisitor<Path>(){
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                // 获取文件后缀
                String suf = "." + StringUtils.substringAfterLast(file.toString(), ".");
                // 判断改后缀是否在后缀集中,同时判断文件是否问隐藏文件
                if (!Files.isHidden(file) && suffixs.stream().parallel().anyMatch(s -> s.endsWith(suf))) {
                    System.out.println(file);
                }
                return FileVisitResult.CONTINUE;
            }
        });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值