热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

通过阈值列表对值列表进行分类-Classifyalistofvaluethroughalistofthresholds

Ihaveamapof<String,Double>consideredasthresholdstoclassifyalistofDoubles我有一个

I have a map of considered as thresholds to classify a list of Doubles

我有一个 的映射,被认为是对双打列表进行分类的阈值

HashMap map = new Hashmap<>();
map.put(A/B,0.7);
map.put(B/C,0.4);
map.put(C/D,0.3);
map.put(D/E,0.1);

The Doubles of the hash is used as threshold to classify a list doubles given, so I want to transform this map into a list of classes A, B, C, D

散列的双打用作阈值来对给定的列表双精度进行分类,因此我想将此映射转换为类A,B,C,D的列表

Class A : from 0.7
Class B : from 0.4 to 0.7
Class C : from 0.3 to 0.4
Class D : from 0.1 to 0.3
Class E : less than 0.1

Do you have any idea on how to perform that as a method StringClassifyByValue(HashMap map, Double value){} returning a String of the class correspondent of the value given as parameter?

您是否知道如何执行该方法StringClassifyByValue(HashMap map,Double value){}返回作为参数给出的值的类对应的String?

ِExample :

this.StringClassifyByValue(map,0.5) have to return B.

this.StringClassifyByValue(map,0.5)必须返回B.

2 个解决方案

#1


1  

Some thoughts: first of all, your data structure is not really helping with the problem you want to solve. But that is exactly what data structures exist for: to give you a helpful abstraction that allows you to efficiently solve your "most important" problem.

一些想法:首先,您的数据结构并没有真正帮助解决您想要解决的问题。但这正是数据结构的存在:为您提供有用的抽象,使您能够有效地解决“最重要”的问题。

Coming from there, I would suggest that you start by creating classes that better fit your problem statement. You could start with something like

从那里开始,我建议您首先创建更适合您的问题陈述的类。你可以从类似的东西开始

public class Interval {

   private final double lowerBoundary;
   private final double upperBoundary;

   public Interval(double lowerBoundary, upperBoundary) {
     this.lowerBoundary = ...
   } 

   public boolean contains(double value) {
     return (value >= lowerBoundary) && (value <=upperBoundary);
   }

And instead of keeping of using a Map you rather have something like List> where:

而不是保持使用Map 而不是像List >那样:

  • Pair could be a class that simply holds two values that belong together (instead of using a "generic" pair class, you could also create your own custom class that combines a Classification with an Interval).
  • Pair可以是一个只包含两个属于一起的值的类(而不是使用“泛型”对类,您也可以创建自己的自定义类,它将分类与Interval结合起来)。

  • Classification represents a "class", like A, B, C, in your example. The point is: be careful about using raw String objects for such purposes. Maybe a simple string is fine for now, but maybe you have to further enhance your logic later on - to then find that "hm, now a simple string doesn't do any more, but now I have to update a ton of places to change that".
  • 在您的示例中,分类表示“类”,如A,B,C。关键是:小心使用原始String对象用于此类目的。也许一个简单的字符串现在很好,但也许你必须在以后进一步增强你的逻辑 - 然后发现“嗯,现在一个简单的字符串不再做了,但现在我必须更新大量的地方改变那个“。

And of course: ideally, you would sort the above list by "intervals". Then finding the Classification for a specific double value is super simple:

当然:理想情况下,您可以按“间隔”对上面的列表进行排序。然后找到特定双值的分类是非常简单的:

 for (Pair combo : listOfPairs) {
   if (combo.getInterval().contains(value)) {
     return combo.getClassification(); // yeeha found one
   }
 }
 return "nothing found" ... or throw some kind of exception

Long story short: I can't tell you how to best transform your existing map into the above list of pair objects - because I don't know the big picture. It might be possible to simply not create that map initially, and directly build such a list of objects.

长话短说:我不能告诉你如何最好地将你现有的地图转换成上面的配对对象列表 - 因为我不知道大局。有可能最初不能创建该映射,并直接构建这样的对象列表。

And for the record: if that map exists as map because there are other, more important requirements ... then you have to carefully balance if you really want to keep using map+list (means "double book-keeping") or if you put all of the above logic into some service that turns your Map into a List, does the lookup and then throws away this "other" representation of your data.

并且为了记录:如果该地图作为地图存在,因为还有其他更重要的要求......那么如果你真的想继续使用地图+列表(意思是“双重保存”)或者如果你,你必须仔细平衡将所有上述逻辑放入一些服务中,将Map转换为List,执行查找,然后抛弃数据的“其他”表示。

#2


1  

private HashMap> getMinMaxThreshold(HashMap map) {

        List threholds = map.values().stream().collect(Collectors.toList());
        HashMapmap1= this.sortByValues(map);
        List keys = map1.keySet().stream().collect(Collectors.toList());

        Collections.sort(threholds);
        Collections.reverse(threholds);

        HashMap> boudaries = new HashMap<>();


        for (int i =0;i<=threholds.size();i++){

            if(i==threholds.size()){
                HashMap testmap = new HashMap<>();
                testmap.put("max",threholds.get(i-1));
                testmap.put("min",0.0);
                boudaries.put(keys.get(keys.size()-1).split("/")[1], testmap);
                System.out.println(threholds.get(i-1)+" ->"+0+" : "+keys.get(keys.size()-1).split("/")[1] );
            }

            else if (i==0){
                HashMap testmap = new HashMap<>();
                testmap.put("max",Math.exp(1000));
                testmap.put("min",threholds.get(i));
                boudaries.put(keys.get(0).split("/")[0], testmap);
                System.out.println(Math.exp(100) +" ->"+threholds.get(i)+" : "+keys.get(0).split("/")[0] );}

            else{
                HashMap testmap = new HashMap<>();
                testmap.put("max",threholds.get(i-1));
                testmap.put("min",threholds.get(i));
                boudaries.put(keys.get(i).split("/")[0], testmap);
            System.out.println(threholds.get(i-1)+" ->"+threholds.get(i)+" : "+keys.get(i).split("/")[0]);}
        }

        System.err.println(boudaries);

        return boudaries;
    }
  1. The ’keys’ is a ’List’ and represent the Classes A,B,C,D.
  2. 'keys'是'List'并代表A,B,C,D类。

  3. The thresholds is a ’List’represent the thresholds. We have to use this threshold to make the boundaries.
  4. 阈值是'List'represent the thresholds。我们必须使用这个阈值来制定边界。

  5. I sort the map of thresholds by values to get the Strings in descending order, and same with the list of Double thresholds to make them in the same order.
  6. 我按值对阈值进行排序,以按降序获取字符串,并与双阈值列表相同,以使它们按相同的顺序排列。

  7. I loop foreach element of threshold.. for the first item it's about the last boudaries so I made as boudaries the Exp(1000) and the previous element.. for the last element, it's about a boudaries with min 0 and max the last element of threshold.

    我循环foreach元素的阈值..对于第一个项目它是关于最后的boudaries所以我做了boudaries Exp(1000)和前一个元素..对于最后一个元素,它是关于一个boudaries有min 0和max最后一个元素阈值。

  8. I use split to get the first element of splited array except the last item where I use the second element of the array to match the last element. That's it

    我使用split来获取splited数组的第一个元素,除了最后一个项目,我使用数组的第二个元素来匹配最后一个元素。而已

Test :

If we have as an input this ’Map’

如果我们输入这个'地图'

{ "YELLOW_DARK/RED_LIGHT" : 0.20459770114942527 , "GREEN_DARK/GREEN_LIGHT" : 0.6226151930261519 , "GREEN_LIGHT/YELLOW_LIGHT" : 0.4632936507936508 , "YELLOW_LIGHT/YELLOW_DARK" : 0.3525246305418719 , "RED_LIGHT/RED_DARK" : 0.027777777777777776}

My code will transorm it into :

我的代码将其转换为:

2.6881171418161356E43 ->0.6226151930261519 : GREEN_DARK
0.6226151930261519 ->0.4632936507936508 : GREEN_LIGHT
0.4632936507936508 ->0.3525246305418719 : YELLOW_LIGHT
0.3525246305418719 ->0.20459770114942527 : YELLOW_DARK
0.20459770114942527 ->0.027777777777777776 : RED_LIGHT
0.027777777777777776 ->0 : RED_DARK

and returning as a HashMap>

并作为HashMap >返回

{GREEN_LIGHT={min=0.4632936507936508, max=0.6226151930261519}, YELLOW_DARK={min=0.20459770114942527, max=0.3525246305418719}, YELLOW_LIGHT={min=0.3525246305418719, max=0.4632936507936508}, RED_DARK={min=0.0, max=0.027777777777777776}, RED_LIGHT={min=0.027777777777777776, max=0.20459770114942527}, GREEN_DARK={min=0.6226151930261519, max=Infinity}}
{ "GREEN/LIGHT_YELLOW" : 0.5366379310344828 , "YELLOW_DARK/RED_LIGHT" : 0.18349195937745413 , "YELLOW_LIGHT/YELLOW_DARK" : 0.3571428571428571 , "RED_LIGHT/RED_DARK" : 0.08940809968847352}

推荐阅读
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • JavaSE笔试题-接口、抽象类、多态等问题解答
    本文解答了JavaSE笔试题中关于接口、抽象类、多态等问题。包括Math类的取整数方法、接口是否可继承、抽象类是否可实现接口、抽象类是否可继承具体类、抽象类中是否可以有静态main方法等问题。同时介绍了面向对象的特征,以及Java中实现多态的机制。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • JDK源码学习之HashTable(附带面试题)的学习笔记
    本文介绍了JDK源码学习之HashTable(附带面试题)的学习笔记,包括HashTable的定义、数据类型、与HashMap的关系和区别。文章提供了干货,并附带了其他相关主题的学习笔记。 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • javascript  – 概述在Firefox上无法正常工作
    我试图提出一些自定义大纲,以达到一些Web可访问性建议.但我不能用Firefox制作.这就是它在Chrome上的外观:而那个图标实际上是一个锚点.在Firefox上,它只概述了整个 ... [详细]
  • JVM 学习总结(三)——对象存活判定算法的两种实现
    本文介绍了垃圾收集器在回收堆内存前确定对象存活的两种算法:引用计数算法和可达性分析算法。引用计数算法通过计数器判定对象是否存活,虽然简单高效,但无法解决循环引用的问题;可达性分析算法通过判断对象是否可达来确定存活对象,是主流的Java虚拟机内存管理算法。 ... [详细]
  • 关键词:Golang, Cookie, 跟踪位置, net/http/cookiejar, package main, golang.org/x/net/publicsuffix, io/ioutil, log, net/http, net/http/cookiejar ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • 模板引擎StringTemplate的使用方法和特点
    本文介绍了模板引擎StringTemplate的使用方法和特点,包括强制Model和View的分离、Lazy-Evaluation、Recursive enable等。同时,还介绍了StringTemplate语法中的属性和普通字符的使用方法,并提供了向模板填充属性的示例代码。 ... [详细]
author-avatar
赖军菁佳瑄柏昌
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有