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

Source: Internet
Author: User

Reprinted from: http://blog.csdn.net/top_code/article/details/10552827

Code:

 Packagecom.example.test;Importjava.util.ArrayList;Importjava.util.Arrays;ImportJava.util.HashMap;ImportJava.util.HashSet;Importjava.util.List;ImportJava.util.Map;ImportJava.util.Set; Public classConvertortest {/**     * @paramargs*/     Public Static voidMain (string[] args) {Testlist2array ();        Testarray2list ();        Testset2list ();        Testlist2set ();        Testset2array ();        Testarray2set ();        Testmap2set ();    Testmap2list (); }    Private Static voidtestmap2list () {Map<string, string> map =NewHashmap<string, string>(); Map.put ("A", "ABC"); Map.put ("K", "KK"); Map.put ("L", "LV"); //Convert map Key to listList<string> mapkeylist =NewArraylist<string>(Map.keyset ()); System.out.println ("Mapkeylist:" +mapkeylist); //Convert map Key to listList<string> mapvalueslist =NewArraylist<string>(Map.values ()); System.out.println ("Mapvalueslist:" +mapvalueslist); }    Private Static voidTestmap2set () {Map<string, string> map =NewHashmap<string, string>(); Map.put ("A", "ABC"); Map.put ("K", "KK"); Map.put ("L", "LV"); //Converts the map's key to setSet<string> Mapkeyset =Map.keyset (); System.out.println ("Mapkeyset:" +mapkeyset); //Convert the value of map to setSet<string> Mapvaluesset =NewHashset<string>(Map.values ()); System.out.println ("Mapvaluesset:" +mapvaluesset); }    Private Static voidTestarray2set () {string[] arr= {"AA", "BB", "DD", "CC", "BB"}; //Array-->setSet<string> set =NewHashset<string>(Arrays.aslist (arr));      SYSTEM.OUT.PRINTLN (set); }    Private Static voidTestset2array () {Set<String> set =NewHashset<string>(); Set.add ("AA"); Set.add ("BB"); Set.add ("CC"); String[] Arr=Newstring[set.size ()]; //set--> ArraySet.toarray (arr);      System.out.println (arrays.tostring (arr)); }    Private Static voidTestlist2set () {List<String> list =NewArraylist<string>(); List.add ("ABC"); List.add ("EFG"); List.add ("LMN"); List.add ("LMN"); //List-->setSet<string> Listset =NewHashset<string>(list);    System.out.println (Listset); }    Private Static voidtestset2list () {Set<String> set =NewHashset<string>(); Set.add ("AA"); Set.add ("BB"); Set.add ("CC"); //Set --ListList<string> setlist =NewArraylist<string>(set);      System.out.println (setlist); }    Private Static voidTestlist2array () {//list--> Arraylist<string> list =NewArraylist<string>(); List.add ("AA"); List.add ("BB"); List.add ("CC"); Object[] Objects= List.toarray ();//returns an Object arraySystem.out.println ("Objects:" +arrays.tostring (objects)); String[] Arr=Newstring[list.size ()]; List.toarray (arr);//put the converted array into an already created objectSystem.out.println ("STRINGS1:" +arrays.tostring (arr)); }        Private Static voidtestarray2list () {//Array-->liststring[] ss = {"JJ", "KK"}; List<String> List1 =arrays.aslist (ss); List<String> List2 = arrays.aslist ("AAA", "BBB");          System.out.println (List1);      System.out.println (LIST2); }}

Complete.

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

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.