List ,Set,Map集合與陣列互轉

尚雲峰111發表於2017-06-28

環境: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  
}

}


相關文章