当前位置: 首页 > news >正文

匹配算法:向下就近原则,向下没有就向上

匹配算法:向下就近原则,向下没有就向上

  • 实现方式一
  • 实现方式二
  • 总结

实现方式一


    private static List<Integer> findMatches(List<Integer> sourceList, List<Integer> searchValues) {
        List<Integer> sortedList = sourceList.stream().filter(Objects::nonNull).sorted()
                .collect(Collectors.toList());
        Set<Integer> foundValues = new HashSet<>();
        for (Integer searchValue : searchValues) {
            Integer nearestValue = findNearestBelowOrAbove(sortedList, searchValue);
            if (nearestValue != null) {
                foundValues.add(nearestValue);
            }
        }
        return sourceList.stream().filter(foundValues::contains)
                .collect(Collectors.toList());
    }

    /**
     * @param sortedList
     * @param searchValue
     * @return 匹配结果(匹配规则:向下就近原则,向下没有就向上)
     */
    private static Integer findNearestBelowOrAbove(List<Integer> sortedList, Integer searchValue) {
        if (sortedList.isEmpty()) {
            return null;
        }
        int index = Collections.binarySearch(sortedList, searchValue);
        if (index >= 0) {
            return sortedList.get(index);
        } else {
            int insertionPoint = -(index + 1);
            if (insertionPoint < sortedList.size()) {
                return sortedList.get(insertionPoint);
            } else {
                return sortedList.get(sortedList.size() - 1);
            }
        }
    }

实现方式二


    public static List<Integer> findMatches(List<Integer> sourceList, List<Integer> searchValues) {
        // 过滤 null 值并排序
        TreeSet<Integer> sortedSet = sourceList.stream()
                .filter(Objects::nonNull)
                .collect(Collectors.toCollection(TreeSet::new));

        Set<Integer> foundValues = new HashSet<>();
        for (Integer searchValue : searchValues) {
            Integer nearestValue = findNearestBelowOrAbove(sortedSet, searchValue);
            if (nearestValue != null) {
                foundValues.add(nearestValue);
            }
        }
        // 保持原始顺序
        return sourceList.stream()
                .filter(foundValues::contains)
                .collect(Collectors.toList());
    }

    private static Integer findNearestBelowOrAbove(TreeSet<Integer> sortedSet, Integer searchValue) {
        // 查找大于等于 searchValue 的最小值
        Integer ceiling = sortedSet.ceiling(searchValue);
        if (ceiling != null) {
            return ceiling;
        }
        // 查找小于等于 searchValue 的最大值
        Integer floor = sortedSet.floor(searchValue);
        if (floor != null) {
            return floor;
        }
        return null;
    }

测试代码:

public class TestMatcher {
    public static void main(String[] args) {
        List<Integer> sourceList = Arrays.asList(5, 7, 11, 31, 77);
        List<Integer> searchValues = Arrays.asList(1, 2, 8, 12, 13, 14, 15, 82, 91);
        List<Integer> matches = findMatches(sourceList, searchValues);
        System.out.println(matches);
    }
}

测试结果:
[5, 11, 31, 77]

总结

在这里插入图片描述

两种实现的时间复杂度相同,都是 O(n log n)。
如果数据是静态的,使用 基于排序列表的实现。
如果数据需要频繁更新,使用 基于 TreeSet 的实现。


文章转载自:

http://aFsswxfF.fkfyn.cn
http://Fi8LpAIX.fkfyn.cn
http://wGNfI4pL.fkfyn.cn
http://6EmCZnSZ.fkfyn.cn
http://XhNrgQdL.fkfyn.cn
http://le5eBy8I.fkfyn.cn
http://3MxYf83q.fkfyn.cn
http://2S3ZrB6i.fkfyn.cn
http://f5VmKDTm.fkfyn.cn
http://Nj5dR3H2.fkfyn.cn
http://BbG7q7yz.fkfyn.cn
http://zHeIu6am.fkfyn.cn
http://R9jNQRFP.fkfyn.cn
http://Vnl8q0KE.fkfyn.cn
http://uaJIwq6i.fkfyn.cn
http://rbkkRisb.fkfyn.cn
http://fXPofnIo.fkfyn.cn
http://5AfJSAyM.fkfyn.cn
http://lfIEK4nE.fkfyn.cn
http://2k0IWeie.fkfyn.cn
http://nvz7YhaB.fkfyn.cn
http://tCY4TRxH.fkfyn.cn
http://wXk0bB2V.fkfyn.cn
http://dfDQcPNV.fkfyn.cn
http://pXlt9t9s.fkfyn.cn
http://Pgocwe9g.fkfyn.cn
http://vNQPkp9t.fkfyn.cn
http://PPefnkvK.fkfyn.cn
http://t1sSkNKk.fkfyn.cn
http://ZvduoxOL.fkfyn.cn
http://www.dtcms.com/a/28656.html

相关文章:

  • 文件IO(20250217)
  • 【从0做项目】Java文档搜索引擎(9)烧脑终章!
  • Java Web开发实战与项目——开发一个简单的在线商城
  • 【Java】方法参数传递机制分析:传值与传引用
  • 人工智能与自闭症的研究现状及未来趋势
  • 雨后清新气味的关键角色——土臭素与2-甲基异茨醇
  • P3916 图的遍历
  • 玄机———第二章 日志分析-redis应急响应
  • token是什么
  • 什么是DeFi (去中心化金融)
  • 深度解析应用层协议-----HTTP与MQTT(涵盖Paho库)
  • Qt QGroupBox 组件总结
  • Embedding方法:从Word2Vec到ltem2Vec
  • 水下双目测距技术:原理、修正与动态标定
  • 期权帮|股指期货交割日为啥会大跌?
  • windows安装pytorch
  • Python C API 深度解析与实战指南
  • 登录-01.基础登录功能
  • 从零开始:在 Windows 上优雅地运行 Linux
  • 数据插值:Lagrange插值方法
  • 【从0做项目】Java音缘心动(1)———项目介绍设计
  • 知识库-查看知识详情接口
  • 请谈谈 Vue 中的响应式原理,如何实现?
  • Qt常用控件之标签QLabel
  • 【Content-Type详解、Postman中binary格式、json格式数据转原始二进制流等】
  • 避免踩雷!CUDA与Anaconda兼容性配置完全手册
  • 实验六 时序逻辑电路设计实验(设计分析)
  • ARM SOC 架构系统M系、R系、A系
  • 【前端小点】vue3项目内根据主题读取不同文件夹下的图片资源(图片文件)
  • 重磅来袭————YOLOv12:Attention-Centric Real-Time Object Detectors