拆分与属性相比的ArrayList

时间:2016-10-09 16:50:28

标签: java arraylist

我有History

event类有一个属性ArrayList<History>

是否可以将ArrayList<History>与事件相关的倍数 // register MainViewModel and Inject ILogger // Is this correct? Is this how you inject a service? builder.RegisterType<MainViewModel>() .As<ILogger>() .InstancePerDependency(); 分开?

谢谢

2 个答案:

答案 0 :(得分:1)

使用Java 8:

List<History> list = Arrays.asList(new History("event1"), 
                                   new History("event2"), 
                                   new History("event1"));

Map<String, List<History>> result = list.stream()
                                        .collect(Collectors.groupingBy(History::getEvent));

将生成带有事件名称的映射作为键,并将History条目列表作为值。

答案 1 :(得分:0)

您可以在事件字段中使用分组依据。

public class CreditCard {

    private String event;
    private String name;

    public CreditCard() {
        // TODO Auto-generated constructor stub
    }

    public CreditCard(String event, String name) {
        super();
        this.event = event;
        this.name = name;
    }

    public String getEvent() {
        return event;
    }

    public void setEvent(String event) {
        this.event = event;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "CreditCard [event=" + event + ", name=" + name + "]";
    }

    public static void main(String[] args) {
        List<CreditCard> list = new ArrayList<CreditCard>();
        list.add(new CreditCard("A", "John1"));
        list.add(new CreditCard("B", "John2"));
        list.add(new CreditCard("C", "John3"));

        list.add(new CreditCard("A", "mark1"));
        list.add(new CreditCard("B", "mark2"));
        list.add(new CreditCard("C", "mark3"));

        Map<String,List<CreditCard>> obj = list.stream().collect(Collectors.groupingBy(CreditCard::getEvent,Collectors.toList()));
    }
}