vkostyukov / la4j

Linear Algebra for Java
http://la4j.org
Apache License 2.0
373 stars 108 forks source link

Unordered maps cause invalid vectors to be created. #290

Closed bashok001 closed 6 years ago

bashok001 commented 6 years ago

When we use Vector.fromMap(map, size) or SparseVector.fromMap(map, size) API with maps that is not sorted by keys, the lookup on resultant vector fails.

import org.la4j.Vector;
import java.util.LinkedHashMap;
import java.util.Map;

public class Test {
    public static void main(String[] args) {
        Map<Integer, Double> values = new LinkedHashMap<>();
        values.put(5, 5.0);
        values.put(0, 0.0);

        Vector vec = Vector.fromMap(values, 6);
        System.out.println("0: " + vec.get(0));
        System.out.println("5: " + vec.get(5));
    }
}

Output:

0: 0.0
5: 0.0