Java创建自定义Map

时间:2013-08-26 07:54:47

标签: java map

我对以下代码有两个问题:

1)在这种情况下,我有什么方法可以implement Map,而不是只扩展一个接口的直接实现?我不想写一个完整的Map实现。但是看到我的实现并不关心底层Map实现的使用情况可能会很好。 2)有不良做法吗?

ExtremesMap.java

package ocr.util;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 *
 * @author student
 */
public class ExtremesMap extends HashMap<String, Integer> {
    private Set<String> smallest;
    private int smallestValue;

    private Set<String> biggest;
    private int biggestValue;

    public ExtremesMap() {
        super();
        smallest = new HashSet<>();
        smallestValue = Integer.MAX_VALUE;
        biggest = new HashSet<>();
        biggestValue = Integer.MIN_VALUE;
    }

    @Override
    public void put(String key, Integer value) {
        if (value == null) {
            throw new IllegalArgumentException("ocr.util.ExtremesMap.put: value == null");
        }
        //TODO for real performance application implement own type of Map directly
        Integer retrieveValue = super.get(key);
        if (retrieveValue != null) {
            throw new IllegalStateException("ocr.util.ExtremesMap.put: Not allowed to modify existing value: key = " + key);
        }
        else if (retrieveValue == value) {
            return;
        }
        super.put(key, value);
        if (value < smallestValue) {
            smallest = new HashSet<>();
            smallestValue = value;
        }
        else if (value == smallestValue) {
            smallest.add(key);
        }
        if (value > biggestValue) {
            biggest = new HashSet<>();
            biggestValue = value;
        }
        else if (value == biggestValue) {
            biggest.add(key);
        }
    }

    public String getSmallestString() {
        if (smallest.size() != 1) {
            throw new IllegalStateException("ocr.util.ExtremesMap.getSmallest: smallest.size() != 1: smallest.size() = " + smallest.size());
        }
        return smallest.iterator().next();
    }

    public Set<String> getSmallestSet() {
        return smallest;
    }

    public List<String> getSmallestList() {
        return Arrays.asList(getSmallestArray());
    }

    public String[] getSmallestArray() {
        return smallest.toArray(new String[smallest.size()]);
    }

    public String getBiggestString() {
        if (biggest.size() != 1) {
            throw new IllegalStateException("ocr.util.ExtremesMap.getBiggest: biggest.size() != 1: biggest.size() = " + biggest.size());
        }
        return biggest.iterator().next();
    }

    public Set<String> getBiggestSet() {
        return biggest;
    }

    public List<String> getBiggestList() {
        return Arrays.asList(getBiggestArray());
    }

    public String[] getBiggestArray() {
        return biggest.toArray(new String[biggest.size()]);
    }
}

还有一个我无法在put()中解决的错误:

方法不会覆盖或实现超类型

中的方法
put(String,Integer) in ExtremesMap cannot implement put(K,V) in Map
  return type void is not compatible with Integer
  where K,V are type-variables:
    K extends Object declared in interface Map
    V extends Object declared in interface Map

这里到底出了什么问题?

问候。

1 个答案:

答案 0 :(得分:3)

如错误所示,put signature is

 V put(K key, V value)

这意味着您的方法put应该返回Integer而不是void

public Integer put(String key, Integer value) { ... }

关于你的前两个问题,我会说扩展HashMap不是推荐的做法,因为当你覆盖put时,你正在改变Map的工作方式。你应该更好地使用组合(一个包含私有字段Map的普通类供内部使用)。

对于代码的完整代码审核,您可以在Code Review StackExchange询问。