list、数组、set、map之间的相互转换

list、数组、set、map之间的相互转换

在日常编程中经常遇到需要转换类型的场景,因此在这里对一部分方法进行了总结。

list --> 数组

	List<String> list = new ArrayList<String>();
    list.add("aa");
    list.add("bb");
    list.add("cc");
	//方式一:
	Object[] array1 = list.toArray();	//返回object类的数组array
	//方式二:
	String[] array2 = new String[list.size()];
	list.toArray(array2);//传入初始化长度的数组,得到string类的数组array
	//方式三:
	String[] array3= list.toArray(new String[0]);//也可以直接传入空数组
	
	System.out.println(Arrays.toString(array1)); //[aa, bb, cc]
	System.out.println(Arrays.toString(array2)); //[aa, bb, cc]
	System.out.println(Arrays.toString(array3)); //[aa, bb, cc]

数组–>list

    String[] array = {"aa","bb", "cc"};
    //方式一:
    List<String> list1 = Arrays.asList(array);
    //方式二:
    List<String> list2 = Arrays.asList("aa","bb", "cc");
    //方式三:
    ArrayList<String> list3 = new ArrayList<>(Arrays.asList(array)) ;
    //方式四:
    ArrayList<String> list4 = new ArrayList<>(array.length);
    Collections.addAll(list4, array);
    
    System.out.println(list1);	//[aa, bb, cc]
    System.out.println(list2);	//[aa, bb, cc]
    System.out.println(list3);	//[aa, bb, cc]
    System.out.println(list4);	//[aa, bb, cc]

set --> list

    HashSet<String> set = new HashSet<>();
    set.add("aa");
    set.add("bb");
    set.add("cc");
    List<String> list5 = new ArrayList<>(set);
    System.out.println(list5);  //[aa, bb, cc]

list --> set

    List<String> list = new ArrayList<>();
    list.add("aa");
    list.add("bb");
    list.add("cc");
    Set<String> set = new HashSet<>(list);
    System.out.println(set); //[aa, bb, cc]

数组 --> set

    String[] array = {"aa","bb", "cc"};
    //方式一:
    Set<String> set1 = new HashSet<>(Arrays.asList(array));
    //方式二:
    Set<String> set2=new HashSet<>();
    Collections.addAll(set2,array);
    
    System.out.println(set1); //[aa, bb, cc]
    System.out.println(set2); //[aa, bb, cc]

set --> 数组

    Set<String> set = new HashSet<String>();
    set.add("aa");
    set.add("bb");
    set.add("cc");
    //方式一:
    Object[] array1 = set.toArray();
    //方式二:
    String[] array2= new String[set.size()];
    set.toArray(array2);
    //方式三:
    String[] array3= set.toArray(new String[0]);//也可以直接传入空数组
    
    System.out.println(Arrays.toString(array1)); //[aa, bb, cc]
    System.out.println(Arrays.toString(array2)); //[aa, bb, cc]
    System.out.println(Arrays.toString(array3)); //[aa, bb, cc]

map --> set/list

    Map<String, String> map = new HashMap<String, String>();
    map.put("key1", "value1");
    map.put("key2", "value2");
    // 将键转化为Set
    Set<String> mapKeySet = map.keySet();
    System.out.println(mapKeySet);	//[key1, key2]
    // 将值转化为Set
    Set<String> mapValuesSet = new HashSet<>(map.values());
    System.out.println(mapValuesSet); //[value2, value1]
    // 将值转化为List
    List<String> mapValuesList = new ArrayList<>(map.values());
    System.out.println(mapValuesList); //[value1, value2]

这里只总结了一些简单方便的转换方法,还有一些如stream的方式没有列举。欢迎大家补充。

你可能感兴趣的:(java,list,数据结构,java)