我有一组数据,在绘制之前要重新组织。
数据由一堆重复的数字组成,从这些数字中您可以计算出频率。然后,在另一个功能中,我尝试以升序排列数字来重新组织转换后的数据(重新组织货币)。
Java有许多用于此的工具,但由于键是将数字视为字符串(我知道这很奇怪,但是我无法更改...),因此我一直未能成功。
此函数将所有数据放入必须处理的原始数据列表中。它来自SQL查询的“有序ASC”。
public Map<String, Integer> getDurations(){
EntityManager em = getEntityManager();
try {
System.out.println("Building query");
//Query query = em.createNativeQuery("Data.prices", String.class);
Query spm = em.createQuery("SELECT d.length FROM Data d ORDER BY d.length ASC");
List<String> durationList = new ArrayList<>();
//Load output to a list
for (Object item : spm.getResultList()){
durationList.add(item+"");
}
//Find the frecuency for later dynamically express the data into the chart
System.out.println(durationList.toString());
return findFrecuency(durationList);
} finally {
em.close();
}
}
但是一旦处理完毕...密钥是列表中重复的前一个数字,值是重复次数->频率。
private Map<String, Integer> findFrecuency(List<String> data){
Map<String,Integer> frecuencyTable = new HashMap<>();
for(String d : data){
if(!frecuencyTable.containsKey(d)){
frecuencyTable.put(d,1);
}else{
frecuencyTable.put(d, frecuencyTable.get(d)+1);
}
}
//Reorganize the data before export
frecuencyTable = frecuencyTable
.entrySet()
.stream()
.sorted(comparingByKey())
.collect(toMap(e -> e.getKey(), e -> e.getValue(), (e1, e2) -> e2, LinkedHashMap::new));
System.out.println("Resulting frecuency is: " + frecuencyTable);
return frecuencyTable;
}
我已经尝试了这个很酷的功能
frecuencyTable = frecuencyTable
.entrySet()
.stream()
.sorted(comparingByKey())
.collect(toMap(e -> e.getKey(), e -> e.getValue(), (e1, e2) -> e2, LinkedHashMap::new));
但是最终数据看起来像这样...
Resulting frecuency is: {100=12, 101=7, 102=11, 103=9, 104=6, 105=6, 106=6, 107=10, 108=5, 109=7, 110=9, 111=7, 112=13, 113=8, 114=10, 115=7, 116=4, 117=5, 118=8, 119=6, 120=9, 121=8, 122=11, 123=7, 124=4, 125=7, 126=9, 127=5, 128=7, 129=7, 130=6, 131=5, 132=8, 133=5, 134=5, 135=10, 136=9, 137=9, 138=6, 139=11, 140=5, 141=7, 142=7, 143=7, 144=8, 145=7, 146=5, 147=9, 148=7, 149=6, 150=8, 151=7, 152=9, 153=9, 154=7, 155=6, 156=4, 157=6, 158=6, 159=6, 160=6, 161=9, 162=5, 163=7, 164=4, 165=5, 166=5, 167=7, 168=4, 169=6, 170=4, 171=8, 172=8, 173=7, 174=6, 175=6, 176=10, 177=6, 178=10, 179=13, 180=7, 181=10, 182=6, 183=5, 184=8, 185=10, 46=5, 47=7, 48=11, 49=5, 50=9, 51=7, 52=7, 53=9, 54=6, 55=2, 56=5, 57=7, 58=7, 59=9, 60=8, 61=10, 62=6, 63=9, 64=9, 65=7, 66=2, 67=8, 68=5, 69=6, 70=7, 71=7, 72=4, 73=12, 74=12, 75=10, 76=7, 77=6, 78=6, 79=6, 80=10, 81=3, 82=7, 83=6, 84=13, 85=17, 86=5, 87=6, 88=4, 89=6, 90=5, 91=6, 92=11, 93=8, 94=4, 95=2, 96=2, 97=4, 98=8, 99=8}
在键185之后,转到键46,我是不知道如何解决。
我的目标是建立一个升序的键顺序,但是键是一个字符串。