java map转 数组中_Java 集合转换(数组、List、Set、Map相互转换)

时间:2025-02-27 14:47:55

package ;

import ;

import ;

import ;

import ;

import ;

import ;

import ;

public class ConvertorTest {

/**

* @param args

*/

public static void main(String[] args) {

testList2Array();

testArray2List();

testSet2List();

testList2Set();

testSet2Array();

testArray2Set();

testMap2Set();

testMap2List();

}

private static void testMap2List() {

Map map = new HashMap();

("A", "ABC");

("K", "KK");

("L", "LV");

// 将Map Key 转化为List

List mapKeyList = new ArrayList(());

("mapKeyList:"+mapKeyList);

// 将Map Key 转化为List

List mapValuesList = new ArrayList(());

("mapValuesList:"+mapValuesList);

}

private static void testMap2Set() {

Map map = new HashMap();

("A", "ABC");

("K", "KK");

("L", "LV");

// 将Map 的键转化为Set

Set mapKeySet = ();

("mapKeySet:"+mapKeySet);

// 将Map 的值转化为Set

Set mapValuesSet = new HashSet(());

("mapValuesSet:"+mapValuesSet);

}

private static void testArray2Set() {

String[] arr = {"AA","BB","DD","CC","BB"};

//数组-->Set

Set set = new HashSet((arr));

(set);

}

private static void testSet2Array() {

Set set = new HashSet();

("AA");

("BB");

("CC");

String[] arr = new String[()];

//Set-->数组

(arr);

((arr));

}

private static void testList2Set() {

List list = new ArrayList();

("ABC");

("EFG");

("LMN");

("LMN");

//List-->Set

Set listSet = new HashSet(list);

(listSet);

}

private static void testSet2List() {

Set set = new HashSet();

("AA");

("BB");

("CC");

//Set --> List

List setList = new ArrayList(set);

(setList);

}

private static void testList2Array() {

//List-->数组

List list = new ArrayList();

("AA");

("BB");

("CC");

Object[] objects = ();//返回Object数组

("objects:"+(objects));

String[] arr = new String[()];

(arr);//将转化后的数组放入已经创建好的对象中

("strings1:"+(arr));

}

private static void testArray2List() {

//数组-->List

String[] ss = {"JJ","KK"};

List list1 = (ss);

List list2 = ("AAA","BBB");

(list1);

(list2);

}

}