zoukankan      html  css  js  c++  java
  • 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);
        }

  • 相关阅读:
    layui table表格可搜索下拉框
    mysql重置密码
    纯js代码生成可搜索选择下拉列表
    ORACLE视图简单创建和使用
    zTree多条件模糊查询
    zTree模糊搜索,显示全部节点和高亮显示
    让我们来写一个v-model吧
    使用nodejs爬取静态网页数据
    vue-cli引用vant使用rem自适应
    前端以BASE64码的形式上传图片
  • 原文地址:https://www.cnblogs.com/yangfei-beijing/p/6062569.html
Copyright © 2011-2022 走看看