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中 set,list,array(集合與陣列)、Map相互轉換Java陣列
- 【Scala篇】--Scala中集合陣列,list,set,map,元祖陣列
- Java中List與陣列互轉Java陣列
- 陣列與集合List的相互轉化陣列
- Java集合框架List,Map,Set等Java框架
- 陣列轉換成List集合陣列
- Java中List陣列互轉Java陣列
- Java中集合與陣列互轉總結Java陣列
- Java 中的泛型 集合(List,Set) MapJava泛型
- Map轉實體類或List陣列陣列
- Java集合體系總結 Set、List、Map、QueueJava
- Java List/Set/MapJava
- List、Set、Queue、Map
- Java集合類: Set、List、Map、Queue使用場景梳理Java
- Java執行緒安全的集合類:Map、List、SetJava執行緒
- Java:List集合&Integer物件&int[]陣列相互轉化Java物件陣列
- Java集合中List,Set以及Map等集合體系詳解(史上最全)Java
- List,Set,Queue,Map介面
- java中Map,List與Set的區別Java
- Java中List集合轉Map集合報錯:Duplicate keyJava
- json字串與Object、List、Map的互轉工具類JSON字串Object
- Kotlin——高階篇(四):集合(Array、List、Set、Map)基礎Kotlin
- java基礎學習之九:集合型別Set/List/MapJava型別
- java基礎-StringBuild、StringBuffer,集合List、Map、SetJavaUI
- Html遍歷物件、list集合、陣列HTML物件陣列
- Java 集合與陣列互相轉換Java陣列
- java中list、set和map 的區別(轉)Java
- List、Set、Map的區別
- set、List、map的區別
- list與Set、Map區別及適用場景
- set\list\map部分原始碼解析原始碼
- java 基礎之 Set、Map、ListJava
- 轉:java中陣列與List相互轉換的方法Java陣列
- Java學習--list,set,Map介面使用Java
- Java中的Set, List, Map漫談Java
- ES6的Set、Map資料結構 陣列資料結構陣列
- java之陣列與集合Java陣列
- 二維陣列和稀疏陣列互轉陣列