• java 8


    java 8 发布已经有一段时间了,然而很多新特性被拒之门外,让人敬而生畏,但是,时代在进步,技术在发展,要追随时代的脚步就要跟随新的潮流。总结下java 8 中常用的小功能点,学如逆水行舟,不进则退~

    1. 元素为对象的集合排序

    1>.实体类实现Comparable接口,在实体类中重写comparaTo方法

    public class Cat implements Comparable<Cat> {
    private int age;
    private String name;

    public Cat(int age, String name) {
    this.age = age;
    this.name = name;
    }

    public int getAge() {
    return age;
    }

    public void setAge(int age) {
    this.age = age;
    }
    ......
    public int compareTo(Cat o) {
    return this.getAge() - o.getAge();
    }
    ......
    }

    Collections.sort(listCat1);

    2>.新建排序类,实现Comparator接口,重写compare接口

    import java.util.Comparator;
    public class PersonComparator implements Comparator<Person> { public int compare(Person o1, Person o2) {
    return o1.getAge() - o2.getAge();
    }
    }

    Collections.sort(listPerson, ascComparator);

    3>.lambda表达式排序

    List<Apple> inventory = Arrays.asList(new Apple(80,"green"),new Apple(155, "green"), new Apple(120, "red"));
    inventory.sort((Apple a1,Apple a2) -> a2.getWeight().compareTo(a1.getWeight()));

    2. 集合遍历

    inventory.forEach(n -> System.out.println("@inventory@" + n.getWeight()));

    3. 集合过滤

    List<Apple> apples = inventory.stream().filter((Apple a) -> a.getColor().equals("green")).collect(Collectors.toList());

    4. 集合分组

     public static List<Transaction> transactions = Arrays.asList( new Transaction(Currency.EUR, 1500.0),
                                                                      new Transaction(Currency.USD, 2300.0),
                                                                      new Transaction(Currency.GBP, 9900.0),
                                                                      new Transaction(Currency.EUR, 1100.0),
                                                                      new Transaction(Currency.JPY, 7800.0),
                                                                      new Transaction(Currency.CHF, 6700.0),
                                                                      new Transaction(Currency.EUR, 5600.0),
                                                                      new Transaction(Currency.USD, 4500.0),
                                                                      new Transaction(Currency.CHF, 3400.0),
                                                                      new Transaction(Currency.GBP, 3200.0),
                                                                      new Transaction(Currency.USD, 4600.0),
                                                                      new Transaction(Currency.JPY, 5700.0),
                                                                      new Transaction(Currency.EUR, 6800.0) );

    private static void groupImperatively() {
            Map<Currency, List<Transaction>> transactionsByCurrencies = new HashMap<>();
            for (Transaction transaction : transactions) {
                Currency currency = transaction.getCurrency();
                List<Transaction> transactionsForCurrency = transactionsByCurrencies.get(currency);
                if (transactionsForCurrency == null) {
                        transactionsForCurrency = new ArrayList<>();
                    transactionsByCurrencies.put(currency, transactionsForCurrency);
                }
                transactionsForCurrency.add(transaction);
            }

            System.out.println(transactionsByCurrencies);
        }

    private static void groupFunctionally() {
            Map<Currency, List<Transaction>> transactionsByCurrencies = transactions.stream().collect(groupingBy(Transaction::getCurrency));
            System.out.println(transactionsByCurrencies);
        }

  • 相关阅读:
    MFC
    驱动学习
    Ubuntu下为Apache简单配置SSL的方法(HTTPS的实现)
    在linux下helloworld的C程序
    swift安装,linux
    gcc,cc,g++,CC的区别
    ldconfig与 /etc/ld.so.conf
    ubuntu16.04,mysql5.7重启不成功。Restarting mysql (via systemctl): mysql.serviceJob for mysql.service failed because the control process exited with error code. See "systemctl status mysql.service"
    linux .o,.a,.so文件
    zipgateway-2-61-0的安装
  • 原文地址:https://www.cnblogs.com/yangfei-beijing/p/6062569.html
Copyright © 2020-2023  润新知