常用操作的步骤
- 将list转成流 list.stream()
- 然后stream流里面有filter过滤【//按条件过滤集合[list里面原本有10个Student,过滤后就只有3个了]】,有map()抽离【每个对象的属性名和值其实是一个键值对,一个对象其实就是一组键值对构成的集合,map就是单独抽离出每组的一个键值对构成新的集合】,
- Stream filter(Predicate<? super T> predicate);
Stream map(Function<? super T, ? extends R> mapper);
IntStream mapToInt(ToIntFunction<? super T> mapper);
Stream flatMap(Function<? super T, ? extends Stream<? extends R>> mapper);
IntStream flatMapToInt(Function<? super T, ? extends IntStream> mapper);
Stream distinct();
Stream sorted(Comparator<? super T> comparator);
Stream limit(long maxSize);
void forEach(Consumer<? super T> action);
void forEachOrdered(Consumer<? super T> action);
A[] toArray(IntFunction<A[]> generator);
T reduce(T identity, BinaryOperator accumulator);
<R, A> R collect(Collector<? super T, A, R> collector);
Optional max(Comparator<? super T> comparator);
long count();
public static Stream iterate(final T seed, final UnaryOperator f)
public static Stream of(T t)
//按条件过滤集合[list里面原本有10个Student,过滤后就只有3个了]
List<Student> collect = lists.stream().filter(s -> s.getNumber()==1).collect(Collectors.toList());
//筛选出没有分配班级的学生
List<Student> collect = stuList.stream().filter(student -> stuClassList.stream().noneMatch(stuClass -> stuClass.getStudentNo().equals(student.getStudentNo()))).collect(Collectors.toList());
// 根据name,sex两个属性去重
List<Person> unique = persons.stream().collect(
Collectors. collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getName() + ";" + o.getSex()))), ArrayList::new)
);
//list转map
Map<String, String> collect = list.stream().collect(Collectors.toMap(p -> p.getId(), p -> p.getName()));
//1.提取出list对象中的一个属性
List<String> stIdList1 = stuList.stream().map(Person::getId).collect(Collectors.toList());
//2.提取出list对象中的一个属性并去重【每个对象的属性名和值其实是一个键值对,一个对象其实就是一组键值对构成的集合,map就是单独抽离出每组的一个键值对构成新的集合】
List<String> stIdList2 = stuList.stream().map(Person::getId).distinct().collect(Collectors.toList());
//省去了重写hashcode equals 非常有用 谢谢 // 根据name,sex两个属性去重 List<Person> unique = persons.stream().collect( Collectors. collectingAndThen( Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getName() + ";" + o.getSex()))), ArrayList::new) );
//3. map 转list
map 转list
根据map的key值排序
List list = map.entrySet().stream().sorted(Comparator.comparing(e -> e.getKey()))
.map(e -> new Person(e.getKey(), e.getValue())).collect(Collectors.toList());
根据map的value值排序
List list = map.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getValue))
.map(e -> new Person(e.getKey(), e.getValue())).collect(Collectors.toList());
根据map的value值排序
List list = map.entrySet().stream().sorted(Map.Entry.comparingByKey())
.map(e -> new Person(e.getKey(), e.getValue())).collect(Collectors.toList());
过滤list中的一些条件形成新的list
list = List.stream().filter(sourceCarModelId -> 过滤条件).collect(Collectors.toList());
基础:
https://www.runoob.com/java/java8-streams.html
Stream sorted() 示例:
https://www.cnblogs.com/a-du/p/8289537.html
如何创建Lambda函数将String转换为Integer
用法初探:
public class M {
public static void main(String arg[]){
List<String> strings = Arrays.asList("43","2","3","453","25","35","543","52","53");
// strings.stream()
// strings.stream().sorted(Comparator.reverseOrder()).limit(5).forEach(System.out::println);
// Random random = new Random();
//random.ints().limit(10).sorted().forEach(System.out::println);
// long a= 11325856+2211984+15756158+37320486+10666197+9174242+494500+46657516;
// System.out.println(a);
Map<String,Integer>sortMap=new LinkedHashMap<>();
Map<String,Integer>map=new HashMap<>();
map.put("a",1);
map.put("b",1343);
map.put("c",21);
map.put("d",11);
// map.forEach((k,v)->{System.out.println(k+"="+v);});
map.entrySet().stream().sorted( Map.Entry.<String, Integer>comparingByValue().reversed()).limit(3).forEachOrdered(e->sortMap.put(e.getKey(),e.getValue()));
// map.entrySet().stream().sorted().limit(3).forEachOrdered(e->m.put(e.getKey(),e.getValue()));
// for (Map.Entry<String, Object> k:m.entrySet()){
// System.out.println(k.getValue());
// }
sortMap.forEach((k,v)->{System.out.println(k+"="+v);});
}
}
按照map的value给map排序
- 直接给fastJSON里的JSONObject按照值排序我试了几种方法,不好使,没办法,只好将其转为map来处理了
public class EightThird {
public static void main( String[] args )
{
JSONObject jsonObject=new JSONObject();
LinkedHashMap<String,Object> sortMap=new LinkedHashMap<>();
jsonObject.put("a",1);
jsonObject.put("b",233);
jsonObject.put("c",223);
jsonObject.put("d",12);
Map<String, Integer> itemMap = JSONObject.toJavaObject(jsonObject, Map.class);
// Map<String,Integer>params=JSONObject.parseObject(jsonObject.toJSONString(), (Type) new HashMap<String,Integer>());
itemMap.entrySet().stream().forEach(e->System.out.println(e.getValue()+"="+e.getKey()));
//System.out.println("ok");
//jsonObject.entrySet().stream().sorted(Comparator.comparing(Student::getAge).reversed()).limit(3).forEachOrdered(e->sortMap.put(e.getKey(),e.getValue()));
// jsonObject.entrySet().stream().sorted((Comparator<? super Map.Entry<String, Object>>) Map.Entry.<String, Integer>comparingByValue().reversed()).limit(3).forEachOrdered(e->sortMap.put(e.getKey(),e.getValue()));
// sortMap.entrySet().stream().forEach(e->System.out.println(e.getKey()+"="+e.getValue()));
}
}
JSONObject jsonObject=new JSONObject();
Map<String,Object>sortMap=new LinkedHashMap<>();
List<JSONObject> temp1="从数据库查询得到的";
for (String es:temp1.get(0).keySet()){
if (temp1.get(0).get(es)!=null){
allTotal+=Integer.parseInt((String) temp1.get(0).get(es));
jsonObject.put(es,Integer.parseInt((String) temp1.get(0).get(es)));
}
}
Map<String, Integer> itemMap = JSONObject.toJavaObject(jsonObject, Map.class);
itemMap.entrySet().stream().sorted(Map.Entry.<String, Integer>comparingByValue().reversed()).limit(5).forEachOrdered(e->sortMap.put(e.getKey(),e.getValue()));
sortMap.put("total",allTotal);
return sortMap;
https://www.cnblogs.com/woyaobianfei/p/9187127.html