Can any one explain this strange behavior of java HashMap? -
Can any one explain this strange behavior of java HashMap? -
i have used hashmap in java lot has never encountered behavior. have types, item , itemgroup. defined shown in next codes snippets.
public class item { string id; float total; } public class itemgroup { string keyword; int frequency; list<item> items; }
so itemgroup consists of 0..* items. these items have keyword in mutual , keyword appears in scheme frequency. fun part, have next method given list of items creates list of groups.
public static itemgroup[] creategroups(item[] items){ hashmap<string, itemgroup> groups = new hashmap<string, itemgroup>(); string[] words; (int i=0; i<items.length; i++){ words = items[i].getid().split(regex); // process keywords (int j=0; j<words.length; j++){ if (words[j].isempty()) break; itemgroup grouping = groups.get(words[j]); if (group != null){ group.incrementfrequency(); group.getitems().add(items[i]); }else { grouping = economfactory.einstance.createitemgroup(); group.setkeyword(words[j]); group.incrementfrequency(); group.getitems().add(items[i]); groups.put(words[j], group); } } } homecoming groups.values().toarray(new itemgroup[0]); }
the part gets unusual when adding item itemgroup (the line group.getitems().add(items[i]);). during rehashing grouping loses items in unusual way. using debugging can see grouping contains item after operation latter on, e.g. when returning value of method, groups has lost items.
i tried this:
public static itemgroup[] creategroups(item[] items){ hashmap<string, itemgroup> groups = new hashmap<string, itemgroup>(); string[] words; (int i=0; i<items.length; i++){ words = items[i].getid().split(regex); // create new item based on current 1 in list item item = economfactory.einstance.createitem(); item.setid(items[i].getid()); item.settotal(items[i].gettotal()); // process key words (int j=0; j<words.length; j++){ if (words[j].isempty()) break; itemgroup grouping = groups.get(words[j]); if (group != null){ group.incrementfrequency(); group.getitems().add(item); }else { grouping = economfactory.einstance.createitemgroup(); group.setkeyword(words[j]); group.incrementfrequency(); group.getitems().add(item); groups.put(words[j], group); } } } homecoming groups.values().toarray(new itemgroup[0]); }
but got same result. next solution, however, works fine.
public static itemgroup[] creategroups(item[] items){ hashmap<string, itemgroup> groups = new hashmap<string, itemgroup>(); string[] words; (int i=0; i<items.length; i++){ words = items[i].getid().split(regex); // process key words (int j=0; j<words.length; j++){ if (words[j].isempty()) break; // create new item based on current 1 in list item item = economfactory.einstance.createitem(); item.setid(items[i].getid()); item.settotal(items[i].gettotal()); itemgroup grouping = groups.get(words[j]); if (group != null){ group.incrementfrequency(); group.getitems().add(item); }else { grouping = economfactory.einstance.createitemgroup(); group.setkeyword(words[j]); group.incrementfrequency(); group.getitems().add(item); groups.put(words[j], group); } } } homecoming groups.values().toarray(new itemgroup[0]); }
the method economfactory.einstance.createitemgroup() implemented follow:
public itemgroup createitemgroup() { itemgroupimpl itemgroup = new itemgroupimpl(); homecoming itemgroup; }
where itemgroupimpl implementation of itemgroup, e.i. subclasses itemgroup. because utilize emf (eclipse modeling framework).
can please explain behavior (why itemgroup objects lose items)?
here codes itemgroup , itemgroupimpl. same way looks codes item , itemimpl.
public interface itemgroup extends eobject { string getkeyword(); void setkeyword(string value); int getfrequency(); void setfrequency(int value); elist<item> getitems(); void incrementfrequency(); } public class itemgroupimpl extends eobjectimpl implements itemgroup { protected static final string keyword_edefault = null; protected string keyword = keyword_edefault; protected static final int frequency_edefault = 0; protected int frequency = frequency_edefault; protected elist<item> items; protected itemgroupimpl() { super(); } @override protected eclass estaticclass() { homecoming econompackage.literals.item_group; } public string getkeyword() { homecoming keyword; } public void setkeyword(string newkeyword) { string oldkeyword = keyword; keyword = newkeyword; if (enotificationrequired()) enotify(new enotificationimpl(this, notification.set, econompackage.item_group__keyword, oldkeyword, keyword)); } public int getfrequency() { homecoming frequency; } public void setfrequency(int newfrequency) { int oldfrequency = frequency; frequency = newfrequency; if (enotificationrequired()) enotify(new enotificationimpl(this, notification.set, econompackage.item_group__frequency, oldfrequency, frequency)); } public elist<item> getitems() { if (items == null) { items = new eobjectcontainmentelist<item>(item.class, this, econompackage.item_group__items); } homecoming items; } public void incrementfrequency() { this.frequency = getfrequency() + 1; } @override public notificationchain einverseremove(internaleobject otherend, int featureid, notificationchain msgs) { switch (featureid) { case econompackage.item_group__items: homecoming ((internalelist<?>)getitems()).basicremove(otherend, msgs); } homecoming super.einverseremove(otherend, featureid, msgs); } @override public object eget(int featureid, boolean resolve, boolean coretype) { switch (featureid) { case econompackage.item_group__keyword: homecoming getkeyword(); case econompackage.item_group__frequency: homecoming getfrequency(); case econompackage.item_group__items: homecoming getitems(); } homecoming super.eget(featureid, resolve, coretype); } @suppresswarnings("unchecked") @override public void eset(int featureid, object newvalue) { switch (featureid) { case econompackage.item_group__keyword: setkeyword((string)newvalue); return; case econompackage.item_group__frequency: setfrequency((integer)newvalue); return; case econompackage.item_group__items: getitems().clear(); getitems().addall((collection<? extends item>)newvalue); return; } super.eset(featureid, newvalue); } @override public void eunset(int featureid) { switch (featureid) { case econompackage.item_group__keyword: setkeyword(keyword_edefault); return; case econompackage.item_group__frequency: setfrequency(frequency_edefault); return; case econompackage.item_group__items: getitems().clear(); return; } super.eunset(featureid); } @override public boolean eisset(int featureid) { switch (featureid) { case econompackage.item_group__keyword: homecoming keyword_edefault == null ? keyword != null : !keyword_edefault.equals(keyword); case econompackage.item_group__frequency: homecoming frequency != frequency_edefault; case econompackage.item_group__items: homecoming items != null && !items.isempty(); } homecoming super.eisset(featureid); } @override public string tostring() { if (eisproxy()) homecoming super.tostring(); stringbuffer result = new stringbuffer(); result.append("(keyword: "); result.append(keyword); result.append(", frequency: "); result.append(frequency); result.append(')'); homecoming result.tostring(); } }
in sec set of code, intialized?
public static itemgroup[] creategroups(item[] items){ hashmap<string, itemgroup> groups = new hashmap<string, itemgroup>(); string[] words; // create new item based on current 1 in list item item = economfactory.einstance.createitem(); item.setid(items[i].getid()); item.settotal(items[i].gettotal());
the variable has not been initialized in context, value (since not blowing up, assume have global "i" somehwere else in code, , starting method value instaed of 0.
in code working have initialized in method before access :
public static itemgroup[] creategroups(item[] items){ hashmap<string, itemgroup> groups = new hashmap<string, itemgroup>(); string[] words; (int i=0; i<items.length; i++){ words = items[i].getid().split(regex); // process key words (int j=0; j<words.length; j++){ if (words[j].isempty()) break;
so, guess why seeing behavior seeing... initialize variables before accessing them.
java hashmap
Comments
Post a Comment