Package com.example.test;
Import java.util.ArrayList;
Import Java.util.Arrays;
Import Java.util.HashMap;
Import Java.util.HashSet;
Import java.util.List;
Import Java.util.Map;
Import Java.util.Set;
public class Convertortest {
/**
* @param args
*/
public static void Main (string[] args) {
Testlist2array ();
Testarray2list ();
Testset2list ();
Testlist2set ();
Testset2array ();
Testarray2set ();
Testmap2set ();
Testmap2list ();
}
private static void Testmap2list () {
map<string, string> Map = new hashmap<string, string> ();
Map.put ("A", "ABC");
Map.put ("K", "KK");
Map.put ("L", "LV");
//Convert map Key to List
list<string> mapkeylist = new arraylist<string> (Map.keyset ());
System.out.println ("mapkeylist:" +mapkeylist);
//Convert map Key to List
list<string> mapvalueslist = new arraylist<string> (Map.values ());
System.out.println ("mapvalueslist:" +mapvalueslist);
}
private static void Testmap2set () {
map<string, string> map = new hashmap<string, string> ();
Map.put ("A", "ABC");
Map.put ("K", "KK");
Map.put ("L", "LV");
Converts the map's key to set
set<string> Mapkeyset = Map.keyset ();
System.out.println ("Mapkeyset:" +mapkeyset);
Convert the value of map to set
set<string> mapvaluesset = new hashset<string> (Map.values ());
System.out.println ("Mapvaluesset:" +mapvaluesset);
}
private static void Testarray2set () {
String[] arr = {"AA", "BB", "DD", "CC", "BB"};
Array-->set
set<string> set = new Hashset<string> (Arrays.aslist (arr));
SYSTEM.OUT.PRINTLN (set);
}
private static void Testset2array () {
set<string> set = new hashset<string> ();
Set.add ("AA");
Set.add ("BB");
Set.add ("CC");
string[] arr = new string[set.size ()];
Set--> Array
Set.toarray (arr);
System.out.println (arrays.tostring (arr));
}
private static void Testlist2set () {
list<string> list = new arraylist<string> ();
List.add ("ABC");
List.add ("EFG");
List.add ("LMN");
List.add ("LMN");
List-->set
set<string> listset = new hashset<string> (list);
System.out.println (Listset);
}
private static void Testset2list () {
set<string> set = new hashset<string> ();
Set.add ("AA");
Set.add ("BB");
Set.add ("CC");
Set--List
List<string> setlist = new arraylist<string> (set);
System.out.println (setlist);
}
private static void Testlist2array () {
List--> Array
list<string> list = new arraylist<string> ();
List.add ("AA");
List.add ("BB");
List.add ("CC");
Object[] objects = List.toarray ();//Returns an object array
System.out.println ("Objects:" +arrays.tostring (objects));
string[] arr = new string[list.size ()];
List.toarray (arr);//convert the converted array into an already created object
System.out.println ("STRINGS1:" +arrays.tostring (arr));
}
private static void Testarray2list () {
Array-->list
string[] ss = {"JJ", "KK"};
List<string> List1 = arrays.aslist (ss);
List<string> List2 = arrays.aslist ("AAA", "BBB");
System.out.println (List1);
System.out.println (LIST2);
}
}
Java Collection Transformations (arrays, List, Set, map conversions)