当前位置 : 主页 > 编程语言 > c++ >

list to map and map to list相互转换

来源:互联网 收集:自由互联 发布时间:2021-06-30
java list to map and map to list /***user实体类*/@Userpublic class User { private int userId; private String userName; //.....省略set,get方法}@list to map 常见的方法(性能对比未知)public void listToMap() { User userOne
java list to map and map to list
/**
*user实体类
*/
@User
public class User {
    private int userId;
    private String userName;

    //.....省略set,get方法
}

@list to map 常见的方法(性能对比未知)
public void listToMap() {
    User userOne = new User();
    userOne.setUserId(1010);
    userOne.setUserName("李四");

    User userTwo = new User();
    userTwo.setUserId(1011);
    userTwo.setUserName("张三");

    List
 
   userList = new ArrayList
  
   (); userList.add(userOne); userList.add(userTwo); // 一般方法 Map
   
     map = new HashMap
    
     (); fro(User user : userList) { map.put(user.getUserId, User); } //JAVA 8直接用流的方法(userId为key) Map
     
       map = userList.strem.collect( Collectors.toMap(User::getUserId, (p) -> p)); //Java 包装类 Integer等 (key,value都是list的对应的值) List
      
        list = new ArrayList<>(Integer); Map
       
         map = list.strem.collect( Collectors.toMap(Integer::intValue, (p) -> p));//intValue,不同类型有对应的方法 //使用guava 工具类库 Map
        
          map = Maps.uniqueIndex(userList, new Function 
         
           () { public Integer apply(User from) { return from.getUserId(); }}); } @map to list public void mapToList() { Map
          
            map = new HashMap
           
            (); //取key为list的值 List
            
              listOne = new ArrayList
             
              (map.keySet()); //取value为list的值 List
              
                list = new ArrayList
               
                (map.values()); //Map.Entry
                
                  为list的值 List
                 
                  > listTwo = new ArrayList
                  
                   >(map.entrySet()); }
                  
                 
                
               
              
             
            
           
          
         
        
       
      
     
    
   
  
 
网友评论