Properties
加载 defaults.properties 文件
写 Properties 到 xml 文件
读 Properties 从 xml 文件
Lambda 表达式
自定义
内置
sort 方法中使用 Lambada
序列化
文件序列化
Gson 序列化
Jackson 序列化
Comparable 和 Comparator
Comparable 对象排序
Comparator 对象排序
结语
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");
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
- <properties>
- <comment>testing properties with xml</comment>
- <entry key="color">green</entry>
- <entry key="name">Steve</entry>
- <entry key="age">23</entry>
- </properties>
读 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 方法, 可以有多个重写对象的方法
- @FunctionalInterface
- interface MyFunctionalInterface {
- void fn();
- }
- MyFunctionalInterface mfi = () -> System.out.println("函数式接口");
- mfi.fn();
等价于
- MyFunctionalInterface mfi = new MyFunctionalInterface() {
- @Override
- public void fn() {
- System.out.println("函数式接口");
- }
- };
内置
- Predicate<String> p = o -> o.isEmpty(); // 返回值类型必须是布尔值
- Function<String, Boolean> f = o -> o.isEmpty(); // 返回值类型可以自定义
- Consumer<String> c = o -> System.out.println(o); // 返回值类型为 void
- c.accept("没有返回值");
sort 方法中使用 Lambada
原始写法
- List<Integer> list = new ArrayList<>();
- list.add(3);
- list.add(1);
- list.add(2);
- Collections.sort(list, new Comparator<Integer>(){
- 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 序列化
- <dependency>
- <groupId>com.google.code.gson</groupId>
- <artifactId>gson</artifactId>
- <version>2.8.5</version>
- </dependency>
- public class User {
- private Integer id;
- private String name;
- public User(Integer id, String name) {
- this.id = id;
- this.name = name;
- }
- getter...
- setter...
- }
- // 序列化成 JSON
- User user = new User(1, "小李");
- Gson gson = new Gson();
- String JSON = gson.toJson(user);
- // 反序列化
- User userCopy = gson.fromJson(JSON, User.class);
Jackson 序列化
依赖
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-databind</artifactId>
- <version>2.9.6</version>
- </dependency>
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<User> {
- 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<User> list = Arrays.asList(new User(2, "小李"), new User(3, "小李"), new User(1, "小李"));
- Collections.sort(list);
Comparator 对象排序
- List<User> list = Arrays.asList(new User(2, "小李"), new User(3, "小张"), new User(1, "小王"));
- Collections.sort(list, new Comparator<User>() {
- @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 成神的系列文章之一
如果你想知道, 但是本文没有的, 请下方留言
我会第一时间总结出来并发布填充到本文
来源: http://www.bubuko.com/infodetail-2795915.html