将Arraylist拆分为多个列表,用于相同的元素(String) - JAVA

时间:2016-04-20 08:45:58

标签: java arraylist split

我有一个类似对象的ArrayList。我必须根据类似的元素(String)将列表拆分成多个列表

这是一个例子,ArrayList有内部对象,而不是(String,Integer)

以下是示例列表:

ArrayList{ {'Milk', 12}, {'Apple', 60}, {'Coffe', 87}, {'Pear', 39}, {'Coffe', 87}, 
           {'Milk', 99}, {'Apple', 43}, {'Milk', 20} }

结果应该是:

清单1:

ArrayList{ {'Milk', 12}, {'Milk', 99}, {'Milk', 20} }

List2:

ArrayList{ {'Apple', 60}, {'Apple', 43} }

List3:

ArrayList{ {'Coffe', 87}, {'Coffe', 87} }

List4:

ArrayList{ {'Pear', 39} }

有人会给我一个更简单的方法吗?

2 个答案:

答案 0 :(得分:2)

使用Multimaps

中的com.google.common.collect

例如

List<Map.Entry<String,Integer>>entries=...
Map<String,Collection<Map.Entry<String,Integer>>>map=Multimaps.index(entries,new GroupFunction()).asMap();

class GroupFunction implements Function<Map.Entry<String,Integer>, String> {
    @Override
    public String apply(Map.Entry<String,Integer>input) {
        return input.getKey();
    }
}

答案 1 :(得分:1)

以下是根据对象中的字符串分割arraylist的代码 -

假设Sampleobject.class为 -

 public class SampleObject implements Comparable<SampleObject> {

    String name;
    String value;

    SampleObject(String name, String value) {
        setName(name);
        setValue(value);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    @Override
    public int compareTo(SampleObject o) {
        return (this.name).compareTo(o.name);
    }
}

Main Class SplitArray.class -

    public class SplitArray {

    public static void main(String[] args) throws IOException, InstantiationException, IllegalAccessException {

        List<SampleObject> list = new ArrayList<SampleObject>();

        list.add(new SampleObject("Milk", "12"));
        list.add(new SampleObject("Apple", "60"));
        list.add(new SampleObject("Coffe", "87"));
        list.add(new SampleObject("Pear", "39"));
        list.add(new SampleObject("Coffe", "87"));
        list.add(new SampleObject("Milk", "99"));
        list.add(new SampleObject("Apple", "43"));
        list.add(new SampleObject("Milk", "20"));
        list.add(new SampleObject("Milk", "20"));

        System.out.println(splitArrayByName(list));
    }

    private static List<List<SampleObject>> splitArrayByName(List<SampleObject> list) {

        List<List<SampleObject>> masterList = new ArrayList<List<SampleObject>>();
        List<SampleObject> uniqueList = new ArrayList<SampleObject>();

        Collections.sort(list);
        uniqueList = removeDuplicates(list);
        int i = 0;
        int j = 0;
        int childListIndex = 0;

        for (SampleObject element : uniqueList) {
            for (i = j; i < list.size(); i++) {

                if (element.compareTo(list.get(i)) == 0) {
                    if (masterList.size() == 0) {
                        masterList.add(new ArrayList<SampleObject>());
                    }
                    if (masterList.size() >= 1) {
                        masterList.get(childListIndex).add(list.get(i));
                    }
                    j++;
                } else {
                    masterList.add(new ArrayList<SampleObject>());
                    childListIndex++;
                    break;
                }
            }
        }
        return masterList;
    }

    private static List<SampleObject> removeDuplicates(List<SampleObject> list) {

        List<SampleObject> uniqueList = new ArrayList<SampleObject>();
        HashSet<String> set = new HashSet<String>();

        for (SampleObject sampleObject : list) {
            if (!set.contains(sampleObject.getName())) {
                uniqueList.add(sampleObject);
                set.add(sampleObject.getName());
            }
        }
        return uniqueList;
    }
}