1.JsonProperty的使用
public class ProductVO {
@JsonProperty("name")
private String categoryName;
}
- 如果前端需要返回name,而后端避免有歧义,不想叫name可以使用JsonProperty来转换
2.Java8新特性之forEach+Lambda 表达式遍历Map和List
一、遍历Map
============Java8之前的方式==========
Map<String, Integer> items = new HashMap<>();
items.put("A", 10);
items.put("B", 20);
items.put("C", 30);
items.put("D", 40);
items.put("E", 50);
items.put("F", 60);
for (Map.Entry<String, Integer> entry : items.entrySet()) {
System.out.println("Item : " + entry.getKey() + " Count : " + entry.getValue());
}
============forEach + Lambda表达式==========
Map<String, Integer> items = new HashMap<>();
items.put("A", 10);
items.put("B", 20);
items.put("C", 30);
items.put("D", 40);
items.put("E", 50);
items.put("F", 60);
items.forEach((k,v)->System.out.println("Item : " + k + " Count : " + v));
items.forEach((k,v)->{
System.out.println("Item : " + k + " Count : " + v);
if("E".equals(k)){
System.out.println("Hello E");
}
});
二、遍历List:
============Java8之前的方式==========
List<String> items = new ArrayList<>();
items.add("A");
items.add("B");
items.add("C");
items.add("D");
items.add("E");
for(String item : items){
System.out.println(item);
}
============forEach + Lambda表达式==========
List<String> items = new ArrayList<>();
items.add("A");
items.add("B");
items.add("C");
items.add("D");
items.add("E");
//输出:A,B,C,D,E
items.forEach(item->System.out.println(item));
//输出 : C
items.forEach(item->{
if("C".equals(item)){
System.out.println(item);
}
});
三、去重
List<Order> list = new ArrayList<User>();
Order o1 = new Order("1","MCS-2019-1123");
list.add(o1 );
Order o2= new Order("2","MCS-2019-1124");
list.add(o2);
Order o3= new Order("3","MCS-2019-1125");
list.add(o3);
List<String> orderNoList=list.stream().map(User::getOrderNo).collect(Collectors.toList());
System.out.println("输出单号集合:"+orderNoList);
List<String> idList=list.stream().map(Order::getId()).collect(Collectors.toList());
System.out.println(idList)
结果
输出第一个:["MCS-2019-1123", "MCS-2019-1124", "MCS-2019-1125"]
[1, 2, 3]