Properties
加载defaults.properties文件
defaults.properties内容如下 lastname=Smith获取properties属性(defaults.properties文件和TestController文件置于同级目录) try (InputStream bundledResource = TestController.class.getResourceAsStream("defaults.properties")) { Properties defaults = new Properties(); defaults.load(bundledResource); return defaults; } catch (IOException e) { throw new UncheckedIOException( "defaults.properties not properly packaged" + " with application", e); }
写Properties到xml文件
Properties prop = new Properties();prop.setProperty("name", "Steve");prop.setProperty("color", "green");prop.setProperty("age", "23");File file = new File("C:\\Users\\26401\\Desktop\\defaults.properties");if (!file.exists()){ file.createNewFile();}prop.storeToXML(new FileOutputStream(file), "testing properties with xml"); testing properties with xml green Steve 23
读Properties从xml文件
Properties prop = new Properties();File file = new File("C:\\Users\\26401\\Desktop\\defaults.properties");if (file.exists()){ prop.loadFromXML(new FileInputStream(file)); for (String name : prop.stringPropertyNames()){ System.out.println(name + "=" + prop.getProperty(name)); }}else { System.err.println("Error: No file found at: " + file);}
Lambda表达式
自定义
Lambda表达式只能用于函数式接口函数式接口只能包含一个抽象方法,可以有多个default和static方法,可以有多个重写对象的方法@FunctionalInterfaceinterface MyFunctionalInterface { void fn();}MyFunctionalInterface mfi = () -> System.out.println("函数式接口");mfi.fn();等价于MyFunctionalInterface mfi = new MyFunctionalInterface() { @Override public void fn() { System.out.println("函数式接口"); }};
内置
Predicate p = o -> o.isEmpty(); // 返回值类型必须是布尔值Function f = o -> o.isEmpty(); // 返回值类型可以自定义Consumer c = o -> System.out.println(o); // 返回值类型为voidc.accept("没有返回值");
sort方法中使用Lambada
原始写法List list = new ArrayList<>();list.add(3);list.add(1);list.add(2);Collections.sort(list, new Comparator (){ public int compare(Integer b, Integer l){ return b.compareTo(l); }}); // [1,2,3]Lambada写法Collections.sort(list, (b, l) -> b.compareTo(l));或者Collections.sort(list, Comparator.comparing(Integer::valueOf));
序列化
文件序列化
public class SerialClass implements Serializable { private static final long serialVersionUID = 1L;}
Gson序列化
com.google.code.gson gson 2.8.5 public class User { private Integer id; private String name; public User(Integer id, String name) { this.id = id; this.name = name; } getter... setter...}// 序列化成jsonUser user = new User(1, "小李");Gson gson = new Gson();String json = gson.toJson(user);// 反序列化User userCopy = gson.fromJson(json, User.class);
Jackson序列化
依赖 com.fasterxml.jackson.core jackson-databind 2.9.6 json字符串转对象 ObjectMapper objectMapper = new ObjectMapper(); User outputObject = objectMapper.readValue( "{\"id\":\"1\",\"name\":\"小叶\"}", User.class); outputObject.getName(); @JsonIgnoreProperties(ignoreUnknown = true) // 忽视反序列化遇到的不认识的属性 public class User { ... }对象转字符串 User user = new User(1, "小李"); ObjectMapper objectMapper = new ObjectMapper(); String json = objectMapper.writeValueAsString(user);
Comparable和Comparator
Comparable对象排序
public class User implements Comparable { private Integer id; private String name; public User(Integer id, String name) { this.id = id; this.name = name; } public void setId(Integer id) { this.id = id; } public Integer getId() { return id; } public void setName(String name) { this.name = name; } public String getName() { return name; } @Override public boolean equals(Object o) { if (! (o instanceof User)) return false; User p = (User)o; return id.equals(p.id) && name.equals(p.name); } @Override public int hashCode() { return Objects.hash(id, name); } @Override public int compareTo(User other) { int idCompare = id.compareTo(other.id); if (idCompare != 0) { return idCompare; } else { return id.compareTo(other.id); } }}List list = Arrays.asList(new User(2, "小李"), new User(3, "小李"), new User(1, "小李"));Collections.sort(list);
Comparator对象排序
List list = Arrays.asList(new User(2, "小李"), new User(3, "小张"), new User(1, "小王"));Collections.sort(list, new Comparator () { @Override public int compare(User u1, User u2) { return u1.getId().compareTo(u2.getId()); }});Collections.sort(list,(u1, u2) -> { return u1.getId().compareTo(u2.getId());});Collections.sort(list,Comparator.comparing(User::getId).thenComparing(User::getName));
结语
本文章是java成神的系列文章之一如果你想知道,但是本文没有的,请下方留言我会第一时间总结出来并发布填充到本文