文章目录
- 一句话
- 场景
- 语法测试
- 关键语句截图
- Source Code
- 控制台输出
一句话
List<User>
--> Map<User对象中欲作为key的字段的类型, User>
List<User> userList = getUserList();
Map<Integer, User> idAndUserMap = userList.stream().collect(Collectors.toMap(User::getId, Function.identity()));
场景
- MyBatis 查询返回List很容易,但如何返回Map?
- Hibernate 查询返回List很容易,但如何返回Map?
- 如何使用 Java Stream API 将 List 转换为 Map?
先说1,经查 Mybatis looks up how to return data of Map and List collection types
Mybatis中提供注解:org.apache.ibatis.annotations.MapKey
你需要做的,就是把本来的 DAO 层方法的返回类型从 List 修改为 Map,并且在其上加上注解 @MapKey("你想拿来当key的字段的字段名称")
public interface UserMapper {
@Select("select id, name, address from users")
List<User> selectUserList();
@MapKey("id")
@Select("select id, name, address from users")
Map<Integer, User> getUserIdAndUserMap();
}
再说2,经历过1之后,我想 hibernate 是否也有原生支持的API?奈何,他也许有,但我至今没查到。
于是就找到了3,3的解决方式可以作为场景1和2的通解,且在其他更多场景也可自由应用。
所以详细记录3。
语法测试
关键语句截图
Source Code
List2MapDemo.java
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
public class List2MapDemo {
public static void main(String[] args) {
System.out.println("\r\nList<YourObjectType> :");
List<User> userList = getUserList();
userList.forEach(currentElement -> {
System.out.printf("element: %s\r\n", currentElement);
});
System.out.println("\r\nMap<IdType, YourObjectType> :");
Map<Integer, User> idAndUserMap = userList.stream().collect(Collectors.toMap(User::getId, Function.identity()));
Set<Integer> idSet = idAndUserMap.keySet();
idSet.forEach(currentId -> {
System.out.printf("key: %s value: %s\r\n", currentId, idAndUserMap.get(currentId));
});
System.out.println("\r\nMap<IdType, ValueType> :");
Map<Integer, String> idAndNameMap = userList.stream().collect(Collectors.toMap(User::getId, User::getName));
Set<Integer> idSet2 = idAndNameMap.keySet();
idSet2.forEach(currentId -> {
System.out.printf("key: %s value: %s\r\n", currentId, idAndNameMap.get(currentId));
});
}
static List<User> getUserList() {
List<User> userList = new ArrayList<>(5);
userList.add(new User(1, "one", "address-1"));
userList.add(new User(2, "two", "address-2"));
userList.add(new User(3, "three", "address-3"));
userList.add(new User(4, "four", "address-4"));
userList.add(new User(5, "five", "address-5"));
return userList;
}
}
class User {
private Integer id;
private String name;
private String address;
public User(){};
public User(Integer id, String name, String address){
super();
this.id = id;
this.name = name;
this.address = address;
};
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", address='" + address + '\'' +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
控制台输出
List<YourObjectType> :
element: User{id=1, name='one', address='address-1'}
element: User{id=2, name='two', address='address-2'}
element: User{id=3, name='three', address='address-3'}
element: User{id=4, name='four', address='address-4'}
element: User{id=5, name='five', address='address-5'}
Map<IdType, YourObjectType> :
key: 1 value: User{id=1, name='one', address='address-1'}
key: 2 value: User{id=2, name='two', address='address-2'}
key: 3 value: User{id=3, name='three', address='address-3'}
key: 4 value: User{id=4, name='four', address='address-4'}
key: 5 value: User{id=5, name='five', address='address-5'}
Map<IdType, ValueType> :
key: 1 value: one
key: 2 value: two
key: 3 value: three
key: 4 value: four
key: 5 value: five