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);
        }

  • 相关阅读:
    异常-ERROR yarn.ApplicationMaster: User class threw exception: java.sql.SQLException: Communications link failure
    异常-Data truncation: Truncated incorrect DOUBLE value: '-9370.3530-'
    算法中的时间复杂度分析
    MySQL开启binlog无法启动ct 10 21:27:31 postfix/pickup[4801]: warning: 6BD991A0039: message has been queue
    【异常】ERROR main:com.cloudera.enterprise.dbutil.SqlFileRunner: Exception while executing ddl scripts. com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'ROLES' already exists
    CDH5.16.1的agent启动报错:ERROR Error, CM server guid updated, expected d9bcadb4-f983-41b8-a667-66760f47bc91, received a67f5efa-8473-4f6a-94d6-231d1f432ef0
    MySQL无法启动:ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
    [Ubuntu] Ubuntu13.04, the desktop freezed after login
    [ubuntu] ubuntu13.04安装rabbitcvs管理svn
    [javascript] ajaxfileupload.js 跨域上传文件
  • 原文地址:https://www.cnblogs.com/yangfei-beijing/p/6062569.html
Copyright © 2011-2022 走看看