I am using Java 1.6,
I have this class
import java.util.ArrayList;
import java.util.List;
public class TestDrive
{
public TestDrive()
{
super();
}
public static void main( String[] args )
{
TestDrive testDrive = new TestDrive();
List<Preference> prefs = new ArrayList<Preference>();
Preference pref1 = new Preference();
pref1.setType( "BREAKFAST" );
pref1.setCode( "Eggs" );
prefs.add( pref1 );
Preference pref2 = new Preference();
pref2.setType( "SPORTS" );
pref2.setCode( "Basket" );
prefs.add( pref2 );
Preference pref3 = new Preference();
pref3.setType( "BREAKFAST" );
pref3.setCode( "Milk" );
prefs.add( pref3 );
Preference pref4 = new Preference();
pref4.setType( "SPORTS" );
pref4.setCode( "Tennis" );
prefs.add( pref4 );
//The list may contains more and more Preference objects of different types not only SPORTS and BREAKFAST
}
public static class Preference
{
Preference()
{
super();
}
private String type;
private String code;
public void setType( String pType )
{
this.type = pType;
}
public String getType()
{
return type;
}
public void setCode( String pCode )
{
this.code = pCode;
}
public String getCode()
{
return code;
}
}
}
What is the most efficient way to group the preferences of the same type, In other word I want just a print statement to print this line.
BREAKFAST(Eggs,Milk),SPORTS(Basket,Tennis),....
You could use a HashMap
where the key
is Preference#type
and the value is List<Preference>
Map<String, List<Preference>> map =
new LinkedHashMap<String, List<TestDrive.Preference>>();
for(Preference e : prefs) {
List<Preference> list = map.get(e.getType());
if(list == null) {
list = new ArrayList<TestDrive.Preference>();
map.put(e.getType(), list);
}
list.add(e);
}
for(Entry<String, List<Preference>> l : map.entrySet()) {
System.out.println(l.getKey() + " " + l.getValue());
}
added following toString to Preference
@Override
public String toString() {
return code;
}
Output:
BREAKFAST [Eggs, Milk]
SPORTS [Basket, Tennis]
Most efficient way? It depends what you need.
Best approach without any specific requirement? You could use a
Map<String,Set<String>> preferencesByType = new HashMap<String,Set<String>>();
for (Preference p : prefs) {
Set<String> group = preferencesByType.get(p.getType());
if (group == null) {
group = new HashSet<String>();
preferencesByType.put(p.getType(), group);
}
group.add(p.getCode());
}
By the way you should really consider defining your own constructor Preference(String type, String code)
instead that using setters.
Here's a quick (but working) solution:
Map<String,List<String>> list2Map(List<Preference> preferences) {
Map<String, List<String>> preferenceMap = new HashMap<String, List<String>>();
for (Preference pref : preferences) {
String type = pref.getType();
String code = pref.getCode();
if (preferenceMap.containsKey(type)) {
preferenceMap.get(type).add(code);
} else {
List<String> codes = new LinkedList<String>();
codes.add(code);
preferenceMap.put(type, codes);
}
}
return preferenceMap;
}
void printPreferences(Map<String, List<String>> preferenceMap) {
boolean firstType = true;
for (String type : preferenceMap.keySet()) {
System.out.print((firstType ? "" : ",") + type + "(");
List<String> codes = preferenceMap.get(type);
boolean firstCode = true;
for (String code : codes) {
System.out.print((firstCode ? "" : ",") + code);
firstCode = false;
}
System.out.print(")");
firstType = false;
}
}
And then put this into your main()
method:
testDrive.printPreferences(testDrive.list2Map(prefs));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.