java成神之——properties,lambda表达式,序列化

2021-05-17 21:29

阅读:520

标签:函数式   tsp   imp   ret   creat   stc   sys   文件   des   

  • 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");

testing properties with xmlgreenSteve23

读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 p = o -> o.isEmpty();            // 返回值类型必须是布尔值
Function f = o -> o.isEmpty();    // 返回值类型可以自定义
Consumer c = o -> System.out.println(o);   // 返回值类型为void
c.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...
}

// 序列化成json
User 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成神的系列文章之一

如果你想知道,但是本文没有的,请下方留言

我会第一时间总结出来并发布填充到本文

java成神之——properties,lambda表达式,序列化

标签:函数式   tsp   imp   ret   creat   stc   sys   文件   des   

原文地址:https://www.cnblogs.com/ye-hcj/p/9745975.html


评论


亲,登录后才可以留言!