List和Array的转换
package com.example.面向对象1; import java.util.*; public class listdemo2 { public static void main(String[] args) { // List转换为Array List<String> list1 = new ArrayList<>(); list1.add("AAAA"); list1.add("BBBB"); list1.add("CCCC"); list1.add("DDDD"); String[] array = list1.toArray(new String[list1.size()]); for (String animal : array) { System.out.println(animal); } //Array转换为List String[] countries = {"AAAA", "BBBB", "CCCC", "DDDD"}; List list2 = Arrays.asList(countries); System.out.println(list2); } }
List转换为JSONArray,List转JSONArray
package com.example.democrud.democurd.controller; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import java.util.ArrayList; import java.util.List; public class indexof { public static void main(String[] args) { // List转换为JSONArray List<String> list1 = new ArrayList<>(); list1.add("AAAA"); list1.add("BBBB"); list1.add("CCCC"); list1.add("DDDD"); String[] array = list1.toArray(new String[list1.size()]); for (String animal : array) { System.out.println(animal); } System.out.println("List转换为JSONArray"); JSONArray array1= JSONArray.parseArray(JSON.toJSONString(list1)); System.out.println("List转换为JSONArray"+array1); System.out.println("---------------------------------"); //创建一个对象 User user1 = new User("秦疆1号", 3, "男"); User user2 = new User("秦疆2号", 3, "男"); User user3 = new User("秦疆3号", 3, "男"); User user4 = new User("秦疆4号", 3, "男"); List<User> list = new ArrayList<User>(); list.add(user1); list.add(user2); list.add(user3); list.add(user4); System.out.println(list); System.out.println("List转换为JSONArray"); JSONArray array3= JSONArray.parseArray(JSON.toJSONString(list)); System.out.println("List转JSONArray"+array3); System.out.println("---------------------------------"); List<User> list2 = JSONObject.parseArray(array3.toJSONString(), User.class); System.out.println("JSONArray转List"+list2); } }
运行结果:
AAAA BBBB CCCC DDDD List转换为JSONArray List转换为JSONArray["AAAA","BBBB","CCCC","DDDD"] --------------------------------- [User(name=秦疆1号, age=3, sex=男), User(name=秦疆2号, age=3, sex=男), User(name=秦疆3号, age=3, sex=男), User(name=秦疆4号, age=3, sex=男)] List转换为JSONArray List转JSONArray[{"sex":"男","name":"秦疆1号","age":3},{"sex":"男","name":"秦疆2号","age":3},{"sex":"男","name":"秦疆3号","age":3},{"sex":"男","name":"秦疆4号","age":3}] --------------------------------- JSONArray转List[User(name=秦疆1号, age=3, sex=男), User(name=秦疆2号, age=3, sex=男), User(name=秦疆3号, age=3, sex=男), User(name=秦疆4号, age=3, sex=男)]
Map转换为List
package com.example.demo.controller.Demo; import com.example.demo.entity.User; import org.apache.commons.beanutils.BeanUtils; import java.beans.BeanInfo; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class demoTest3 { public static void main(String[] args) { // Map的Key值转换为List Map<Integer, String> map = new HashMap<>(); map.put(1, "AAAA"); map.put(2, "BBBB"); map.put(3, "CCCC"); map.put(4, "DDDD"); List<Integer> list = new ArrayList<Integer>(map.keySet()); System.out.println("\n****** Map的Key值转换为List ******"); System.out.println("map.keySet()==>" + list); // Map的Value值转换为List Map<Integer, String> map1 = new HashMap<>(); map1.put(1, "AAAA"); map1.put(2, "BBBB"); map1.put(3, "CCCC"); map1.put(4, "DDDD"); List<String> list1 = new ArrayList<String>(map.values()); System.out.println("\n****** Map的Value值转换为List ******"); System.out.println("map.values()==>" + list1); } }
Map转换为Set
package com.example.demo.controller.Demo; import com.example.demo.entity.User; import org.apache.commons.beanutils.BeanUtils; import java.beans.BeanInfo; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.Method; import java.util.*; public class demoTest3 { public static void main(String[] args) { // Map的Key值转换为List Map<Integer,String> map = new HashMap<>(); map.put(1,"AAAA"); map.put(2,"BBBB"); map.put(3,"CCCC"); map.put(4,"DDDD"); Set<Integer> set = new HashSet<>(map.keySet()); System.out.println("\n****** Map的Key值转换为Set ******"); System.out.println("map.keySet()==>" + set); // Map的Value值转换为Set Map<Integer,String> map1 = new HashMap<>(); map1.put(1,"AAAA"); map1.put(2,"BBBB"); map1.put(3,"CCCC"); map1.put(4,"DDDD"); Set<String> set1 = new HashSet<String>(map.values()); System.out.println("\n****** Map的Value值转换为Set ******"); System.out.println("map.values()==>" + set1); } }