Do maps preserve insert order?

by Adam Granger

HTML

<div class="console">
</div>

JavaScript

var foo = [];
foo[1] = "A";
foo[3] = "B";
foo[2] = "C";
foo[4] = "D";

foo['a'] = "E";
foo['c'] = "F";
foo['b'] = "G";
foo['d'] = "H";

foo[100000001] = "I";
foo[100000003] = "J";
foo[100000002] = "K";
foo[100000004] = "L";

for (var i in foo) {
    $('.console').append(foo[i] + ",");
}

LinkedHashMap = function LinkedHashMap() {
    this.list = [];
    this.map = [];
    this.listPos = [];    
    this.put = function(key, value) {
        this.listPos[key] = this.list.length;
        this.list.push(value);
        this.map[key] = value;
    };
    this.get = function(key) {
        return this.map[key];
    };
    this.remove = function(key) {
        delete this.map[key];
        this.list.splice(this.listPos[key], 1);
        delete this.listPos[key];        
    };
    this.containsKey = function(key) {
        console.log(this.map[key]);
        return this.map[key] === undefined;
    };        
    this.values = function() {
        return this.list;
    };
    this.clear = function() {
        this.list = [];
        this.map = [];
        this.listPos = [];
    };    
    return this;
}
        
        
var lhm = new LinkedHashMap();
lhm.put(1, "A");
lhm.put(3, "B");
lhm.put(2, "C");
lhm.put(4, "D");

var lhm2 = new LinkedHashMap();
lhm2.put(9, "Z");


$('.console').append("<br />===values test===<br/>");
var list = lhm.values();
for (var i = 0; i < list.length; i++) {
    $('.console').append(list[i] + ",");
}

$('.console').append("<br />===contains test==<br/>");

$('.console').append(lhm.containsKey(1)?'true':'false');
$('.console').append(lhm.containsKey(999)?'true':'false');



$('.console').append("<br />===remove test===<br/>");
lhm.remove(3);
list = lhm.values();
for (var i = 0; i < list.length; i++) {
    $('.console').append(list[i] + ",");
}

$('.console').append("<br />===clear test==<br/>");
lhm.clear();

list = lhm.values();
for (var i = 0; i < list.length; i++) {
    $('.console').append(list[i] + ",");
}

$('.console').append("<br...