Java 类型转换

引用对象数组类型转换

  • String数组转Integer数组

 String[] strIds = new String[]{"123","321","567"};
 Integer[] ids = (Integer[]) ConvertUtils.convert(strIds, Integer.class);

 Double[] ids = (Double[]) ConvertUtils.convert(strIds, Double.class);
 Long[] ids = (Long[]) ConvertUtils.convert(strIds, Long.class);

注:支持数组与数组之间的直接转换,如String->Long String->Double

数组与List转换


 public class User{
     private Long id;
     private String name;
     //省略get,set....
 }
 
String[] arrays = {"a", "b", "c"};

//数组转List
List<String> list = Stream.of(arrays).collect(Collectors.toList());
//List转数组
String[] strings = list.stream().toArray(String[]::new);
//对象取值
String[] strings = list.stream().map(User::getName).toArray();
//对象取值,转型
String[] ids =  (String[])ConvertUtils.convert(list.stream().map(User::getId).toArray(), String.class);


引用对象转值

  • Long数组转long数组

 Long[] longIds = new Long[]{123123L,321321L,56786L};
 long[] ids = Arrays.stream(longIds).filter(Objects::nonNull).distinct().mapToLong(Long::longValue).toArray();

 //String[] strIds = new String[]{"123","321","567"};
 //long[] ids = Arrays.stream(strIds).filter(Objects::nonNull).distinct().mapToLong(Long::parseLong).toArray();

List转Map


 public class User{
     private Long id;
     private String name;
     //省略get,set....
 }
 
 //不能重复Key,值不能为空
 Map<Long, String> map = list.stream().collect(Collectors.toMap(User::getId, User::getName));
 //重载,不能重复Key,值可以为空
 Map<Long, String> map = list.stream().collect(HashMap::new, (m,v)->m.put(v.getId(), v.getName()),HashMap::putAll);
 //去重复取新值 Map值不能为空
 Map<Long, String> map = list.stream().collect(Collectors.toMap(User::getId, User::getName,(oldValue,newValue)->newValue)));
 //获取对象   Map值不能为空
 Map<Long, User> map = list.stream().collect(Collectors.toMap(User::getId,Function.identity(),(oldValue,newValue)->newValue)));
 //去重复取新值  过滤空
 Map<Long, String> map = list.stream()
          .filter(user->user.getId()!=null)
          .filter(user->user.getName()!=null)
          .collect(Collectors.toMap(User::getId, User::getName,(oldValue,newValue)->newValue)));

Map转List


 public class User{
     private Long id;
     private String name;
     //省略get,set....
 }
 
 //获取值
 List<User> userList = new ArrayList<>(userMap.values());
 //获取键
 List<Long> ids = new ArrayList(userMap.keySet());
 //获取子元素
 List<String> names =  userMap.values().stream().collect(ArrayList::new,(m,v)->m.add(v.getName()),ArrayList::addAll);
//获取子元素 + 过滤
 List<String> names =  userMap.values().stream()
          .filter(user->!user.getName().equals("admin"))
          .collect(ArrayList::new,(m,v)->m.add(v.getName()),ArrayList::addAll);

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 对象的创建与销毁 Item 1: 使用static工厂方法,而不是构造函数创建对象:仅仅是创建对象的方法,并非Fa...
    孙小磊阅读 6,233评论 0 3
  • 日常开发过程中,我们经常会遇到基本数据类型的转换,今天就来稍微总结一下Java的类型转换。 1.自动类型转换(隐式...
    晏子小七阅读 2,905评论 0 0
  • 基本类型:从左到右转换规则表 String 转 Integer / int Integer / int 转 Str...
    07120665a058阅读 3,379评论 0 0
  • 一、值类型与引用类型 值类型: 在Java中,值类型表示的是这种数据类型的值代表数据本身。八种基本数据类型就是值类...
    夜阑w阅读 7,305评论 0 2
  • 1..String转换成Date类型 2.Date转换成String类型 3.Long类型转Date类型 4.St...
    孔嘚嘚儿阅读 1,085评论 0 1