我可以在scala中覆盖获取map的功能

时间:2016-01-06 09:42:11

标签: scala dictionary

我使用这样的地图。

$.ajax({
    url: "<?php echo CHILD_URL; ?>/takeaway-orders.php",
    type: 'POST',
    async:false,
    data: 'uniq='+encodeURIComponent(uniq)+'&menu_id='+encodeURIComponent(menuid)+'&quan='+encodeURIComponent(quan)+'&price='+encodeURIComponent(price)+'&tillval_count='+tillval_count,
    success: function(value){


        var resid = value;

        if(tillval_count>0){

            $('.special_request_content').html('<center><img src="'+siteurl+'/images/loader.gif'+'"></center>');
            $.ajax({
                url: "<?php echo CHILD_URL; ?>/special-request.php",
                type: 'POST',
                async:false,
                data: 'id='+encodeURIComponent(resid)+'&menu_det='+encodeURIComponent(menu_det)+'&siteurl='+encodeURIComponent('<?php echo CHILD_URL; ?>')+'&temp_tillval=1&price='+encodeURIComponent(price),
                success: function(value){

                    $('.special_request_content').html(value);
                }
            });

            $('.fader, #special_request').fadeIn();

            $('#btn_skip').click(function(){
                //for the cart
                getcountItem(0);
            });  

        }
        else{

            $('#takeaway .cart_content').html('<center><img src="'+siteurl+'/images/loader.gif'+'"></center>');
            $('#takeaway .cart_content').load("<?php echo CHILD_URL.'/takeaway-cart.php';?>");

            //for the cart
            getcountItem(0);

        }


    }
});

但是,当我使用var mapList :Map[Array[String], Long] = new Map() 时,它不会返回匹配的值。即使Array [String]具有相同的元素和相同的序列。

我认为地图可以保存参考而不是数据。因此,如果引用(或指针)不相同,则说它不相同。但我想比较Array的元素而不是引用。

我有办法吗?

1 个答案:

答案 0 :(得分:1)

Map依赖于您用作键的对象的比较方法。 Array通过引用检查相等性。如果不使用Array,则使用Scala的一种不可变集合类型,例如: List,你会得到你想要的行为。简单的例子:

val l = List(1,2,3)
val a = Array(1,2,3)

val lm = Map(l -> "Hello")
val am = Map(a -> "Hello")

lm.get(List(1,2,3)) //Some(Hello)

am.get(Array(1,2,3))//None