0

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. 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?

null
  • 5,207
  • 1
  • 19
  • 35
genghiskhan
  • 1,095
  • 12
  • 21

1 Answers1

0

the HashMap has your algorithm of the switching, if you want keep sequence to put in collection, you need to use 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}

Marcelo Ferreira
  • 428
  • 1
  • 5
  • 20