java 8

时间:2023-03-09 17:15:21
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);
    }