简体   繁体   English

如何在HashMap中创建Farey序列?

[英]How can I create a Farey Sequence in a HashMap?

I am trying to create a HashMap for a Farey sequence of degree 6 where the denominator is the key and the fraction is the value. 我正在尝试为度数为6的Farey序列创建一个HashMap ,其中分母是键,而分数是值。 My code is: 我的代码是:

public class FareySequence {
    public static void main(String[] args){ 
        Map<Double, Double> map = new HashMap<Double, Double>();

        for(double n = 1.0; n < 6.0; n++){
            for(double d = 6.0; d > n; d--){
                double frac = n / d;
                if(frac < 1){
                    if(!map.containsValue(frac)){
                        map.put(d, frac);
                    }
                }
            }
        }
        System.out.print(map);
    }
}

However, when I run it, I get: 但是,当我运行它时,我得到:

{
      4.0=0.75,
      2.0=0.5,
      5.0=0.8,
      6.0=0.8333333333333334,
      3.0=0.6666666666666666
}

What am I doing wrong? 我究竟做错了什么?

the HashMap has your algorithm of the switching, if you want keep sequence to put in collection, you need to use TreeMap() HashMap有您的切换算法,如果您要保持序列放入集合中,则需要使用TreeMap()

That print in sequence to put in: 该打印顺序放入:

{2.0=0.5, 3.0=0.6666666666666666, 4.0=0.75, 5.0=0.8, 6.0=0.8333333333333334} {2.0 = 0.5,3.0 = 0.6666666666666666,4.0 = 0.75,5.0 = 0.8,6.0 = 0.8333333333333334}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM