/**
*
*/
package com.ljh.myo2o;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;
/**
* @description:
* @author :ljh
* @CreateTime:2018-12-03 07:42:20
*/
public class MainMethodTest {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Mango");
list.add("Banana");
list.add("Mango");
list.add("Apple");
System.out.printf("原List集合是:%s%n",list.toString());
Set<String> linkedHashSet = new LinkedHashSet<>(list);
System.out.println(linkedHashSet);
//去重复后,按自然顺序排序
Set<String> treeSet = new TreeSet<>(list);
System.out.println(treeSet);
//java8 以后提供了流API,使得List集合去重复更简单
//将list变成流使用 list.stream(),将流变成list使用流的 collect方法
List<String> collect = list.stream().distinct().collect(Collectors.toList());
System.out.println(collect);
//对list集合进行分组
Map<Integer, List<String>> collect2 = list.stream().collect(Collectors.groupingBy(String::length));
// Map遍历
collect2.forEach((k, v) -> {
System.out.println(k);
System.out.println(v);
});
}
}