NOTE

Java 8 New Features

Java 8 features and a groupingBy example that converts a List into Map<String, List>.

JavaCreated Updated 1 min readhistorical

This is a historical learning note and may contain outdated or incomplete understanding.

1. New Features

  • Lambda expressions (allow functions to be passed as method parameters)
  • Stream API
  • Date Time API
  • Optional class
  • PermGen space was removed and replaced by Metaspace

2. Common Usage

2.1. List -> Map<String, List>

public class Main
{
    public static void main(String[] args)
    {
        List<Employee> employeeList = new ArrayList<>(Arrays.asList(
                            new Employee(1, "A", 100),
                            new Employee(2, "A", 200),
                            new Employee(3, "B", 300),
                            new Employee(4, "B", 400),
                            new Employee(5, "C", 500),
                            new Employee(6, "C", 600)));

        Map<String, List<Employee>> employeesMap = employeeList.stream()
                                .collect(Collectors.groupingBy(Employee::getName));

        System.out.println(employeesMap);
    }
}

3. Reference

Discussion

Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub