map集合转化成list集合

本文介绍了一个使用Java实现的具体案例,展示了如何将包含员工信息的HashMap转换为ArrayList的过程。通过定义Emp类存储员工数据,并使用ToMap辅助类完成转换操作。

package emp;

public class Emp {
 private String name;
 private int age;
 private int salar;
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 public int getSalar() {
  return salar;
 }
 public void setSalar(int salar) {
  this.salar = salar;
 }
 public Emp() {
  super();
  // TODO Auto-generated constructor stub
 }
 public Emp(String name, int age, int salar) {
  super();
  this.name = name;
  this.age = age;
  this.salar = salar;
 }
 @Override
 public String toString() {
  return "Emp [name=" + name + ", age=" + age + ", salar=" + salar + "]";
 }

}

 

 

 

public class ToList {
 private Object key;
 private Object value;
 public ToList() {
  super();
  // TODO Auto-generated constructor stub
 }
 public ToList(Object key, Object value) {
  super();
  this.key = key;
  this.value = value;
 }
 @Override
 public String toString() {
  return "ToMap [key=" + key + ", value=" + value + "]";
 }
 public static ArrayList toList(HashMap<String,Emp> map){
  ArrayList alist=new ArrayList();
  Set set=map.keySet();
  Iterator<String> it = set.iterator();
  while(it.hasNext()){
   //Object key=it.next();
   String key=it.next();
   //Object value=map.get(key);
   Emp value=map.get(key);
   Object obj=new ToList(key,value);
   alist.add(obj); 
  }
  return alist; 
 } 
 public static void main(String[] args) {
  HashMap<String,Emp> hm=new HashMap<String,Emp>();
  hm.put("a", new Emp("a",32,8000));
  hm.put("b", new Emp("b",46,6000));
  hm.put("c", new Emp("c",22,2000));
  hm.put("d", new Emp("d",51,9000));
  ArrayList alist=toList(hm);
  System.out.println(alist); 
 }

Java 中,将 `Map` 集合换为 `List` 集合通常是指将 `Map` 中的键、值或键值对换为 `List`。以下是几种常见的换方式: ### 将 Map 的键换为 List 可以使用 `keySet()` 方法获取 `Map` 的所有键,并将其换为 `List`: ```java Map<String, String> map = new HashMap<>(); map.put("1", "a"); map.put("2", "b"); map.put("3", "c"); List<String> keyList = new ArrayList<>(map.keySet()); System.out.println(keyList); // 输出: [1, 2, 3] ``` ### 将 Map 的值换为 List 可以使用 `values()` 方法获取 `Map` 的所有值,并将其换为 `List`: ```java List<String> valueList = new ArrayList<>(map.values()); System.out.println(valueList); // 输出: [a, b, c] ``` ### 将 Map 的键值对换为 List 如果需要将 `Map` 的键值对换为 `List`,可以使用 `entrySet()` 方法获取 `Map` 的键值对集合,并将其换为 `List`: ```java List<Map.Entry<String, String>> entryList = new ArrayList<>(map.entrySet()); System.out.println(entryList); // 输出: [1=a, 2=b, 3=c] ``` ### 将 Map 的键值对换为自定义对象的 List 如果 `Map` 中存储的是自定义对象,可以将其换为 `List`。假设有一个 `Student_3` 类,可以将 `Map` 的值换为 `Student_3` 对象的 `List`: ```java public class Student_3 { private int id; private String name; private int age; private String sex; public Student_3(int id, String name, int age, String sex) { this.id = id; this.name = name; this.age = age; this.sex = sex; } @Override public String toString() { return "[" + id + " " + name + " " + age + " " + sex + "]"; } } // 假设 map 中存储的是 Student_3 对象 Map<String, Student_3> map = new HashMap<>(); map.put("1", new Student_3(1, "Alice", 20, "Female")); map.put("2", new Student_3(2, "Bob", 22, "Male")); List<Student_3> studentList = new ArrayList<>(map.values()); System.out.println(studentList); // 输出: [[1 Alice 20 Female], [2 Bob 22 Male]] ``` 这些方法可以帮助你将 `Map` 集合换为 `List` 集合,具体选择哪种方式取决于你的需求[^1]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值