List、Map、Set的区别与联系及应用场景

电脑技术 电脑技术 373 人阅读 | 1 人回复 | 2023-11-03

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
https://www.cnblogs.com/ryelqy/p/14312176.html



...
还是博客园更有些深度

回答|共 1 个

legs+ 发表于 2023-11-3 18:51:55| 字数 1,219 | 显示全部楼层

  1. import com.itcodai.course02.entity.User;
  2. import org.springframework.web.bind.annotation.RequestMapping;
  3. import org.springframework.web.bind.annotation.RestController;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;

  8. @RestController
  9. @RequestMapping("/json")
  10. public class JsonController {

  11.     @RequestMapping("/user")
  12.     public User getUser() {
  13.         return new User(1, "倪升武", "123456");
  14.     }

  15.     @RequestMapping("/list")
  16.     public List<User> getUserList() {
  17.         List<User> userList = new ArrayList<>();
  18.         User user1 = new User(1, "倪升武", "123456");
  19.         User user2 = new User(2, "达人课", "123456");
  20.         userList.add(user1);
  21.         userList.add(user2);
  22.         return userList;
  23.     }

  24.     @RequestMapping("/map")
  25.     public Map<String, Object> getMap() {
  26.         Map<String, Object> map = new HashMap<>(3);
  27.         User user = new User(1, "倪升武", "123456");
  28.         map.put("作者信息", user);
  29.         map.put("博客地址", "http://blog.itcodai.com");
  30.         map.put("CSDN地址", "http://blog.csdn.net/eson_15");
  31.         map.put("粉丝数量", 4153);
  32.         return map;
  33.     }
  34. }
复制代码

举例说明

当然,首先要有实体类
  1. public class User {
  2.     private Long id;
  3.     private String username;
  4.     private String password;
  5.     /* 省略get、set和带参构造方法 */
  6. }
复制代码





您需要登录后才可以回帖 登录 | 立即注册

本版积分规则