All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alibaba.toolkit.util.collection.DefaultMapEntry Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2002-2012 Alibaba Group Holding Limited.
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.alibaba.toolkit.util.collection;

import java.util.Map;

/**
 * 

* Map.Entry的默认实现. 具有如下特征: *

*
    *
  • 支持值为null的key
  • *
  • 可以和任意Map.Entry的实现进行equals比较
  • *
  • 如果两个Map.Entry相同(e1.equals(e2) == true), 则它们的 * hashCode()也相等
  • *
* * @author Michael Zhou * @version $Id: DefaultMapEntry.java,v 1.1 2003/07/03 07:26:16 baobao Exp $ */ public class DefaultMapEntry implements Map.Entry { private final Object key; private Object value; /** * 创建一个Map.Entry. * * @param key Map.Entry的key * @param value Map.Entry的value */ public DefaultMapEntry(Object key, Object value) { this.key = key; this.value = value; } /** * 取得key. * * @return Map.Entry的key */ public Object getKey() { return key; } /** * 取得value. * * @return Map.Entry的value */ public Object getValue() { return value; } /** * 设置value的值. * * @param value 新的value值 * @return 老的value值 */ public Object setValue(Object value) { Object oldValue = this.value; this.value = value; return oldValue; } /** * 判断两个对象是否相同. * * @param o 要比较的对象 * @return 如果相同, 则返回true */ @Override public boolean equals(Object o) { if (o == null) { return false; } if (o == this) { return true; } if (!(o instanceof Map.Entry)) { return false; } Map.Entry e = (Map.Entry) o; Object k1 = getKey(); Object k2 = e.getKey(); if (k1 == k2 || k1 != null && k1.equals(k2)) { Object v1 = getValue(); Object v2 = e.getValue(); if (v1 == v2 || v1 != null && v1.equals(v2)) { return true; } } return false; } /** * 取得Map.Entry的hash值. 如果两个Map.Entry相同, * 则它们的hash值也相同. * * @return hash值 */ @Override public int hashCode() { return (key == null ? 0 : key.hashCode()) ^ (value == null ? 0 : value.hashCode()); } /** * 将Map.Entry转换成字符串. * * @return 字符串形式的Map.Entry */ @Override public String toString() { return getKey() + "=" + getValue(); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy