标签:key mon sys font err static 一个 import stat
1 for循环 2 import com.google.common.base.Function; 3 import com.google.common.collect.Maps; 4 5 import java.util.ArrayList; 6 import java.util.HashMap; 7 import java.util.List; 8 import java.util.Map; 9 10 public class ListToMap { 11 public static void main(String[] args) { 12 List<User> userList = new ArrayList<>(); 13 User user1 = new User(); 14 user1.setId(1L); 15 user1.setAge("12"); 16 17 User user2 = new User(); 18 user2.setId(2L); 19 user2.setAge("13"); 20 21 userList.add(user1); 22 userList.add(user2); 23 24 Map<Long, User> maps = new HashMap<>(); 25 for (User user : userList) { 26 maps.put(user.getId(), user); 27 } 28 29 System.out.println(maps); 30 31 } 32 33 public static class User { 34 private Long id; 35 private String age; 36 37 public Long getId() { 38 return id; 39 } 40 41 public void setId(Long id) { 42 this.id = id; 43 } 44 45 public String getAge() { 46 return age; 47 } 48 49 public void setAge(String age) { 50 this.age = age; 51 } 52 53 @Override 54 public String toString() { 55 return "User{" + 56 "id=" + id + 57 ", age=‘" + age + ‘\‘‘ + 58 ‘}‘; 59 } 60 } 61 } 62 63 64 126 使用guava 127 Map<Long, User> maps = Maps.uniqueIndex(userList, new Function<User, Long>() { 128 @Override 129 public Long apply(User user) { 130 return user.getId(); 131 } 132 });
139 使用JDK1.8 140 Map<Long, User> maps = userList.stream().collect(Collectors.toMap(User::getId,Function.identity())); 141 1 142 看来还是使用JDK 1.8方便一些。另外,转换成map的时候,可能出现key一样的情况,如果不指定一个覆盖规则,上面的代码是会报错的。转成map的时候,最好使用下面的方式: 143 144 Map<Long, User> maps = userList.stream().collect(Collectors.toMap(User::getId, Function.identity(), (key1, key2) -> key2)); 145 148 有时候,希望得到的map的值不是对象,而是对象的某个属性,那么可以用下面的方式: 149 150 Map<Long, String> maps = userList.stream().collect(Collectors.toMap(User::getId, User::getAge, (key1, key2) -> key2));
for循环import com.google.common.base.Function;import com.google.common.collect.Maps;
import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;
public class ListToMap { public static void main(String[] args) { List<User> userList = new ArrayList<>(); User user1 = new User(); user1.setId(1L); user1.setAge("12");
User user2 = new User(); user2.setId(2L); user2.setAge("13");
userList.add(user1); userList.add(user2);
Map<Long, User> maps = new HashMap<>(); for (User user : userList) { maps.put(user.getId(), user); }
System.out.println(maps);
}
public static class User { private Long id; private String age;
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getAge() { return age; }
public void setAge(String age) { this.age = age; }
@Override public String toString() { return "User{" + "id=" + id + ", age=‘" + age + ‘\‘‘ + ‘}‘; } }}
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162使用guava Map<Long, User> maps = Maps.uniqueIndex(userList, new Function<User, Long>() { @Override public Long apply(User user) { return user.getId(); } });123456使用JDK1.8Map<Long, User> maps = userList.stream().collect(Collectors.toMap(User::getId,Function.identity()));1看来还是使用JDK 1.8方便一些。另外,转换成map的时候,可能出现key一样的情况,如果不指定一个覆盖规则,上面的代码是会报错的。转成map的时候,最好使用下面的方式:
Map<Long, User> maps = userList.stream().collect(Collectors.toMap(User::getId, Function.identity(), (key1, key2) -> key2));
12有时候,希望得到的map的值不是对象,而是对象的某个属性,那么可以用下面的方式:
Map<Long, String> maps = userList.stream().collect(Collectors.toMap(User::getId, User::getAge, (key1, key2) -> key2));
标签:key mon sys font err static 一个 import stat
原文地址:https://www.cnblogs.com/MarchVivian/p/14728792.html