日常用到,一点一滴记录,不断丰富,知识积累,塑造自身价值。欢迎收藏
String 转 List
String str = "1,2,3,4";
List lists = Arrays.stream(str.split(",")).map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
List 转 String
list.stream().map(String::valueOf).collect(Collectors.joining(","))
List<> 对象获取某个值 转为 Long[] , String[] , Integer[]
基本类型:
List list = new ArrayList();
list.add(1L);
list.add(2L);
list.add(3L);
Long[] l = list.stream().toArray(Long[]::new)
String[] s = list.stream().toArray(String[]::new)
Integer[] in = list.stream().toArray(Integer[]::new)
对象形式:
List list = new ArrayList();
ScheduleJobEntity sc = new ScheduleJobEntity()
sc.setId(1)
list.add(sc);
sc = new ScheduleJobEntity()
sc.setId(2)
list.add(sc);
sc = new ScheduleJobEntity()
sc.setId(3)
list.add(sc);
list.stream().map(ScheduleJobEntity::getId).toArray(Long[]::new)
list.stream().map(ScheduleJobEntity::getId).toArray(Long[]::new)
list.stream().map(ScheduleJobEntity::getId).toArray(Long[]::new)
Long[] l = list.stream().toArray(Long[]::new)
String[] s = list.stream().toArray(String[]::new)
Integer[] in = list.stream().toArray(Integer[]::new)
List<> 对象获取某个值 转为 list
List = new ArrayList();
List items = itemsBeans.stream().map(ItemsBean::getItemid).collect(Collectors.toList());
Set 转 List
Set sets = xxx
List hids = sets.stream().collect(Collectors.toList());
List 转 Map
List hosts = xxxx
Map hostMap = hosts.stream().collect(Collectors.toMap(Hosts::getHostid,Hosts::getName,(oldValue, newValue) -> newValue));
List 转 Map
List hosts = xxxx
Map appsMap= apps.stream().collect(Collectors.toMap(ItemTriggerGroupByName::getHostId, e -> e , (oldValue, newValue) -> newValue));
List 分组 转 Map
Map> groupBy = appleList.stream().collect(Collectors.groupingBy(Apple::getId));
List
List
List 求和,最小,最大值,平均值获取
单数组类型
List strs = Arrays.asList(34,5,1,76,23);
最小值
strs .stream().min(Comparator.comparing(Function.identity())).get();
最大值
strs .stream().max(Comparator.comparing(Function.identity())).get();
对象数组类型
int sum = empList.stream().mapToInt(Employee::getAge()).sum();
int max = empList.stream().mapToInt(Employee::getAge()).max().getAsInt();
int min = empList.stream().mapToInt(Employee::getAge()).min().getAsInt();
double avg = empList.stream().mapToInt(Employee::getAge()).average().getAsDouble();
System.out.println("最大值:"+max+"\n最小值:"+min+"\n总和:"+sum+"\n平均值:"+avg);