I have a list of hashmap. One field is an Integer, and the other one is a Boolean which will tell me if that item is selected or not.
The final purpose is to change the ArrayList, unselected the old one item which was selected, and marking the new one. I made this piece of code, but I wich to know if there is a better way to do it.
VisualizationBean visualizationBean = new VisualizationBean();
ArrayList<HashMap<Boolean, Integer>> resultsPerPage = visualizationBean.getResultsPerPage();
for (Iterator iterator = resultsPerPage.iterator(); iterator.hasNext();) {
HashMap<Boolean, Integer> hashMap = (HashMap<Boolean, Integer>) iterator.next();
if(hashMap.containsKey(true)){
int beforeSelected = hashMap.get("true");
hashMap.put(false, beforeSelecte开发者_开发百科d);
}
else{
if(hashMap.get(false) == number){
hashMap.put(true, number);
}
}
}
Thanks in advance
Please don't use a List of Maps to do what you are doing.
Use BitSet
, it's designed for this exact purpose. (Depending on your requirements you may need a list of BitSets, but never a List of Maps, especially not from Boolean to anything).
Alternatively you could use a Map from Integer to Boolean (not from Boolean to Integer).
- Declare interfaces rather than concrete types, i.e. use
List
andMap
rather thanArrayList
andHashMap
"true"
is clearly wrong - needs to betrue
, as the former is a String the latter aBoolean
As for the logic of your code, it would be easier for me to comment if you explained a bit more/better what you are trying to do.
Edit: If I understand what you're trying to do right, then I suggest that you have a new class, something like SelectedItem
that has an id
and a method called isSelected()
. Then you'd call a method like getItems(boolean selected)
or getItems
on VisualisationBean
. The former would give you only those items that are selected or not selected, the latter would give you all items and you would have to iterate through them to see which one is selected and which one is not. That type of change would make it feel more object-oriented rather than handling data structures.
Edit2: Or, depending on your needs, go with the suggestion made by @Sean Patrick Floyd
Make a small helper class (In simplistic code)
class VisiblitySetting {
public static int InvalidIndex = -1;
public boolean IsSelectedNow;
public boolean WasSelectedBefore;
public int SelectionIndex;
}
ArrayList<int> wasselected = ...;
ArrayList<int> isnowselected = ...;
VisualizationBean visualizationBean = new VisualizationBean();
ArrayList<VisiblitySetting> resultsPerPage = visualizationBean.getResultsPerPage();
for (Iterator iterator = resultsPerPage.iterator(); iterator.hasNext();) {
VisiblitySetting vis = (VisiblitySetting) iterator.next();
if(vis.IsSelectedNow){
wasselected.add(vis.SelectionIndex);
vis.IsSelected = false;
}
else if(vis.WasSelectedBefore){
isnowselected.add(vis.SelectionIndex);
vis.IsSelected = false;
}
}
}
But I might have missunderstood your code. Using a hash like that doesn't make much sense.
精彩评论