Java creating a custom Map -
i have 2 questions regarding following code:
1) there way can implement map
in case, rather extend 1 direct implementation of interface? not wish write whole map implementation though. nice seeing implementation not care underlying map implementation being used. 2) there bad practices?
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 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 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()]); } }
also there 1 bug unable resolve in put()
:
method not override or implement method supertype
put(string,integer) in extremesmap cannot implement put(k,v) in map return type void not compatible integer k,v type-variables: k extends object declared in interface map v extends object declared in interface map
what goes wrong here?
regards.
as error says, put
signature is:
v put(k key, v value)
that means method put
should return integer
, not void
:
public integer put(string key, integer value) { ... }
concerning first 2 questions, not recommended practice extend hashmap
, because changing way map working when override put
. should better use composition instead (a normal class contains private field map
internal use).
for whole code review of code, ask @ code review stackexchange.
Comments
Post a Comment