List ,Set,Map集合与数组互转

List ,Set,Map集合与数组互转

环境:java8


public class 集合与数组转换 {

List<Employee> emps = Arrays.asList(
new Employee(102, "李四", 59, 6666.66, Status.BUSY),
new Employee(101, "张三", 18, 9999.99, Status.FREE),
new Employee(103, "王五", 28, 3333.33, Status.VOCATION),
new Employee(104, "赵六", 8, 7777.77, Status.BUSY),
new Employee(104, "赵六", 8, 7777.77, Status.FREE),
new Employee(104, "赵六", 8, 7777.77, Status.FREE),
new Employee(105, "田七", 38, 5555.55, Status.BUSY)
);


//List集合转数组
@Test
public void test2()  {
//方式1 筛选年龄大于28的
Employee[] array = emps.stream()
   .filter(e->e.getAge()>28)
   .toArray(Employee[]::new);
   //等同
//    .toArray(length-> new Employee[length]);
System.out.println(Arrays.toString(array));

//方式2
Employee[] array2 = emps.toArray(new Employee[emps.size()]);
System.out.println(Arrays.toString(array2));

//方式3
Employee[] array3 =(Employee[]) emps.toArray();
System.out.println(Arrays.toString(array3));

}

//将Employee特定属性转换为数组
@Test
public void test3()  {
   String[] array = emps.stream()
   .filter(e->e.getAge()>28)
   .map(Employee::getName)
   .toArray(String[]::new);
System.out.println(Arrays.toString(array));
}

//数组转List
@Test
public void test22() {
List<Employee> emps = Arrays.asList(
new Employee(102, "李四", 59, 6666.66, Status.BUSY),
new Employee(101, "张三", 18, 9999.99, Status.FREE),
new Employee(103, "王五", 28, 3333.33, Status.VOCATION),
new Employee(104, "赵六", 8, 7777.77, Status.BUSY));
// 因为:Arrays.asList()返回一个受指定数组支持的固定大小的列表。所以不能做Add、Remove等操作。
//如果要对集合做add,remove操作
emps=new ArrayList<>(emps);
emps.remove(0);
System.out.println(emps);
}



// set集合互转 ,几乎跟List一致
@Test
public void test4()  {
Set<String> set=new HashSet<String>();
//数组转集合
set.addAll(Arrays.asList("aa","bb","cc"));

//集合转数组
//方式1
String[] array = set.stream()
  .toArray(String[]::new);
System.out.println(Arrays.toString(array));
//方式2
String[] array2 = set.toArray(new String[set.size()]);
System.out.println(Arrays.toString(array2));
//方式3 似乎只能转Object,遍历的Object值跟String一样
Object[] array3 = set.toArray();
// String[] array3 = (String[]) set.toArray();//报错不能转换
System.out.println(Arrays.toString(array3));

//其他TreeSet等一样

}


//Map与数组
@Test
public void test5() {
Map<String, Integer> map=new HashMap<String, Integer>();
//目前没发现数组可以直接转map
map.put("aa", 1);
map.put("bb", 2);
map.put("cc", 3);

//键
Set<String> keySet = map.keySet();
//值
Collection<Integer> values = map.values();
//视图
Set<Entry<String, Integer>> entrySet = map.entrySet();

//map转数组(回到了set和list转数组的方法了)
//方式1
String[] array =  map.keySet().stream()
                            .toArray(String[]::new);
//方式2
String[] array2 = map.keySet().toArray(new String[map.size()]);

//或者
//对于entrySet,需要对键值分别转换
//key-->String[]
String[] array3 = entrySet.stream()
                                            .map(Entry::getKey)
                                            .toArray(String[]::new);
System.out.println(Arrays.toString(array3));

//values-->Integer[]
Integer[] array4 = entrySet.stream()
                                              .map(Entry::getValue)
                                              .toArray(Integer[]::new);
System.out.println(Arrays.toString(array4));


// Integer[] array5 = values.stream()
//      .toArray(Integer[]::new);
// System.out.println(Arrays.toString(array5));


//补:List,set集合转换,互相作为构造入参就可以了

   }
}








public class Employee {


private int id;
private String name;
private int age;
private double salary;
private Status status;


public Employee() {
}


public Employee(String name) {
this.name = name;
}


public Employee(String name, int age) {
this.name = name;
this.age = age;
}

public String  show(){
return "返回String";
}


public Employee(String name, int age,double salary) {
this.name = name;
this.age = age;
this.salary=salary;
}

public Employee(int id, String name, int age, double salary) {
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
}
public Employee(int id, String name, int age, double salary,Status status) {
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
this.status=status;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

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 double getSalary() {
return salary;
}

public void setSalary(double salary) {
this.salary = salary;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
long temp;
temp = Double.doubleToLongBits(salary);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
if (age != other.age)
return false;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (Double.doubleToLongBits(salary) != Double.doubleToLongBits(other.salary))
return false;
return true;
}

@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", age=" + age + ", salary=" + salary + "]";
}

public enum Status{
BUSY,FREE,VOCATION  
}

}


个人分类:JAVA 工具类
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页