Chinaunix首页 | 论坛 | 博客
  • 博客访问: 544733
  • 博文数量: 298
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 3077
  • 用 户 组: 普通用户
  • 注册时间: 2019-06-17 10:57
文章分类

全部博文(298)

文章存档

2022年(96)

2021年(201)

2019年(1)

我的朋友

分类: Java

2021-07-28 10:29:38


点击(此处)折叠或打开

  1. package com.ven.utils;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Optional;
  6. /**
  7.  * java 8 Optional 工具类
  8.  * @author fhadmin
  9.  * @from fhadmin.cn
  10.  * @ version 1.0
  11.  */
  12. public class OptionalUtil {
  13.  
  14.     /**
  15.      * 判断输入的字符串是否为空
  16.      * @param str String
  17.      * @return
  18.      */
  19.     public static boolean isEmpty(String str){
  20.         return Optional.ofNullable(str).isPresent();
  21.     }
  22.     
  23.     /**
  24.      * 判断输入的Integer是否为空
  25.      * @param num Integer
  26.      * @return
  27.      */
  28.     public static boolean isEmpty(Integer num){
  29.         return Optional.ofNullable(num).isPresent();
  30.     }
  31.     /**
  32.      * 判断输入的Double是否为空
  33.      * @param dnum Double
  34.      * @return
  35.      */
  36.     public static boolean isEmpty(Double dnum){
  37.         return Optional.ofNullable(dnum).isPresent();
  38.     }
  39.     /**
  40.      * 判断输入的Object是否为空
  41.      * @param obj
  42.      * @return
  43.      */
  44.     public static boolean isEmpty(Object obj){
  45.         return Optional.ofNullable(obj).isPresent();
  46.         
  47.     }
  48.     
  49.     /**
  50.      * 判断输入的List是否为空,获取该对象
  51.      * @param obj
  52.      * @return
  53.      */
  54.     public static Object get(Object obj){
  55.         return Optional.ofNullable(obj).isPresent()!=false?Optional.ofNullable(obj).get():null;
  56.         
  57.     }
  58.     /**
  59.      * 测试
  60.      * @param args
  61.      */
  62.     @SuppressWarnings("unchecked")
  63.     public static void main(String[] args) {
  64.         List<String> list = new ArrayList<>();
  65.         list.add("a");
  66.         list.add("b");
  67.         list.add("c");
  68.         List<String> objList = (List<String>) OptionalUtil.get(list);
  69.         objList.forEach(item->{System.out.println(item);});
  70.         
  71.         String str = null;
  72.         System.out.println("当前对象:"+OptionalUtil.get(str));
  73.         
  74.         
  75.     }
  76. }


点击(此处)折叠或打开

  1. package com.ven.utils;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Optional;
  6. /**
  7.  * java 8 Optional 工具类
  8.  * @author fhadmin
  9.  * @from fhadmin.cn
  10.  * @ version 1.0
  11.  */
  12. public class OptionalUtil {
  13.  
  14.     /**
  15.      * 判断输入的字符串是否为空
  16.      * @param str String
  17.      * @return
  18.      */
  19.     public static boolean isEmpty(String str){
  20.         return Optional.ofNullable(str).isPresent();
  21.     }
  22.     
  23.     /**
  24.      * 判断输入的Integer是否为空
  25.      * @param num Integer
  26.      * @return
  27.      */
  28.     public static boolean isEmpty(Integer num){
  29.         return Optional.ofNullable(num).isPresent();
  30.     }
  31.     /**
  32.      * 判断输入的Double是否为空
  33.      * @param dnum Double
  34.      * @return
  35.      */
  36.     public static boolean isEmpty(Double dnum){
  37.         return Optional.ofNullable(dnum).isPresent();
  38.     }
  39.     /**
  40.      * 判断输入的Object是否为空
  41.      * @param obj
  42.      * @return
  43.      */
  44.     public static boolean isEmpty(Object obj){
  45.         return Optional.ofNullable(obj).isPresent();
  46.         
  47.     }
  48.     
  49.     /**
  50.      * 判断输入的List是否为空,获取该对象
  51.      * @param obj
  52.      * @return
  53.      */
  54.     public static Object get(Object obj){
  55.         return Optional.ofNullable(obj).isPresent()!=false?Optional.ofNullable(obj).get():null;
  56.         
  57.     }
  58.     /**
  59.      * 测试
  60.      * @param args
  61.      */
  62.     @SuppressWarnings("unchecked")
  63.     public static void main(String[] args) {
  64.         List<String> list = new ArrayList<>();
  65.         list.add("a");
  66.         list.add("b");
  67.         list.add("c");
  68.         List<String> objList = (List<String>) OptionalUtil.get(list);
  69.         objList.forEach(item->{System.out.println(item);});
  70.         
  71.         String str = null;
  72.         System.out.println("当前对象:"+OptionalUtil.get(str));
  73.         
  74.         
  75.     }
  76. }


点击(此处)折叠或打开

  1. package com.ven.utils;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Optional;
  6. /**
  7.  * java 8 Optional 工具类
  8.  * @author fhadmin
  9.  * @from fhadmin.cn
  10.  * @ version 1.0
  11.  */
  12. public class OptionalUtil {
  13.  
  14.     /**
  15.      * 判断输入的字符串是否为空
  16.      * @param str String
  17.      * @return
  18.      */
  19.     public static boolean isEmpty(String str){
  20.         return Optional.ofNullable(str).isPresent();
  21.     }
  22.     
  23.     /**
  24.      * 判断输入的Integer是否为空
  25.      * @param num Integer
  26.      * @return
  27.      */
  28.     public static boolean isEmpty(Integer num){
  29.         return Optional.ofNullable(num).isPresent();
  30.     }
  31.     /**
  32.      * 判断输入的Double是否为空
  33.      * @param dnum Double
  34.      * @return
  35.      */
  36.     public static boolean isEmpty(Double dnum){
  37.         return Optional.ofNullable(dnum).isPresent();
  38.     }
  39.     /**
  40.      * 判断输入的Object是否为空
  41.      * @param obj
  42.      * @return
  43.      */
  44.     public static boolean isEmpty(Object obj){
  45.         return Optional.ofNullable(obj).isPresent();
  46.         
  47.     }
  48.     
  49.     /**
  50.      * 判断输入的List是否为空,获取该对象
  51.      * @param obj
  52.      * @return
  53.      */
  54.     public static Object get(Object obj){
  55.         return Optional.ofNullable(obj).isPresent()!=false?Optional.ofNullable(obj).get():null;
  56.         
  57.     }
  58.     /**
  59.      * 测试
  60.      * @param args
  61.      */
  62.     @SuppressWarnings("unchecked")
  63.     public static void main(String[] args) {
  64.         List<String> list = new ArrayList<>();
  65.         list.add("a");
  66.         list.add("b");
  67.         list.add("c");
  68.         List<String> objList = (List<String>) OptionalUtil.get(list);
  69.         objList.forEach(item->{System.out.println(item);});
  70.         
  71.         String str = null;
  72.         System.out.println("当前对象:"+OptionalUtil.get(str));
  73.         
  74.         
  75.     }
  76. }


点击(此处)折叠或打开

  1. package com.ven.utils;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Optional;
  6. /**
  7.  * java 8 Optional 工具类
  8.  * @author fhadmin
  9.  * @from fhadmin.cn
  10.  * @ version 1.0
  11.  */
  12. public class OptionalUtil {
  13.  
  14.     /**
  15.      * 判断输入的字符串是否为空
  16.      * @param str String
  17.      * @return
  18.      */
  19.     public static boolean isEmpty(String str){
  20.         return Optional.ofNullable(str).isPresent();
  21.     }
  22.     
  23.     /**
  24.      * 判断输入的Integer是否为空
  25.      * @param num Integer
  26.      * @return
  27.      */
  28.     public static boolean isEmpty(Integer num){
  29.         return Optional.ofNullable(num).isPresent();
  30.     }
  31.     /**
  32.      * 判断输入的Double是否为空
  33.      * @param dnum Double
  34.      * @return
  35.      */
  36.     public static boolean isEmpty(Double dnum){
  37.         return Optional.ofNullable(dnum).isPresent();
  38.     }
  39.     /**
  40.      * 判断输入的Object是否为空
  41.      * @param obj
  42.      * @return
  43.      */
  44.     public static boolean isEmpty(Object obj){
  45.         return Optional.ofNullable(obj).isPresent();
  46.         
  47.     }
  48.     
  49.     /**
  50.      * 判断输入的List是否为空,获取该对象
  51.      * @param obj
  52.      * @return
  53.      */
  54.     public static Object get(Object obj){
  55.         return Optional.ofNullable(obj).isPresent()!=false?Optional.ofNullable(obj).get():null;
  56.         
  57.     }
  58.     /**
  59.      * 测试
  60.      * @param args
  61.      */
  62.     @SuppressWarnings("unchecked")
  63.     public static void main(String[] args) {
  64.         List<String> list = new ArrayList<>();
  65.         list.add("a");
  66.         list.add("b");
  67.         list.add("c");
  68.         List<String> objList = (List<String>) OptionalUtil.get(list);
  69.         objList.forEach(item->{System.out.println(item);});
  70.         
  71.         String str = null;
  72.         System.out.println("当前对象:"+OptionalUtil.get(str));
  73.         
  74.         
  75.     }
  76. }

 1. 关于Java8工具类Optional的一点使用心得
??对于Java开发中遇到的npe(空指针问题),其实使用工具类Optional处理效果一般,个人感觉最大的用处就是可以减少一定的if语句,减少一定的非空判断,千万不能将Optional放在if内,会非常多此一举。

个人觉得最实用的例子:
??如果获取不到student的name属性(为空),那么将使用student的rename属性,即使rename、name为空,也不会出现空指针异常。

点击(此处)折叠或打开

  1. import lombok.Data;
  2. import java.util.Optional;

  3. public class qwe {
  4.     //java fhadmin.cn
  5.     public static void main(String[] args) {
  6.         Student student = new Student();
  7.         student.setReName("tom");
  8.         Student student1 = new Student();
  9.         student1.setName( Optional.ofNullable(student.getName()).orElseGet(student::getReName)); //
  10.         System.out.println(student1);
  11.     }
  12.     @Data
  13.     static class Student{
  14.         String name;
  15.         String reName;
  16.     }
  17. }
2. 工具类

点击(此处)折叠或打开

  1. package com.ven.utils;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Optional;
  6. /**
  7.  * java 8 Optional 工具类
  8.  * @author fhadmin
  9.  * @from fhadmin.cn
  10.  * @ version 1.0
  11.  */
  12. public class OptionalUtil {
  13.  
  14.     /**
  15.      * 判断输入的字符串是否为空
  16.      * @param str String
  17.      * @return
  18.      */
  19.     public static boolean isEmpty(String str){
  20.         return Optional.ofNullable(str).isPresent();
  21.     }
  22.     
  23.     /**
  24.      * 判断输入的Integer是否为空
  25.      * @param num Integer
  26.      * @return
  27.      */
  28.     public static boolean isEmpty(Integer num){
  29.         return Optional.ofNullable(num).isPresent();
  30.     }
  31.     /**
  32.      * 判断输入的Double是否为空
  33.      * @param dnum Double
  34.      * @return
  35.      */
  36.     public static boolean isEmpty(Double dnum){
  37.         return Optional.ofNullable(dnum).isPresent();
  38.     }
  39.     /**
  40.      * 判断输入的Object是否为空
  41.      * @param obj
  42.      * @return
  43.      */
  44.     public static boolean isEmpty(Object obj){
  45.         return Optional.ofNullable(obj).isPresent();
  46.         
  47.     }
  48.     
  49.     /**
  50.      * 判断输入的List是否为空,获取该对象
  51.      * @param obj
  52.      * @return
  53.      */
  54.     public static Object get(Object obj){
  55.         return Optional.ofNullable(obj).isPresent()!=false?Optional.ofNullable(obj).get():null;
  56.         
  57.     }
  58.     /**
  59.      * 测试
  60.      * @param args
  61.      */
  62.     @SuppressWarnings("unchecked")
  63.     public static void main(String[] args) {
  64.         List<String> list = new ArrayList<>();
  65.         list.add("a");
  66.         list.add("b");
  67.         list.add("c");
  68.         List<String> objList = (List<String>) OptionalUtil.get(list);
  69.         objList.forEach(item->{System.out.println(item);});
  70.         
  71.         String str = null;
  72.         System.out.println("当前对象:"+OptionalUtil.get(str));
  73.         
  74.         
  75.     }
  76. }


点击(此处)折叠或打开

  1. package com.ven.utils;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Optional;
  6. /**
  7.  * java 8 Optional 工具类
  8.  * @author fhadmin
  9.  * @from fhadmin.cn
  10.  * @ version 1.0
  11.  */
  12. public class OptionalUtil {
  13.  
  14.     /**
  15.      * 判断输入的字符串是否为空
  16.      * @param str String
  17.      * @return
  18.      */
  19.     public static boolean isEmpty(String str){
  20.         return Optional.ofNullable(str).isPresent();
  21.     }
  22.     
  23.     /**
  24.      * 判断输入的Integer是否为空
  25.      * @param num Integer
  26.      * @return
  27.      */
  28.     public static boolean isEmpty(Integer num){
  29.         return Optional.ofNullable(num).isPresent();
  30.     }
  31.     /**
  32.      * 判断输入的Double是否为空
  33.      * @param dnum Double
  34.      * @return
  35.      */
  36.     public static boolean isEmpty(Double dnum){
  37.         return Optional.ofNullable(dnum).isPresent();
  38.     }
  39.     /**
  40.      * 判断输入的Object是否为空
  41.      * @param obj
  42.      * @return
  43.      */
  44.     public static boolean isEmpty(Object obj){
  45.         return Optional.ofNullable(obj).isPresent();
  46.         
  47.     }
  48.     
  49.     /**
  50.      * 判断输入的List是否为空,获取该对象
  51.      * @param obj
  52.      * @return
  53.      */
  54.     public static Object get(Object obj){
  55.         return Optional.ofNullable(obj).isPresent()!=false?Optional.ofNullable(obj).get():null;
  56.         
  57.     }
  58.     /**
  59.      * 测试
  60.      * @param args
  61.      */
  62.     @SuppressWarnings("unchecked")
  63.     public static void main(String[] args) {
  64.         List<String> list = new ArrayList<>();
  65.         list.add("a");
  66.         list.add("b");
  67.         list.add("c");
  68.         List<String> objList = (List<String>) OptionalUtil.get(list);
  69.         objList.forEach(item->{System.out.println(item);});
  70.         
  71.         String str = null;
  72.         System.out.println("当前对象:"+OptionalUtil.get(str));
  73.         
  74.         
  75.     }
  76. }

点击(此处)折叠或打开


  1. package com.ven.utils;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Optional;
  6. /**
  7.  * java 8 Optional 工具类
  8.  * @author fhadmin
  9.  * @from fhadmin.cn
  10.  * @ version 1.0
  11.  */
  12. public class OptionalUtil {
  13.  
  14.     /**
  15.      * 判断输入的字符串是否为空
  16.      * @param str String
  17.      * @return
  18.      */
  19.     public static boolean isEmpty(String str){
  20.         return Optional.ofNullable(str).isPresent();
  21.     }
  22.     
  23.     /**
  24.      * 判断输入的Integer是否为空
  25.      * @param num Integer
  26.      * @return
  27.      */
  28.     public static boolean isEmpty(Integer num){
  29.         return Optional.ofNullable(num).isPresent();
  30.     }
  31.     /**
  32.      * 判断输入的Double是否为空
  33.      * @param dnum Double
  34.      * @return
  35.      */
  36.     public static boolean isEmpty(Double dnum){
  37.         return Optional.ofNullable(dnum).isPresent();
  38.     }
  39.     /**
  40.      * 判断输入的Object是否为空
  41.      * @param obj
  42.      * @return
  43.      */
  44.     public static boolean isEmpty(Object obj){
  45.         return Optional.ofNullable(obj).isPresent();
  46.         
  47.     }
  48.     
  49.     /**
  50.      * 判断输入的List是否为空,获取该对象
  51.      * @param obj
  52.      * @return
  53.      */
  54.     public static Object get(Object obj){
  55.         return Optional.ofNullable(obj).isPresent()!=false?Optional.ofNullable(obj).get():null;
  56.         
  57.     }
  58.     /**
  59.      * 测试
  60.      * @param args
  61.      */
  62.     @SuppressWarnings("unchecked")
  63.     public static void main(String[] args) {
  64.         List<String> list = new ArrayList<>();
  65.         list.add("a");
  66.         list.add("b");
  67.         list.add("c");
  68.         List<String> objList = (List<String>) OptionalUtil.get(list);
  69.         objList.forEach(item->{System.out.println(item);});
  70.         
  71.         String str = null;
  72.         System.out.println("当前对象:"+OptionalUtil.get(str));
  73.         
  74.         
  75.     }
  76. }


阅读(5189) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~