1 List -> Map


設個User類:

  1. public class User {
  2. private String userName;
  3. private String userId;
  4. private String userInfo;
  5. public User(){}
  6. public User(String userName, String userId, String userInfo) {
  7. this.userName = userName;
  8. this.userId = userId;
  9. this.userInfo = userInfo;
  10. }
  11. //getter setter
  12. }

1)foreach()方法

  1. //foreach
  2. public Map<String,User> ltmForEach(List<User> list){
  3. Map<String,User> map = new HashMap<>();
  4. list.forEach(user -> {
  5. map.put(user.getUserId(), user);
  6. });
  7. return map;
  8. }

2)collect()方法

  1. //collect()
  2. public Map<String,User> ltmStream(List<User> list){
  3. /**
  4. * Collectors.toMap()方法引數:
  5. * keyMapper: User::getUserId,呼叫User的getter方法
  6. * valueMapper: user->user, User類作為value
  7. * mergeFunction: 當key衝突時,value合併的方法:(n1,n2)->n2為替換,(n1,n2)->n1+n2為累加
  8. * mapSupplier: Map構造器,需要返回特定Map的時候使用,如TreeMap::new返回以key排序的Map
  9. */
  10. return list.stream()
  11. .collect(Collectors.toMap(User::getUserId,user->user,(n1,n2)->n2,TreeMap::new));
  12. }

3)Map常用遍歷方法

  1. // 1. 增強for迴圈
  2. Map<String, User> ltmStreamMap = l.ltmStream(list);
  3. for(Map.Entry entry : ltmStreamMap.entrySet()){
  4. System.out.println(entry.getKey()+" : "+entry.getValue());
  5. }
  6. // 2. Iterator迭代器
  7. Map<String, User> ltmForEachMap = l.ltmForEach(list);
  8. Iterator<Map.Entry<String, User>> iterator = ltmForEachMap.entrySet().iterator();
  9. while(iterator.hasNext()){
  10. Map.Entry<String, User> next = iterator.next();
  11. System.out.println(next.getKey()+" : "+next.getValue());
  12. }
  13. // 3. 內迴圈
  14. Map<String, User> ltmForEachMap = l.ltmForEach(list);
  15. ltmForEachMap.forEach((k,v)->{
  16. System.out.println(k+" : "+v);
  17. });

2 Array -> List


Array指陣列型別資料,如:String[]、Object[]、int[]等

一般使用工具類Arrays的asList方法:

  1. public class arrayToList {
  2. public static void main(String[] args) {
  3. String[] strs = new String[]{"a","b","c"};
  4. List<String> list = Arrays.asList(strs);
  5. for(String str : list){
  6. System.out.println(str);
  7. }
  8. }
  9. }

3 List -> Array


集合List轉為陣列型別Array

通常使用Collection的toArray方法

  1. public class listToArray {
  2. public static void main(String[] args) {
  3. List<String> list = new ArrayList<>();
  4. list.add("a");
  5. list.add("b");
  6. list.add("c");
  7. //List->Array,申請大小為list.size()的陣列空間
  8. String[] strs = list.toArray(new String[list.size()]);
  9. }
  10. }

4 Array <-> Set


  1. public class arrayToSet {
  2. public static void main(String[] args) {
  3. /**
  4. * Array陣列型別轉為Set型別集合
  5. * 需要Array->List->Set
  6. */
  7. String[] strs = new String[]{"a","b","c"};
  8. Set<String> set = new HashSet<>(Arrays.asList(strs));
  9. /**
  10. * Set轉為Array和List轉為Array原理相同
  11. */
  12. strs = set.toArray(new String[set.size()]);
  13. }
  14. }

5 List <-> Set


List和Set都實現了Collection藉口,Collection.addAll()方法

相互轉換可以通過:

  1)Collection.addAll()

  2)構造方法直接傳入List/Set

  1. /** * List -> Set
  2. */
  3. Set set = new HashSet(list);//構造傳參
  4. set.addAll(list);//Collection.addAll()
  5. /**
  6. * Set -> List
  7. */
  8. List list = new ArrayList(set);
  9. list.addAll(set);