Java-java8 Comparator排序时字段值为空放到最前或者最后
项目中碰到需要对List中的实体类Students(id,name,age)进行排序,年龄从大到小进行排序,为NUll排到最后
原有数组 为[1, null, 5, null, null, 3, 6, null, 4]
经过排序后为[1, 3, 4, 5, 6, null, null, null, null]
实现compare方法即可,该方法的参数是需要比较的两个数据(Integer o1,Integer o2)
如果两者都不为null,就直接进行数字比较;其中如果有一个为null,就将null排到后面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 @org .junit.Test public void testT () { List<Integer> list = new ArrayList<Integer>(); list.add(1 ); list.add(null ); list.add(5 ); list.add(null ); list.add(null ); list.add(3 ); list.add(6 ); list.add(null ); list.add(4 ); System.out.println("之前" + list); Collections.sort(list, new Comparator<Integer>() { @Override public int compare (Integer o1, Integer o2) { if (o1 != null && o2 != null ) { return o1.compareTo(o2); } else { return o1 == null ? 1 : -1 ; } } }); System.out.println("之后" + list); }
使用Stream进行排序 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 List<Integer> list = new ArrayList<Integer>(); list.add(1 ); list.add(null ); list.add(5 ); list.add(null ); list.add(null ); list.add(3 ); list.add(6 ); list.add(null ); list.add(4 ); list.sort(Comparator.nullsLast(Integer::compareTo)); System.out.println("list = " + list); List<String> names2 = Arrays.asList("XML" , null , "Java" , "HTML" , "CSS" ); names2.sort(Comparator.nullsLast(String::compareTo)); System.out.println(names2);
Java-找出子集中不同的元素: 1 2 3 4 5 6 7 8 9 public static void findDifference (List<String> listA,List<String>listB) { List<String> delTagGroup = new ArrayList<>(); listA.removeIf(s -> listB.contains(s)); }
测试:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 List<String> list =new ArrayList<>(); list.add("ZYF62CFA5ED6" ); String s= getInCond(list); System.out.println(s); ArrayList<String> listA = new ArrayList<>(); listA.add("1" ); listA.add("2" ); ArrayList<String> listB = new ArrayList<>(); listB.add("1" ); listB.add("3" ); listB.add("4" ); findDifference(listA,listB); System.out.println(listA.toString());
结果:
Java-文件配置工具类: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 package com.jfinal.kit;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.util.Properties;import com.jfinal.core.Const;public class Prop { protected Properties properties; public Prop () { properties = new Properties(); } public Prop (String fileName) { this (fileName, Const.DEFAULT_ENCODING); } public Prop (String fileName, String encoding) { InputStream inputStream = null ; try { inputStream = getClassLoader().getResourceAsStream(fileName); if (inputStream == null ) { throw new IllegalArgumentException("Properties file not found in classpath: " + fileName); } properties = new Properties(); properties.load(new InputStreamReader(inputStream, encoding)); } catch (IOException e) { throw new RuntimeException("Error loading properties file." , e); } finally { if (inputStream != null ) try {inputStream.close();} catch (IOException e) {LogKit.error(e.getMessage(), e);} } } private ClassLoader getClassLoader () { ClassLoader ret = Thread.currentThread().getContextClassLoader(); return ret != null ? ret : getClass().getClassLoader(); } public Prop (File file) { this (file, Const.DEFAULT_ENCODING); } public Prop (File file, String encoding) { if (file == null ) { throw new IllegalArgumentException("File can not be null." ); } if (!file.isFile()) { throw new IllegalArgumentException("File not found : " + file.getName()); } InputStream inputStream = null ; try { inputStream = new FileInputStream(file); properties = new Properties(); properties.load(new InputStreamReader(inputStream, encoding)); } catch (IOException e) { throw new RuntimeException("Error loading properties file." , e); } finally { if (inputStream != null ) try {inputStream.close();} catch (IOException e) {LogKit.error(e.getMessage(), e);} } } public Prop append (Prop prop) { if (prop == null ) { throw new IllegalArgumentException("prop can not be null" ); } properties.putAll(prop.getProperties()); return this ; } public Prop append (String fileName, String encoding) { return append(new Prop(fileName, encoding)); } public Prop append (String fileName) { return append(fileName, Const.DEFAULT_ENCODING); } public Prop appendIfExists (String fileName, String encoding) { try { return append(new Prop(fileName, encoding)); } catch (Exception e) { return this ; } } public Prop appendIfExists (String fileName) { return appendIfExists(fileName, Const.DEFAULT_ENCODING); } public Prop append (File file, String encoding) { return append(new Prop(file, encoding)); } public Prop append (File file) { return append(file, Const.DEFAULT_ENCODING); } public Prop appendIfExists (File file, String encoding) { if (file.isFile()) { append(new Prop(file, encoding)); } return this ; } public Prop appendIfExists (File file) { return appendIfExists(file, Const.DEFAULT_ENCODING); } public String get (String key) { String value = properties.getProperty(key); return value != null && value.length() != 0 ? value.trim() : null ; } public String get (String key, String defaultValue) { String value = properties.getProperty(key); return value != null && value.length() != 0 ? value.trim() : defaultValue; } public Integer getInt (String key) { return getInt(key, null ); } public Integer getInt (String key, Integer defaultValue) { String value = properties.getProperty(key); if (value != null ) { return Integer.parseInt(value.trim()); } return defaultValue; } public Long getLong (String key) { return getLong(key, null ); } public Long getLong (String key, Long defaultValue) { String value = properties.getProperty(key); if (value != null ) { return Long.parseLong(value.trim()); } return defaultValue; } public Double getDouble (String key) { return getDouble(key, null ); } public Double getDouble (String key, Double defaultValue) { String value = properties.getProperty(key); if (value != null ) { return Double.parseDouble(value.trim()); } return defaultValue; } public Boolean getBoolean (String key) { return getBoolean(key, null ); } public Boolean getBoolean (String key, Boolean defaultValue) { String value = properties.getProperty(key); if (value != null ) { value = value.toLowerCase().trim(); if ("true" .equals(value)) { return true ; } else if ("false" .equals(value)) { return false ; } throw new RuntimeException("The value can not parse to Boolean : " + value); } return defaultValue; } public boolean containsKey (String key) { return properties.containsKey(key); } public boolean isEmpty () { return properties.isEmpty(); } public boolean notEmpty () { return ! properties.isEmpty(); } public Properties getProperties () { return properties; } }
Java-参数校验框架 title: 参数校验框架 categories:
Hibernate Validator 使用介绍 - 简书 (jianshu.com)
hibernate validator 正则表达式报错javax.validation.constraints.Pattern-左搜 (leftso.com)
Java-Integer和int之间的转换 int到Integer:
int a=3;
Integer A=new Integer(a);
或:
Integer A=Integer.valueOf(a);
Integer到int:
Integer A=new Integer(5);
int a=A.intValue();
至于Integer.parseInt(String str)则是将String类型转为int类型。
int类型是放在栈空间的,Integer是作为对象放在堆空间的;
int 是基本类型,不是类,为了符合面向对象编程,后来出现了Integer 类,他是对int进行封装的。
int不是对象,是java原始的数据类型,它默认值为0。
Integer是个对象,它有自己的方法,默认值为NULL。
实现这种对象包装的目的主要是因为类能够提供必要的方法,用于实现基本数据类型的数值与可打印字符串之间的转换,以及一些其他的实用程序方法; 另外,有些数据结构库类只能操作对象,而不支持基本数据类型的变量,包装类提供一种便利的方式,能够把基本数据类型转换成等价的对象,从而可以利用数据结构库类进行处理。
有时候list和数组int[]转换很麻烦。
List和String[]也同理。难道每次非得写一个循环遍历吗?其实一步就可以搞定。
本文涉及到一些Java8的特性。如果没有接触过就先学会怎么用,然后再细细研究。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 import java.util.Arrays;import java.util.List;import java.util.stream.Collectors; public class Main { public static void main (String[] args) { int [] data = {4 , 5 , 3 , 6 , 2 , 5 , 1 }; List<Integer> list1 = Arrays.stream(data).boxed().collect(Collectors.toList()); Integer[] integers1 = Arrays.stream(data).boxed().toArray(Integer[]::new ); Integer[] integers2 = list1.toArray(new Integer[0 ]); int [] arr1 = list1.stream().mapToInt(Integer::valueOf).toArray(); int [] arr2 = Arrays.stream(integers1).mapToInt(Integer::valueOf).toArray(); List<Integer> list2 = Arrays.asList(integers1); String[] strings1 = {"a" , "b" , "c" }; List<String> list3 = Arrays.asList(strings1); String[] strings2 = list3.toArray(new String[0 ]); } }
打印数组
1 2 3 4 5 6 7 8 9 Object[] paramValues; for (int i = 0 ; i < paramValues.length; i++) { System.out.print(paramValues[i] + ", " ); } for (Object n: paramValues) System.out.println(n+", " ); System.out.println( Arrays.toString(paramValues) ); System.out.println(Arrays.asList(paramValues)); Arrays.asList(arr).stream().forEach(s -> System.out.println(s));
Java-String转Date (3条消息) Java String转Date_n1355553344的博客-CSDN博客_java string转date
Java-空指针异常如何解决: 1、 判断list是否为空时,应该先判断是否为空:
https://blog.csdn.net/qq_35318169/article/details/116207552?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~default-1.no_search_link&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~default-1.no_search_link )
Java-json数据解析 java对象与Json字符串之间的转化(fastjson) - 冰湖一角 - 博客园 (cnblogs.com)
https://blog.csdn.net/qq_36582257/article/details/109241548
SpringBoot-JSON忽略为 null 的属性字段 在返回的类上添加注解: @JsonInclude(JsonInclude.Include.NON_NULL)
该注解可以将实体类中的null的属性自动忽略。 Springboot 项目可以修改 application.yml 文件配置全局自动忽略:
【Spring Boot-技巧】API返回值去除为NULL的字段 - 神秘人2332 - 博客园 (cnblogs.com)
数据库-sqlsever 分页查询: (3条消息) SQL server分页的四种方法(算很全面了)_kanlon的博客-CSDN博客_sql 分页
数据库-sqlsever 版本低 修改JDK版本
(17条消息) JDK版本之TLS协议导致项目报错_后会无期-CSDN博客