Java 将相关对象列表转换为单行
我使用的是Java 1.6 我有这门课Java 将相关对象列表转换为单行,java,Java,我使用的是Java 1.6 我有这门课 import java.util.ArrayList; import java.util.List; public class TestDrive { public TestDrive() { super(); } public static void main( String[] args ) { TestDrive testDrive = new TestDrive();
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;
}
}
}
将相同类型的首选项分组的最有效方法是什么,
换句话说,我只需要一个print语句来打印这一行
早餐鸡蛋、牛奶、运动篮、网球等等。您可以使用一个HashMap,其中键是Preferencetype,值是List
输出:
BREAKFAST [Eggs, Milk]
SPORTS [Basket, Tennis]
最有效的方法?这取决于你需要什么 没有任何具体要求的最佳方法?你可以用一个
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());
}
顺便说一下,你确实应该考虑定义自己的构造函数优先字符串类型,而不是使用SETTER的字符串代码。
< P>这是一个快速但可行的解决方案: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;
}
}
你不需要超级;如果你的课不延长其他的课。如何打印这个早餐鸡蛋,牛奶,运动篮,田纳西谢谢,我现在拿到了。您的解决方案也是正确的。Entry的import语句是什么?它应该是Map.Entry,而不是Entry。@Java\u牺牲品Entry是Map的一个内部类,如果您使用IDE,它会自动导入所有内容,例如在eclipse中,只需按Ctrl+Shift+o,所有内容都在那里。
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());
}
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;
}
}
testDrive.printPreferences(testDrive.list2Map(prefs));