热门标签 | 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}

推荐阅读
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • JDK源码学习之HashTable(附带面试题)的学习笔记
    本文介绍了JDK源码学习之HashTable(附带面试题)的学习笔记,包括HashTable的定义、数据类型、与HashMap的关系和区别。文章提供了干货,并附带了其他相关主题的学习笔记。 ... [详细]
  • 缓存这个东西就是为了提高运行速度的,由于缓存是在寸土寸金的内存里面,不是在硬盘里面,所以容量是很有限的。LRU这个算法就是把最近一次使用时间离现在时间最远的数据删除掉。先说说List:每 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • 本文介绍了在go语言中利用(*interface{})(nil)传递参数类型的原理及应用。通过分析Martini框架中的injector类型的声明,解释了values映射表的作用以及parent Injector的含义。同时,讨论了该技术在实际开发中的应用场景。 ... [详细]
  • 深入理解Java虚拟机的并发编程与性能优化
    本文主要介绍了Java内存模型与线程的相关概念,探讨了并发编程在服务端应用中的重要性。同时,介绍了Java语言和虚拟机提供的工具,帮助开发人员处理并发方面的问题,提高程序的并发能力和性能优化。文章指出,充分利用计算机处理器的能力和协调线程之间的并发操作是提高服务端程序性能的关键。 ... [详细]
  • Java集合详解5:深入理解LinkedHashMap和LRU缓存
    Java集合详解5:深入理解LinkedHashMap和LRU缓存今天我们来深入探索一下LinkedHashMap的底层原理,并且使用linkedhashmap来实现LRU缓存。具体代码在我的 ... [详细]
  • 我有3个来自RESEARCHS的映射值,指定要使用参考数据集填充的行中的范围。该研究 ... [详细]
  • HashTable与ConcurrentHashMap均可实现HashMap的功能,对外提供了键值对存储的数据结构。但是在内部结构及实现上有何区别,性能上的差异到底在哪里又是如何导致的 ... [详细]
  • 类Hashtable<K,V>所有已实现的接口:Serializable,Cloneable,Map<K,V>此类实现一个哈希表,该哈希表将键映 ... [详细]
author-avatar
你送的指环_526
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有