如何从我的数组列表中返回最近加入的成员?

时间:2019-11-06 18:09:55

标签: java loops iterator

我正在尝试找到加入健身俱乐部我的人员列表的最新成员。我的思考过程是找到最高的年份,然后找到最高的月份,但是我在执行它时遇到了麻烦。

这里是俱乐部的成员:

club.join(new Membership("Ellen", 2, 2016, 115.00));
        club.join(new Membership("Eric", 1, 2017, 75.00));
        club.join(new Membership("Ben", 3, 2015, 35.50));
        club.join(new Membership("Matt", 12, 2014, 42.00));
        club.join(new Membership("Luke", 5, 2010, 145.00));
        club.join(new Membership("Jon", 6, 2017, 65.00));
        club.join(new Membership("Ryan", 7, 2016, 0.00));
        club.join(new Membership("Alan", 8, 2015, 195.00));
        club.join(new Membership("Jaden", 9, 2015, 15.00));
        club.join(new Membership("Dan P", 10, 2011, 25.00));
        club.join(new Membership("Jack", 2, 2013, 80.75));
        club.join(new Membership("Mrs. R", 1, 2014, 125.00));
        club.join(new Membership("Dan S", 1, 2015, 45.00));
        club.join(new Membership("Caleb", 10, 2016, 0.00));
        club.join(new Membership("Jacob", 5, 2017, 10.00));
        club.join(new Membership("Anny", 7, 2016, 110.00));
        club.join(new Membership("Wendy", 7, 2015, 85.00));
        club.join(new Membership("Natalie", 8, 2014, 0.00));
        club.join(new Membership("Paul", 9, 2016, 20.00));
        club.join(new Membership("Zuck", 10, 2017, 55.00));

这是我找到最近方法的代码:

int tempMonth = 13;
        int tempYear = 3000;
        ArrayList<Membership> ppl = new ArrayList<Membership>();
        Iterator<Membership> itr = members.iterator();
        while(itr.hasNext())
        {
            Membership item = itr.next();
            if(members.size() == 0)
            {
                return null;
            }
            else
            {
                if(tempYear > item.getYear())
                {
                    if(tempMonth > item.getMonth())
                    {
                        ppl.add(item);
                        itr.remove();
                    }
                }
            }
        }
        return ppl;

1 个答案:

答案 0 :(得分:1)

您可以使用@Component({ template: ` <div *ngIf="todos$ | async as todos"> <div *ngFor="let todo of todos"> {{todo.name}} </div> </div> ` }) export class TodosComponent implements OnInit, OnDestroy { todos$: Observable<Todo[]>; constructor(private store: Store<State>) {} ngOnInit() { this.todos$ = this.store .pipe(select(selectTodos)); } ngOnDestroy(): void { } } 和自定义Collections.maxComparator的列表进行排序

Membership

您也可以使用Collections.max(members, Comparator.comparing(Membership::getYear) .thenComparing(Membership::getMonth));

stream