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

Source: Internet
Author: User

  1. Package com.example.test;
  2. Import java.util.ArrayList;
  3. Import Java.util.Arrays;
  4. Import Java.util.HashMap;
  5. Import Java.util.HashSet;
  6. Import java.util.List;
  7. Import Java.util.Map;
  8. Import Java.util.Set;
  9. Public class Convertortest {
  10. /** 
  11. * @param args
  12. */
  13. public static void Main (string[] args) {
  14. Testlist2array ();
  15. Testarray2list ();
  16. Testset2list ();
  17. Testlist2set ();
  18. Testset2array ();
  19. Testarray2set ();
  20. Testmap2set ();
  21. Testmap2list ();
  22. }
  23. private static void Testmap2list () {
  24. map<string, string> map = new hashmap<string, string> ();
  25. Map.put ("A", "ABC");
  26. Map.put ("K", "KK");
  27. Map.put ("L", "LV");
  28. //Convert map Key to list
  29. list<string> mapkeylist = new Arraylist<string> (Map.keyset ());
  30. System.out.println ("mapkeylist:" +mapkeylist);
  31. //Convert map Key to list
  32. list<string> mapvalueslist = new Arraylist<string> (Map.values ());
  33. System.out.println ("mapvalueslist:" +mapvalueslist);
  34. }
  35. private static void Testmap2set () {
  36. map<string, string> map = new hashmap<string, string> ();
  37. Map.put ("A", "ABC");
  38. Map.put ("K", "KK");
  39. Map.put ("L", "LV");
  40. //Convert the map's key to set
  41. set<string> Mapkeyset = Map.keyset ();
  42. System.out.println ("Mapkeyset:" +mapkeyset);
  43. //Convert the value of map to set
  44. set<string> Mapvaluesset = new Hashset<string> (Map.values ());
  45. System.out.println ("Mapvaluesset:" +mapvaluesset);
  46. }
  47. private static void Testarray2set () {
  48. String[] arr = {"AA","BB","DD","CC","BB"};
  49. //Array-->set
  50. set<string> set = new Hashset<string> (Arrays.aslist (arr));
  51. SYSTEM.OUT.PRINTLN (set);
  52. }
  53. private static void Testset2array () {
  54. set<string> set = new hashset<string> ();
  55. Set.add ("AA");
  56. Set.add ("BB");
  57. Set.add ("CC");
  58. string[] arr = new String[set.size ()];
  59. //set--> Array
  60. Set.toarray (arr);
  61. System.out.println (arrays.tostring (arr));
  62. }
  63. private static void Testlist2set () {
  64. list<string> list = new arraylist<string> ();
  65. List.add ("ABC");
  66. List.add ("EFG");
  67. List.add ("LMN");
  68. List.add ("LMN");
  69. //list-->set
  70. set<string> listset = new hashset<string> (list);
  71. System.out.println (Listset);
  72. }
  73. private static void Testset2list () {
  74. set<string> set = new hashset<string> ();
  75. Set.add ("AA");
  76. Set.add ("BB");
  77. Set.add ("CC");
  78. //set-List
  79. List<string> setlist = new arraylist<string> (set);
  80. System.out.println (setlist);
  81. }
  82. private static void Testlist2array () {
  83. //list--> Array
  84. list<string> list = new arraylist<string> ();
  85. List.add ("AA");
  86. List.add ("BB");
  87. List.add ("CC");
  88. Object[] objects = List.toarray (); //Returns an object array
  89. System.out.println ("Objects:" +arrays.tostring (objects));
  90. string[] arr = new String[list.size ()];
  91. List.toarray (arr); //Convert the converted array into an already created object
  92. System.out.println ("STRINGS1:" +arrays.tostring (arr));
  93. }
  94. private static void Testarray2list () {
  95. //Array-->list
  96. string[] ss = {"JJ","KK"};
  97. List<string> List1 = arrays.aslist (ss);
  98. List<string> List2 = arrays.aslist ("AAA","BBB");
  99. System.out.println (List1);
  100. System.out.println (LIST2);
  101. }
  102. }

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.