如何将大于 0 的 Collection<Double> 值移动到小数位

How to move Collection<Double> values greater than 0 to decimal places

我想将每个大于 0 的值移动到 Collection<Double> 的小数点后一位,我该怎么做?

单个单位移动小数点后3位,十位移动小数点后2位,百位移动小数点后1位。

例如:

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

//should be
[0.140, 0.023, 0.003]

我使用的脚本计算特定字符串出现的次数,但我不确定如何使用 Collection<Double>:

来处理上述问题
public class doubleTest {

HashMap<String, Double> texted(String tex) {
        

        HashMap<String, Double> counts = new HashMap<String, Double>();

        for (String word : tex.split(" ")) { // loops through each word of the string
            // text.split(" ") returns an array, with all the parts of the string between your regexes
            // if current word is not already in the map, add it to the map.

            if (!counts.containsKey(word)) counts.put(word, (double) 0);

            counts.put(word,counts.get(word) + 1); // adds one to the count of the current word

        }
return counts;
    }

}

    public static void main(String[] args) {
        
        final   String text = "Win Win Win Win Draw Draw Loss Loss";
        
        textSplit countWords = new textSplit();
        
        HashMap<String, Double> result = countWords.texted(text);
        

       Collection<Double> resultD = result.values();

      for(int i = 0; i<resultD.size();i++){
            double[] value = new double[]{i*0.001};
Collection<Double> valuet = Arrays.stream(value).boxed().collect(Collectors.toCollection(ArrayList::new));

        System.out.println(valuet);

//output
//[0.0]
//[0.001]
//[0.002]

     
      
  
                   }


        //System.out.println(resultD);
        
        
    }
    
}

更简单的方法是使用 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);
 }
}

输出

[0.14, 0.023, 0.003]