lambda实现集合遍历 排序

 

 

package com.mayikt;

import java.util.ArrayList;
import java.util.function.Consumer;

/**
 * @ClassName Test06
 * @Author 蚂蚁课堂余胜军 QQ644064779 www.mayikt.com
 * @Version V1.0
 **/
public class Test06 {
    public static void main(String[] args) {
        ArrayList<String> strings = new ArrayList<>();
        strings.add("mayikt");
        strings.add("meite");
        strings.add("xiaowei");
//        strings.forEach(new Consumer<String>() {
//            @Override
//            public void accept(String s) {
//                System.out.println(s);
//            }
//        });
        strings.forEach(s -> {
            System.out.println(s);
        });

    }
}

 

 

 

 


 

 

 

 


package com.mayikt;

import com.mayikt.entity.UserEntity;

import java.util.ArrayList;
import java.util.Comparator;

/**
* @ClassName Test07
* @Author 蚂蚁课堂余胜军 QQ644064779 www.mayikt.com
* @Version V1.0
**/
public class Test07 {
public static void main(String[] args) {
ArrayList<UserEntity> userLists = new ArrayList<>();
userLists.add(new UserEntity("xiaomin", 12));
userLists.add(new UserEntity("xiaoxiao", 6));
userLists.add(new UserEntity("xiaojun", 23));
userLists.sort(new Comparator<UserEntity>() {
@Override
public int compare(UserEntity o1, UserEntity o2) {
return o1.getAge() - o2.getAge();
}
});
userLists.sort((o1, o2) -> o1.getAge() - o2.getAge()
);
userLists.forEach((t) -> {
System.out.println(t.toString());
});
}
}
 

 

posted @ 2021-11-22 23:56  AngDH  阅读(84)  评论(0编辑  收藏  举报