Java Collection Transformations (arrays, List, Set, map conversions)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.