先創建兩個實體類
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.math.BigDecimal;
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Item {
private String name;
private int qty;
private BigDecimal price;
}
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Person {
private int id;
private String name;
private String address;
}
FilterMap操作
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
public class ExampleFilterMap {
private static Map<Integer,String> map = new HashMap<>();
static {
map.put(1,"熊大");
map.put(2,"熊二");
map.put(3,"光頭強");
map.put(4,"吉吉國王");
map.put(5,"土坡鼠");
}
public static void main(String[] args){
String result = null;
for (Map.Entry<Integer,String> entry:map.entrySet()){
if ("熊大".equals(entry.getValue())){
result = entry.getValue();
}
}
System.out.println("Before java 8 :"+result);
result = map.entrySet().stream().filter(map->"熊二".equals(map.getValue()))
.map(map->map.getValue())
.collect(Collectors.joining());
System.out.println("After java 8 "+result);
Map<Integer,String> collect = map.entrySet().stream().filter(c->c.getKey()==3)
.collect(Collectors.toMap(p->p.getKey(),p->p.getValue()));
System.out.println(collect);
}
}
控制檯輸出
Before java 8 :熊大
After java 8 熊二
{3=光頭強}
List操作
import java.util.ArrayList;
import java.util.List;
public class ExampleList {
private static List<String> items = new ArrayList<>();
static {
items.add("A");
items.add("BC");
items.add("C");
items.add("BD");
items.add("E");
}
public static void main (String[] args){
for (String item:items){
System.out.println(item);
}
System.out.println("-------------");
items.forEach(c-> System.out.println(c));
System.out.println("-------------");
items.forEach(item->{
if ("C".equals(item)){
System.out.println(item);
}
});
System.out.println("-------------");
items.stream().filter(s->s.contains("B")).forEach(c1-> System.out.println(c1));
}
}
控制檯輸出
A
BC
C
BD
E
-------------
A
BC
C
BD
E
-------------
C
-------------
BC
BD
Map操作
import java.util.HashMap;
import java.util.Map;
public class ExampleMap {
private static Map<String, Integer> items = new HashMap<>();
static {
items.put("A", 10);
items.put("B", 20);
items.put("C", 30);
items.put("D", 40);
items.put("E", 50);
items.put("F", 60);
items.put("G", 70);
items.put("H", 80);
}
public static void main(String[] args){
for (Map.Entry<String,Integer> entry:items.entrySet()){
System.out.println("key:" + entry.getKey() + " value:" + entry.getValue());
}
System.out.println("---------------------");
items.forEach((key,value)-> System.out.println("key:" + key + " value:" + value));
}
}
控制檯輸出
key:A value:10
key:B value:20
key:C value:30
key:D value:40
key:E value:50
key:F value:60
key:G value:70
key:H value:80
---------------------
key:A value:10
key:B value:20
key:C value:30
key:D value:40
key:E value:50
key:F value:60
key:G value:70
key:H value:80
Groupingby操作
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
public class ExampleMapping {
public static void main(String[] args) {
List<Item> items = Arrays.asList(
new Item("apple", 10, new BigDecimal(23.5)),
new Item("apple", 20, new BigDecimal(32.5)),
new Item("orange", 30, new BigDecimal(13.9)),
new Item("orange", 20, new BigDecimal(33.5)),
new Item("orange", 10, new BigDecimal(63.5)),
new Item("orange", 50, new BigDecimal(41.5)),
new Item("peach", 20, new BigDecimal(26.5)),
new Item("peach", 30, new BigDecimal(42.5)),
new Item("peach", 40, new BigDecimal(24.5)),
new Item("peach", 10, new BigDecimal(12.5))
);
Map<String, Long> counting = items.stream()
.collect(Collectors.groupingBy(Item::getName, Collectors.counting()));
System.out.println("counting:"+counting);
Map<String, Integer> sum = items.stream()
.collect(Collectors.groupingBy(Item::getName, Collectors.summingInt(Item::getQty)));
System.out.println("sum:"+sum);
Map<BigDecimal,List<Item>> groupByPriceMap = items.stream().collect(Collectors.groupingBy(Item::getPrice));
System.out.println("groupByPriceMap:"+groupByPriceMap);
Map<BigDecimal, Set<String>> result = items.stream().collect(Collectors.groupingBy(Item::getPrice,Collectors
.mapping(Item::getName,Collectors.toSet())));
System.out.println("result:"+result);
}
}
控制檯輸出
counting:{orange=4, apple=2, peach=4}
sum:{orange=110, apple=30, peach=100}
groupByPriceMap:{41.5=[Item(name=orange, qty=50, price=41.5)], 33.5=[Item(name=orange, qty=20, price=33.5)], 12.5=[Item(name=peach, qty=10, price=12.5)], 63.5=[Item(name=orange, qty=10, price=63.5)], 23.5=[Item(name=apple, qty=10, price=23.5)], 42.5=[Item(name=peach, qty=30, price=42.5)], 26.5=[Item(name=peach, qty=20, price=26.5)], 24.5=[Item(name=peach, qty=40, price=24.5)], 32.5=[Item(name=apple, qty=20, price=32.5)], 13.9000000000000003552713678800500929355621337890625=[Item(name=orange, qty=30, price=13.9000000000000003552713678800500929355621337890625)]}
result:{41.5=[orange], 33.5=[orange], 12.5=[peach], 63.5=[orange], 23.5=[apple], 42.5=[peach], 26.5=[peach], 24.5=[peach], 32.5=[apple], 13.9000000000000003552713678800500929355621337890625=[orange]}
Groupingby操作
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class ExampleTwoMapping {
private static List<Person> personList = new ArrayList<>();
static {
personList.add(Person.builder().id(10).name("張三瘋").address("河南").build());
personList.add(Person.builder().id(11).name("楊過").address("湖北").build());
personList.add(Person.builder().id(12).name("小龍女").address("江西").build());
}
public static void main(String[] args) {
Map<String, List<Person>> collect = personList.stream().collect(Collectors.groupingBy(c -> c.getAddress()));
System.out.println("collect:" + collect);
System.out.println("--------分割線-------");
Map<Integer, Person> map_ = personList.stream().collect(Collectors.toMap((key -> key.getId()), (value -> value)));
map_.forEach((key, value) -> System.out.println(key + ":" + value));
}
}
控制檯輸出
collect:{河南=[Person(id=10, name=張三瘋, address=河南)], 湖北=[Person(id=11, name=楊過, address=湖北)], 江西=[Person(id=12, name=小龍女, address=江西)]}
--------分割線-------
10:Person(id=10, name=張三瘋, address=河南)
11:Person(id=11, name=楊過, address=湖北)
12:Person(id=12, name=小龍女, address=江西)