Transferred from: http://blog.csdn.net/top_code/article/details/10552827
1 Packagecom.example.test;2 3 Importjava.util.ArrayList;4 Importjava.util.Arrays;5 ImportJava.util.HashMap;6 ImportJava.util.HashSet;7 Importjava.util.List;8 ImportJava.util.Map;9 ImportJava.util.Set;Ten One Public classConvertortest { A - /** - * @paramargs the */ - Public Static voidMain (string[] args) { - - Testlist2array (); + testarray2list (); - testset2list (); + Testlist2set (); A Testset2array (); at Testarray2set (); - Testmap2set (); - testmap2list (); - } - - Private Static voidtestmap2list () { in -map<string, string> map =NewHashmap<string, string>(); toMap.put ("A", "ABC"); +Map.put ("K", "KK"); -Map.put ("L", "LV"); the * //Convert map Key to list $List<string> mapkeylist =NewArraylist<string>(Map.keyset ()); Panax NotoginsengSystem.out.println ("Mapkeylist:" +mapkeylist); - the //Convert map Key to list +List<string> mapvalueslist =NewArraylist<string>(Map.values ()); ASystem.out.println ("Mapvalueslist:" +mapvalueslist); the + } - $ Private Static voidTestmap2set () { $ -map<string, string> map =NewHashmap<string, string>(); -Map.put ("A", "ABC"); theMap.put ("K", "KK"); -Map.put ("L", "LV"); Wuyi the //Converts the map's key to set -Set<string> Mapkeyset =Map.keyset (); WuSystem.out.println ("Mapkeyset:" +mapkeyset); - About //Convert the value of map to set $Set<string> Mapvaluesset =NewHashset<string>(Map.values ()); -System.out.println ("Mapvaluesset:" +mapvaluesset); - } - A Private Static voidTestarray2set () { + theString[] arr = {"AA", "BB", "DD", "CC", "BB"}; - $ //Array-->set theSet<string> set =NewHashset<string>(Arrays.aslist (arr)); the System.out.println (set); the } the - Private Static voidTestset2array () { inSet<string> set =NewHashset<string>(); theSet.add ("AA"); theSet.add ("BB"); AboutSet.add ("CC"); the thestring[] arr =Newstring[set.size ()]; the //set--> Array + Set.toarray (arr); - System.out.println (arrays.tostring (arr)); the }Bayi the Private Static voidTestlist2set () { the -list<string> list =NewArraylist<string>(); -List.add ("ABC")); theList.add ("EFG"); theList.add ("LMN"); theList.add ("LMN"); the - //List-->set theSet<string> Listset =NewHashset<string>(list); the System.out.println (listset); the }94 the Private Static voidtestset2list () { the theSet<string> set =NewHashset<string>();98Set.add ("AA"); AboutSet.add ("BB"); -Set.add ("CC");101 102 //Set --List103List<string> setlist =NewArraylist<string>(set);104 System.out.println (setlist); the }106 107 Private Static voidTestlist2array () {108 //list--> Array109list<string> list =NewArraylist<string>(); theList.add ("AA"); 111List.add ("BB"); theList.add ("CC"); 113Object[] objects = List.toarray ();//returns an Object array theSystem.out.println ("Objects:" +arrays.tostring (objects)); the thestring[] arr =Newstring[list.size ()]; 117List.toarray (arr);//put the converted array into an already created object118System.out.println ("STRINGS1:" +arrays.tostring (arr)); 119 } - 121 Private Static voidtestarray2list () {122 //Array-->list123string[] ss = {"JJ", "KK"}; 124List<string> List1 =arrays.aslist (ss); theList<string> List2 = arrays.aslist ("AAA", "BBB"); 126 System.out.println (List1); 127 System.out.println (LIST2); - }129 the}
Java Collection Transformations (arrays, List, Set, map conversions)