首页 > 编程语言 >Java开发技巧-数据结构-使用HashSet判断主键是否存在、使用Pair成对结果返回/Triple三个对象返回

Java开发技巧-数据结构-使用HashSet判断主键是否存在、使用Pair成对结果返回/Triple三个对象返回

时间:2023-06-10 16:35:20浏览次数:49  
标签:返回 ch return string HashSet Triple Pair Java 主键

场景

Java中使用HashSet判断主键是否存在

HashSet实现Set接口,由哈希表(实际上是HashMap)实现,但不保证set的迭代顺序,并允许使用null元素。

HashSet的时间复杂度跟HashMap一致,如果没有哈希冲突则时间复杂度为O(1),

如果存在哈希冲突则时间复杂度不超过O(n)。所以,在日常编码中,可以使用HashSet判断主键是否存在。

案例:

Java中给定一个字符串,返回第一个重复出现的字符

新建工具方法

    public static Serializable findFirstRepeatedChar(String string){
        if(Objects.isNull(string) || string.isEmpty()){
            return null;
        }
        char[] charArray = string.toCharArray();
        Set charSet = new HashSet<>(charArray.length);
        for (char ch:charArray) {
            if(charSet.contains(ch)){
                return ch;
            }
            charSet.add(ch);
        }
        //default return
        return null;
    }

业务调用

        Serializable result = findFirstRepeatedChar("abcda");
        System.out.println(result);

优化方法

Java中由于Set的add函数有个特性:如果添加的元素已经在集合中存在,则会返回false

以上可以优化为

    public static Serializable findFirstRepeatedChar(String string){
        if(Objects.isNull(string) || string.isEmpty()){
            return null;
        }
        char[] charArray = string.toCharArray();
        Set charSet = new HashSet<>(charArray.length);
        for (char ch:charArray) {
            if(!charSet.add(ch)){
                return ch;
            }
            charSet.add(ch);
        }
        //default return
        return null;
    }

Java中使用Pair实现成对结果的返回/Triple实现三个对象一起返回

Pair主要有两种用途:

把key和value放在一起成对处理,主要用于Map中返回键值对,比如Map中的Entry类;

当一个函数需要返回两个结果时,可以使用Pair来避免定义过多的数据模型类。

在JDK中,没有提供原生的Pair数据结构,也可以使用Map::Entry代替。

不过,Apache的commons-lang3包中的Pair类更为好用,下面便以Pair类进行举例说明。

封装业务方法:

    //获取最近的点和距离
    public static Pair<Point,Double> getNearestPointAndDistance(){
        //业务实现
        return Pair.of(new Point(),Double.valueOf(10));
    }

业务调用

        Pair<Point, Double> pair = getNearestPointAndDistance();
        System.out.println(pair.getLeft()+" "+pair.getRight());

Triple类可以支持三个对象一起返回

封装业务方法

    //获取最近的点和距离和方向
    public static Triple<Point,Double,String> getNearestPointAndDistanceAndAngle(){
        //业务实现
        return Triple.of(new Point(),Double.valueOf(10),"东北15度");
    }

业务调用

        Triple<Point, Double, String> triple = getNearestPointAndDistanceAndAngle();
        System.out.println(triple.getLeft()+" "+triple.getMiddle()+" "+triple.getRight());

以上调用结果

 

标签:返回,ch,return,string,HashSet,Triple,Pair,Java,主键
From: https://www.cnblogs.com/badaoliumangqizhi/p/17471476.html

相关文章

  • java——微服务——spring cloud——Nacos——Nacos认识与安装
                   Nacos开发必知Nacos开发必知官网:https://nacos.io/zh-cn/index.htmlNocas文档:https://nacos.io/zh-cn/docs/what-is-nacos.htmlNocas下载:https://github.com/alibaba/nacos/releases  说明:1.4.0以下使用的mysql驱......
  • vue解决后端返回的图片路径名和原图片路径名相同时,浏览器缓存只会加载首次缓存的图片
    方法一:让后端更改返回的路径名字。方法二:前端自己处理。在图片路径后拼接随机数或者时间戳我用的是时间戳this.$refs.rotate.src=res.data.url+"?"+newDate().getTime() :src="formData.indexUrl+'?'+newDate().getTime()"......
  • Java--进阶
    高级文本处理Locale类 //返回Java所支持的全部国家和语言的数组 Locale[]localeList=Locale.getAvailableLocales(); for(Localelocale:localeList) { System.out.println(locale.getLanguage()+"_"+locale.getCountry()); System.out.println(loc......
  • JAVA基础语法
    Day03Java基础语法1.运算符运算符:对字面量或者变量进行操作的符号表达式:用运算符把字面量或者变量连接起来符合java语法的式子就可以称为表达式。不同运算符连接的表达式体现的是不同类型的表达式例如:inta=10;intb=20;intc=a+b;+就叫做运算符a+b就叫表......
  • IDEA编译和构建JavaWeb项目时,项目中没有target目录,且out目录下classes文件下main包下
    问题如下:1.我们在添加web框架时,如图:2.在添加完框架,和配置完Tomcat我们开始运行项目,发现没有target文件和out文件下classes文件下什么都没有原因:出现这种情况,很可能是因为未加载的模块出现在了iml文件中,导致生成taget的时候出错,进而导致out文件内class文件的......
  • Failed to bind properties under 'spring.datasource.primary' to javax.sql.DataSou
     2023-06-1011:04:13.778WARN22452---[main]ConfigServletWebServerApplicationContext:Exceptionencounteredduringcontextinitialization-cancellingrefreshattempt:org.springframework.beans.factory.UnsatisfiedDependencyException:Error......
  • Failed to bind properties under 'spring.datasource.primary' to javax.sql.DataSou
     2023-06-1011:04:13.778WARN22452---[main]ConfigServletWebServerApplicationContext:Exceptionencounteredduringcontextinitialization-cancellingrefreshattempt:org.springframework.beans.factory.UnsatisfiedDependencyException:Error......
  • Failed to bind properties under 'spring.datasource.primary' to javax.sql.DataSou
    ​  2023-06-1011:04:13.778WARN22452---[main]ConfigServletWebServerApplicationContext:Exceptionencounteredduringcontextinitialization-cancellingrefreshattempt:org.springframework.beans.factory.UnsatisfiedDependencyException:......
  • Failed to bind properties under 'spring.datasource.primary' to javax.sql.DataSou
    ​  2023-06-1011:04:13.778WARN22452---[main]ConfigServletWebServerApplicationContext:Exceptionencounteredduringcontextinitialization-cancellingrefreshattempt:org.springframework.beans.factory.UnsatisfiedDependencyException:......
  • javascript简单介绍
    javaScript简介介绍:一种弱类型世界上最流行的脚本语言,其源代码不需要经过编译,而是由浏览器直接运行,控制网页的行为。表现层CSScss层叠样式表是一门标记语言,并不是编程语言,因此不能进行自定义变量,不可以引用等,就是不具备任何语法支持。前端人员提供了一种css的预处理器,提供css......