/**
* jdk 1.8对于集合各种操作简化处理的学习
* (1)集合转为stream
* (2)面向流的filter(有返回值true,false),排序sorted,map[mapToInt……,无返回值,把当前值映射成为另一个对象],distinct,distinctbyKey[重写filter参数类型的方法<T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor)]
* (3)处理后的stream转为集合collect collectors.toList() Collectors.toSet(),Collectors.toMap(Person::getName,Function.identity()) Collectors.groupingBy((f)->……/Person::getName)
* (4)处理后的流取第一个值findFirst optional类型 ispresent判断
* (5)多个流的连接stream.of
* (6)操作集合的降维(降一维)flatMap(Function.identity)
*/
static List<Person> convertToPerson(List<String> nameList){
Function<String,Person> function=(f)->{
Person person=new Person();
person.setName(f);
person.setAge((int)Math.random()*100);
return person;
};
return nameList.stream().map(f->function.apply(f)).collect(Collectors.toList());
}
package java8;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* jdk 1.8对于集合各种操作简化处理的学习
* (1)集合转为stream
* (2)面向流的filter(又返回值true,false),排序sorted,map[mapToInt……,无返回值,把当前值映射成为另一个对象],distinct,distinctbyKey[重写filter参数类型的方法<T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor)]
* (3)处理后的stream转为集合collect collectors.toList() Collectors.toSet(),Collectors.toMap(Person::getName,Function.identity()) Collectors.groupingBy((f)->……/Person::getName)
* (4)处理后的流取第一个值findFirst optional类型 ispresent判断
* (5)多个流的连接stream.of
* (6)操作集合的降维(降一维)flatMap(Function.identity)
*/
public class ForTest {
public static void main(String[] args){
List<String> list=new ArrayList<>();
list.add("aa");
list.add("bb");
list.add("aabb");
list.add("cc");
//list.add("aa");
list.add("bc");
List<String> list1=new ArrayList<>();
list1.add("aa");
list1.add("bb");
list1.add("aa");
list1.add("cc");
list1.add("dd");
System.out.println(getChildALIst(list));
System.out.println(getChildALIst1(list));
System.out.println(getSingleList(list));
System.out.println(getSingleList1(list));
System.out.println(getSingleList2(list));
System.out.println(getChildBigALIst(list));
System.out.println(getChildBigALIst1(list));
System.out.println(getFirstStartAItem(list));
System.out.println(getChildBCLIst(list,true));
System.out.println(getChildBCLIst(list,false));
List<Person> aa=stringConverToPerson(list);
List<Person> aa1=stringConverToPerson(list1);
System.out.println(aa);
System.out.println(getStatisticLMap(list));
System.out.println(getStatisticLMap1(list));
System.out.println(getSameNameAgeSum(aa));
System.out.println(theSmallAgePerson(aa));
System.out.println(getNameAgeMap(aa));
String[] qq=getMulLIstToArray(list,list);
Arrays.stream(qq).forEach(f->System.out.print(f+" "));
System.out.println();
System.out.println(list);
Person[] qq1=getMulLIstToArray1(aa1,aa);
System.out.println(qq1);
Arrays.stream(qq1).forEach(f->System.out.print(f+" "));
System.out.println();
System.out.println(streamOfTest(qq1));
System.out.println("-------------");
List<List<Person>> testPerson=new ArrayList<>();
List<Person> personList1=stringConverToPerson(list1);
List<Person> personList2=stringConverToPerson(list);
testPerson.add(personList1);
testPerson.add(personList2);
System.out.println(testPerson);
System.out.println(nameMapToAge(testPerson));
System.out.println(getLimitPersonList(aa,12));
System.out.println(getLimitPersonList(aa,2));
System.out.println(distinctTneSameString(list1));
System.out.println("===============");
System.out.println(aa1);
System.out.println(distinctTheSameName(aa1));
System.out.println(distinctTheSameNameV1(aa1));
System.out.println(getNameMapPerson(aa1));
}
static class Person{
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Person() {
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
static List<String> getChildALIst(List<String> mParList){
if(CollectionUtils.isEmpty(mParList)){
return Collections.emptyList();
}
List<String> childALIst = new ArrayList<>();
for (String childItem : mParList){
if (childItem.startsWith("a")){
childALIst.add(childItem);
}
}
return childALIst;
}
static List<String> getSingleList(List<String> mParList){
if(CollectionUtils.isEmpty(mParList)){
return Collections.emptyList();
}
List<String> childALIst = new ArrayList<>();
for (String childItem : mParList){
if (!childALIst.contains(childItem)){
childALIst.add(childItem);
}
}
return childALIst;
}
static List<String> getChildALIst1(List<String> mParList) {
if (CollectionUtils.isEmpty(mParList)) {
return Collections.emptyList();
}
return mParList.stream().filter((f) -> StringUtils.startsWith(f, "a")).collect(Collectors.toList());
}
static List<String> getSingleList1(List<String> mParList){
if(CollectionUtils.isEmpty(mParList)){
return Collections.emptyList();
}
return new ArrayList<>(mParList.stream().collect(Collectors.toSet()));
}
static List<String> getSingleList2(List<String> mParList){
if(CollectionUtils.isEmpty(mParList)){
return Collections.emptyList();
}
List<String> childALIst = new ArrayList<>();
mParList.forEach((f)->{
if (!childALIst.contains(f)){
childALIst.add(f);
}
});
return childALIst;
}
static List<String> getChildBigALIst(List<String> mParList) {
if (CollectionUtils.isEmpty(mParList)) {
return Collections.emptyList();
}
List<String> childAList = mParList.stream().filter((f) -> StringUtils.startsWith(f,"a")).collect(Collectors.toList());
if (CollectionUtils.isEmpty(mParList)) {
return Collections.emptyList();
}
List<String> result = new ArrayList<>();
childAList.forEach((f)->{
result.add(f.toUpperCase());
});
return result;
}
static List<String> getChildBigALIst1(List<String> mParList) {
if (CollectionUtils.isEmpty(mParList)) {
return Collections.emptyList();
}
// return mParList.stream().filter((f) -> StringUtils.startsWith(f,"a")).map((m)->{
// return m.toUpperCase();
// }).collect(Collectors.toList());
return mParList.stream().filter((f) -> f.startsWith("a")).map((m)-> m.toUpperCase()).collect(Collectors.toList());
}
static String getFirstStartAItem(List<String> mParList) {
if (CollectionUtils.isEmpty(mParList)) {
return StringUtils.EMPTY;
}
Optional<String> aa= mParList.stream().filter((f) -> StringUtils.startsWith(f,"a")).findFirst();
return aa.isPresent()?aa.get():StringUtils.EMPTY;
}
static List<String> getChildBCLIst(List<String> mParentList, boolean isUpper){
if (CollectionUtils.isEmpty(mParentList)){
return Collections.emptyList();
}
return mParentList.stream().filter((f)->{return f.startsWith("b");}).map((f)->f.toUpperCase()).
filter((f)->f.endsWith(isUpper ? "C" : "c")).collect(Collectors.toList());
}
static List<Person> stringConverToPerson(List<String> mOrignalList){
if (CollectionUtils.isEmpty(mOrignalList)){
return Collections.emptyList();
}
return mOrignalList.stream().map((f)-> {
Person nicePersion = new Person();
nicePersion.setName(f);
nicePersion.setAge((int)(Math.random() * 100));
if (nicePersion.getAge() < 10){
return null;
}else {
return nicePersion;
}
}).sorted(personSortByAge()).collect(Collectors.toList());
}
static List<Person> stringConverToPerson2(List<String> mOrignalList){
if (CollectionUtils.isEmpty(mOrignalList)){
return Collections.emptyList();
}
return mOrignalList.stream().map(f->toPerson(f)).sorted(personSortByAge()).collect(Collectors.toList());
}
static List<Person> stringConverToPerson3(List<String> mOrignalList){
if (CollectionUtils.isEmpty(mOrignalList)){
return Collections.emptyList();
}
return mOrignalList.stream().map(Util::toPerson).sorted(personSortByAge()).collect(Collectors.toList());
}
static Person toPerson(String s){
Person nicePersion = new Person();
nicePersion.setName(s);
nicePersion.setAge((int)(Math.random() * 100));
if (nicePersion.getAge() < 10){
return null;
}else {
return nicePersion;
}
}
static Comparator<Person> personSortByAge(){
Comparator<Person> ageCompartor=(o1,o2)->{
if(o1==null&&o2!=null){
return 1;
}else if(o1!=null&&o2==null){
return -1;
}else if(o1==null&&o2==null){
return 0;
}
return o1.getAge()<o2.getAge()?-1:o1.getAge()==o2.getAge()?0:1;
};
Comparator<Person> nameCompartor=(o1,o2)->{
if(o1==null&&o2!=null){
return -1;
}else if(o1!=null&&o2==null){
return 1;
}else if(o1==null&&o2==null){
return 0;
}
return o1.getName().compareTo(o2.getName());
};
return ageCompartor.thenComparing(nameCompartor);
}
static Map<String, Integer> getStatisticLMap(List<String> mOrignal){
if (CollectionUtils.isEmpty(mOrignal)){
return Collections.emptyMap();
}
Map<String, Integer> resultMap = new HashMap<>();
mOrignal.forEach((f)->{
if (resultMap.keySet().contains(f)){
resultMap.put(f,resultMap.get(f) + 1);
}else {
resultMap.put(f,1);
}
});
return resultMap;
}
static Map<String, Integer> getStatisticLMap1(List<String> mOrignal){
if (CollectionUtils.isEmpty(mOrignal)){
return Collections.emptyMap();
}
Map<String,List<String>> strMap=mOrignal.stream().collect(Collectors.groupingBy((f)->f));
if(CollectionUtils.isEmpty(strMap)){
return Collections.emptyMap();
}
Map<String, Integer> resultMap = new HashMap<>();
strMap.forEach((k,v)->{
resultMap.put(k,v.size());
});
return resultMap;
}
static Map<String, Integer> getSameNameAgeSum(List<Person> mPersonList){
if (CollectionUtils.isEmpty(mPersonList)){
return Collections.emptyMap();
}
Map<String,List<Person>> mNamePersonMap = mPersonList.stream().filter((f) -> Objects.nonNull(f)).collect(Collectors.groupingBy((f) -> f.getName()));
Map<String, Integer> mNameMapAgeSum = new HashMap<>();
mNamePersonMap.forEach((k, v) -> {
mNameMapAgeSum.put(k,
v.stream().map((f) -> f.getAge()).filter((f) -> Objects.nonNull(f)).reduce(0,Integer::sum));
// int ageSum=ageList.stream().reduce(0,Integer::sum);
// ageList.stream().reduce(0,Integer::sum);
});
return mNameMapAgeSum;
}
static Person theSmallAgePerson(List<Person> mPersonList){
if (CollectionUtils.isEmpty(mPersonList)){
return null;
}
Optional<Person> smallAgePerson = mPersonList.stream().sorted(personSortByAge()).filter((f) -> Objects.nonNull(f)).findFirst();
return smallAgePerson.isPresent() ? smallAgePerson.get() : null;
}
static Map<String, Integer> getNameAgeMap(List<Person> mPersonList){
if (CollectionUtils.isEmpty(mPersonList)){
return Collections.emptyMap();
}
return mPersonList.stream().filter((f) -> Objects.nonNull(f)).collect(Collectors.toMap(Person::getName, Person::getAge));
}
static String[] getMulLIstToArray(List<String> l1, List<String> l2){
if (CollectionUtils.isEmpty(l1) || CollectionUtils.isEmpty(l2)){
return null;
}
l1.addAll(l2);
return l1.toArray(new String[]{});
}
static Person[] getMulLIstToArray1(List<Person> l1, List<Person> l2){
if (CollectionUtils.isEmpty(l1) || CollectionUtils.isEmpty(l2)){
return null;
}
return Stream.of(l1.stream(), l2.stream()).filter((f) -> Objects.nonNull(f)).flatMap(Function.identity()).toArray(Person[]::new);
}
static List<Person> streamOfTest(Person[] l1){
return Stream.of(l1).collect(Collectors.toList());
}
static Map<String,Integer> nameMapToAge(List<List<Person>> personList){
if(CollectionUtils.isEmpty(personList)){
return null;
}
Map<String,Integer> result=new HashMap<>();
Map<String,List<Person>> nameMap=personList.stream().filter((f)->Objects.nonNull(f)).flatMap(f->f.stream()).filter((f)->Objects.nonNull(f)).collect(Collectors.groupingBy(Person::getName));
nameMap.forEach((k,v)->{
result.put(k,v.stream().mapToInt((f)->f.getAge()).sum());
v.stream().mapToInt((f)->f.getAge()).min();
v.stream().mapToInt((f)->f.getAge()).max();
v.stream().mapToInt((f)->f.getAge()).average();
v.stream().mapToInt((f)->f.getAge()).limit(6);
});
return result;
}
static List<Person> getLimitPersonList(List<Person> mPersonList, int maxSize){
if (CollectionUtils.isEmpty(mPersonList)){
return Collections.emptyList();
}
return mPersonList.stream().filter((f) -> Objects.nonNull(f)).limit(maxSize).collect(Collectors.toList());
}
static List<String> distinctTneSameString(List<String> mParentList){
if (CollectionUtils.isEmpty(mParentList)){
return Collections.emptyList();
}
return mParentList.stream().distinct().collect(Collectors.toList());
}
static List<Person> distinctTheSameName(List<Person> mPersonList){
if(CollectionUtils.isEmpty(mPersonList)){
return Collections.emptyList();
}
return mPersonList.stream().filter((f) -> Objects.nonNull(f)).filter(distinctByKey(Person::getName)).collect(Collectors.toList());
}
static List<Person> distinctTheSameNameV1(List<Person> mPersonList){
if(CollectionUtils.isEmpty(mPersonList)){
return Collections.emptyList();
}
return mPersonList.stream().filter((f) -> Objects.nonNull(f)).filter(distinctByKeyV1(Person::getName)).collect(Collectors.toList());
}
private static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
Set<Object> seen = ConcurrentHashMap.newKeySet();
return t -> seen.add(keyExtractor.apply(t));
}
private static <T> Predicate<T> distinctByKeyV1(Function<? super T, ?> keyExtractor) {
Map<Object,Boolean> seen = new ConcurrentHashMap<>();
return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}
static Map<String,Person> getNameMapPerson(List<Person> mPersonList){
if (CollectionUtils.isEmpty(mPersonList)){
return Collections.emptyMap();
}
return mPersonList.stream().filter(distinctByKey(Person::getName)).collect(Collectors.toMap(Person::getName,Function.identity()));
}
}