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
returning a String of the class correspondent of the value given as parameter?
您是否知道如何执行该方法StringClassifyByValue(HashMap
ِExample :
this.StringClassifyByValue(map,0.5)
have to return B.
this.StringClassifyByValue(map,0.5)必须返回B.
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
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,执行查找,然后抛弃数据的“其他”表示。
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;
}
'keys'是'List'并代表A,B,C,D类。
阈值是'List'represent the thresholds。我们必须使用这个阈值来制定边界。
我按值对阈值进行排序,以按降序获取字符串,并与双阈值列表相同,以使它们按相同的顺序排列。
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最后一个元素阈值。
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}