java8 Stream API

The Stream flow

  1. A Stream is a cluster of elements from a data source that can support aggregation operations.
  2. Data source: The data source for the Stream, the data source for constructing the Stream object. For example, a Stream object is constructed from a List that is the data source;
  3. Aggregation operations: Operations that process Stream objects and return data from specified rules are called aggregation operations. Filter, map, restrict, sort, and so on are all aggregation operations.

Create an entity class

public class UmsPermission implements Serializable { private Long id; @apiModelProperty (value = "id") private Long PID; @apiModelProperty (value = "name ") private String name; @apiModelProperty (value = "value ") private String value; @apiModelProperty (value = "icon ") private String icon; @apiModelProperty (value = "permission type: 0-> directory; 1 - > menu. 2-> button (interface binding permission) ") private Integer type; @apiModelProperty (value = "front end resource path ") private String URI; @apiModelProperty (value = "enabled; 0 - > disabled; 1-> enable ") private Integer status; @apiModelProperty (value = "createTime ") private Date createTime; @apiModelProperty (value = "sort ") private Integer sort; private static final long serialVersionUID = 1L; // Omit all getters and setters}Copy the code

Create a flow

// Create list list <UmsPermission> permissionList = new ArrayList(); // Create a serial Stream object for the collection Stream<UmsPermission> Stream = permissionList.stream(); / / to create a parallel flow collection object Stream < UmsPermission > parallelStream = permissionList. ParallelStream ();Copy the code

The filter to filter

Return true: return true: return true: return true: return true: return true

Permission.gettype () Obtain the type of the object. Determine whether the type is 1 List<UmsPermission> dirList = permissionList.stream() .filter(permission -> permission.getType() == 0) .collect(Collectors.toList()); List<UmsPermission> dirList = permissionList.stream().filter(permission -> {if (permission.getType() == 0){ return true; } return false; }).collect(Collectors.toList());Copy the code

The map screen

Map to get the value in the map bracket return

List<Long> idList = permissionList.stream().map(permission -> permission.getid ()) .collect(Collectors.toList()); List<Integer> List = permissionList.stream().map(permission -> {return permission.getid (); }).collect(Collectors.toList());Copy the code

List Gets the specified number of elements

Gets a specified number of elements from Stream.

List<UmsPermission> firstFiveList = permissionList.stream().limit(3).collect(seller.tolist ());Copy the code

Skip Skips the specified index

List<UmsPermission> skipList = permissionList.stream().skip(5).collect(seller.tolist ());Copy the code

Count Get total number

// Count operation: Long dirPermissionCount = permissionList.stream().filter(permission -> permission.getType() == 0).count();Copy the code

Sorted order

Sorted sorted sorted brackets return minus 1, 0, 1

List<UmsPermission> sortedList = permissionList.stream() .sorted(Comparator.comparing(UmsPermission::getType)).collect(Collectors.toList()); // Reversed List<UmsPermission> sortedList = permissionlist.stream (); .sorted(Comparator.comparing(UmsPermission::getType).reversed()).collect(Collectors.toList()); List<UmsPermission> sortedList = permissionList.stream() //permission1: the value of the next element .sorted((permission1,permission2)->{// getType() is less than permission2. GetType () returns -1 in positive order //permission1.getType() is equal to permission2.getType() returns 0 unsorted //permission1.getType() is greater than permission2.getType() returns 1 in reverse order return permission1.getType().compareTo(permission2.getType()); }) .collect(Collectors.toList());Copy the code

The controller method

Map<Long,UmsPermission> type key: id, value: Object Map < Long, UmsPermission> permissionMap = permissionList.stream() .collect(Collectors.toMap(permission -> permission.getId(), permission -> permission )); (oldValue, newValue) -> newValue (oldValue, newValue); UmsPermission> permissionMap = permissionList.stream() .collect(Collectors //(oldValue, NewValue) -> newValue (oldValue) -> newValue (oldValue) -> newValue (oldValue) The current value is newValue(the current value overwrites the value in the original map). ToMap (permission -> permission.getid (), permission -> permission ,(oldValue,newValue)-> newValue)); List<Map<String,Object>> List<Map<String,Object>> Collect = permissionList.stream().map(permission -> new) BeanMap(permission)) .collect(Collectors.toList()); Set<Map<String,Object>> Set<Map<String,Object>> Collect = permissionList.stream().map(permission -> new) BeanMap(permission)) .collect(Collectors.toSet());Copy the code