How to reorder a List?
I have created a List by
List<NameValuePair> infoMap = new ArrayList<开发者_如何学Go;NameValuePair>();
URLEncodedUtils.parse(infoMap, new Scanner(Info), encoding);
But the infoMap contains a lot of information of which i only need a couple of values.
How can i create a new List which contains only the needed keys and its values?
Since you need to create a new list, and are very silent about the criteria of deciding whether an item should be the part of your result list or not, the simplest thing i can think of is
private List<NameValuePair> getUsefulList(final List<NameValuePair> baseList)
{
final List<NameValuePair> list = new ArrayList<NameValuePair>();
for (final NameValuePair item : baseList)
{
if (isUseful(item.getName()))
list.add(item);
}
return list;
}
private boolean isUseful(String name)
{
return [... decision based on your criteria ...];
}
with these methods you can create your new list:
final List<NameValuePair> resultList = getUsefulList(baseList);
boolean isNeeded(Strnig name){
//check here if you need this key
return true;
}
List<NameValuePair> infoMap = new ArrayList<NameValuePair>();
...
List<NameValuePair> realInfoMap = new ArrayList<NameValuePair>();
for(NameValuePair val:infoMap){
if(isNeeded(val.getName())){
realInfoMap.add(val);
}
}
URLEncodedUtils.parse(realInfoMap, new Scanner(Info), encoding);
精彩评论