简体   繁体   中英

Java Generic types and equals values return by generic methods?

I have following code:

public class SimpleHashMap<K, V> extends AbstractMap<K, V>{

static final int SIZE = 997;

@SuppressWarnings("unchecked")
LinkedList<MapEntry<K, V>>[] buckets =
        new LinkedList[SIZE];

public V put(K key, V value)
{
    V oldValue = null;
    int index = Math.abs(key.hashCode()) % SIZE;
    if(buckets[index] == null)
        buckets[index] = new LinkedList<MapEntry<K, V>>();
    LinkedList<MapEntry<K, V>> bucket = buckets[index];
    MapEntry<K, V> pair = new MapEntry<K, V>(key, value);
    boolean found = false;
    ListIterator<MapEntry<K, V>> iter = bucket.listIterator();
    int probes = 0;
    while(iter.hasNext())
    {
        MapEntry<K, V> iPair = iter.next();
        probes++;
            if(iPair.getKey().equals(key)){
                oldValue = iPair.getValue();
                iter.set(pair); //zastapienie starego nowym
                found = true;
                System.out.println("Colision at: " + iPair + " : " + probes + (probes == 1 ? " probe " : " probes ") 
                                        + "needed"); 
                break;
            }
    }

    if(!found)
        buckets[index].add(pair);
    return oldValue;
}

    public V remove(Object o)
{
    V removed = null;
    if(this.get(o) != null)
    {
        int index = Math.abs(o.hashCode()) % SIZE;
            for(MapEntry<K, V> pair : buckets[index])
            {
                if(pair.getKey().equals(o))
                {
                    removed = pair.getValue();
                    buckets[index].remove(buckets[index].indexOf(pair));
                    break;
                }
            }
    }
    return removed;
}

@Override
public Set<java.util.Map.Entry<K, V>> entrySet() {
    Set<Map.Entry<K, V>> set = new HashSet<Map.Entry<K, V>>();
    for(LinkedList<MapEntry<K, V>> bucket : buckets){
        if(bucket == null) continue;
        for(MapEntry<K, V> mpair : bucket)
            set.add(mpair);
    }
    return set;
   }
}

I'm interestring in it's method:

    public V remove(Object o)
{
    V removed = null;
    if(this.get(o) != null)
    {
        int index = Math.abs(o.hashCode()) % SIZE;
            for(MapEntry<K, V> pair : buckets[index])
            {
                if(pair.getKey().equals(o))
                {
                    removed = pair.getValue();
                    buckets[index].remove(buckets[index].indexOf(pair));
                    break;
                }
            }
    }
    return removed;
}

When we create instance of above class parametrized in this way:

SimpleHashMap<String, String> m = 
            new SimpleHashMap<String, String>();

add some elements by putAll method...;

Why I can't do:

if(this.get(o).equals(o))

instead of:

if(this.get(o) != null)

I know of errasing types on compile level, but if above .equals means that we compare two Object types, so we compare address, not value. OK, it is logical. But what we compare here:

if(pair.getKey().equals(o))

It looks like we comparing two String types. Now I'm confused, could someone explain to me how exactly this works in this cases? Sorry for my english. I hope someone help me with this. Thanks a lot.

remove(Object o)的参数是一个,因此if(pair.getKey().equals(o))将配对的键与键参数进行比较(将苹果与苹果进行比较),但是this.get(o)返回一个value ,因此if(this.get(o).equals(o))将值与键进行比较,这是没有意义的(将苹果与橙子进行比较)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM