Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何移动收藏<;双倍>;大于0到小数位的值_Java_Collections_Hashmap_Double_Decimal - Fatal编程技术网

Java 如何移动收藏<;双倍>;大于0到小数位的值

Java 如何移动收藏<;双倍>;大于0到小数位的值,java,collections,hashmap,double,decimal,Java,Collections,Hashmap,Double,Decimal,对于集合,我想将大于0的每个值移动到小数点后一位,我该如何做 如果单个单位移动到小数点后3位,十位数移动到小数点后2位,百位数移动到小数点后1位 例如: //an array like this [140, 23, 3] //should be [0.140, 0.023, 0.003] 我正在使用的脚本统计特定字符串出现的次数,但我不确定如何使用集合来实现上述操作: 公共类双重测试{ HashMap texted(字符串tex){ HashMap计数=新建HashMap(); for(St

对于
集合
,我想将大于0的每个值移动到小数点后一位,我该如何做

如果单个单位移动到小数点后3位,十位数移动到小数点后2位,百位数移动到小数点后1位

例如:

//an array like this
[140, 23, 3]

//should be
[0.140, 0.023, 0.003]
我正在使用的脚本统计特定字符串出现的次数,但我不确定如何使用
集合来实现上述操作:

公共类双重测试{
HashMap texted(字符串tex){
HashMap计数=新建HashMap();
for(String-word:tex.split(“”){//循环遍历字符串的每个单词
//split(“”)返回一个数组,在正则表达式之间包含字符串的所有部分
//如果当前单词不在地图中,请将其添加到地图中。
如果(!counts.containsKey(word))计数.put(word,(double)0);
counts.put(word,counts.get(word)+1);//将一个值添加到当前单词的计数中
}
返回计数;
}
}
公共静态void main(字符串[]args){
最终字符串text=“双赢-平局-输球”;
textSplit countWords=新textSplit();
HashMap结果=countWords.texted(文本);
Collection resultD=result.values();

对于(int i=0;i来说,更简单的方法是使用map()函数

逻辑:-用0.001对阵列的每个元素进行多重复制

public class Test2 {
 public static void main(String[] args) {
     
     List<Integer> list = Arrays.asList(140,23,3);
     Collection<Double> collection;
     collection = list.stream().map(x ->  x* .001).collect(Collectors.toCollection(ArrayList::new));
     System.out.println(collection);
 }
}
公共类Test2{
公共静态void main(字符串[]args){
List=Arrays.asList(140,23,3);
收藏;
collection=list.stream().map(x->x*.001).collect(Collectors.toCollection(ArrayList::new));
系统输出打印项次(集合);
}
}
输出


[0.14,0.023,0.003]

我不知道如何“计算特定字符串出现的次数”与将
集合中的数字移动一些小数位有关……您在这里谈论的似乎是非常不同的事情。只需将每个值乘以0.001即可。@GilbertLeBlanc我如何将
0.001
存储为
Collection
?将140乘以0.001,得到0.14。在集合中存储0.14。@GilbertLeBlanc给出了这一点数值是按字符串的计数次数存储的,您建议我将字符串的计数次数乘以
0.001
,这样行吗?
public class Test2 {
 public static void main(String[] args) {
     
     List<Integer> list = Arrays.asList(140,23,3);
     Collection<Double> collection;
     collection = list.stream().map(x ->  x* .001).collect(Collectors.toCollection(ArrayList::new));
     System.out.println(collection);
 }
}