将相关对象列表转换为单行

时间:2014-07-17 13:33:38

标签: 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();
        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;
        }
    }
}

对同一类型的偏好进行分组的最有效方法是什么, 换句话说,我只想要一个打印语句来打印这一行。

早餐(鸡蛋,牛奶),运动(篮,网球),....

3 个答案:

答案 0 :(得分:0)

您可以使用HashMap keyPreference#type且价值为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()); }
Preference

将toString添加到@Override public String toString() { return code; }

BREAKFAST [Eggs, Milk]
SPORTS [Basket, Tennis]

输出:

{{1}}

答案 1 :(得分:0)

最有效的方式?这取决于你的需要。

没有任何特殊要求的最佳方法?你可以使用

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来定义你自己的构造函数Preference(String type, String code)

答案 2 :(得分:0)

这是一个快速(但有效)的解决方案:

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;
    }
}

然后将其放入main()方法:

testDrive.printPreferences(testDrive.list2Map(prefs));