Java list map set array conversion

Source: Internet
Author: User
Tags set set

1.list Turn Set

    1. Set set = new HashSet (new ArrayList ());


2.set Goto List

    1. List List = new ArrayList (new HashSet ());


3. Array to List

    1. List stooges = arrays.aslist ("Larry", "Moe", "Curly");


At this point there are three elements in the Stooges. Note: The list cannot be added at this time, otherwise it will report "Java.lang.UnsupportedOperationException", Arrays.aslist () returns a list, and is a fixed-length list, So it cannot be converted to ArrayList and can only be converted to Abstractlist
The reason is that the Aslist () method returns the list form of an array, and the returned list is just another view of the array, and the array itself does not disappear, and any action on the list is ultimately reflected on the array. So the Remove,add method is not supported.

    1. String[] arr = {"1", "2"};
    2. List List = Arrays.aslist (arr);
    3. String[] arr = {"1", "2"};
    4. List List = Arrays.aslist (arr);


4. Array to set

    1. Int[] A = { 1, 2, 3};
    2. Set set = new HashSet (Arrays.aslist (a));
    1. Int[] A = { 1, 2, 3};
    1. Set set = new HashSet (Arrays.aslist (a));


5.map of the relevant operation.

  1. Map map = new HashMap ();
  2. Map.put ("1", "a");
  3. Map.put (' 2 ', ' B ');
  4. Map.put (' 3 ', ' C ');
  5. SYSTEM.OUT.PRINTLN (map);
  6. All values are output
  7. System.out.println (Map.keyset ());
  8. All keys are output
  9. System.out.println (Map.values ());
  10. Convert the value of map to list
  11. List List = new ArrayList (Map.values ());
  12. SYSTEM.OUT.PRINTLN (list);
  13. Convert the value of map to set
  14. Set set = new HashSet (Map.values ());
  15. SYSTEM.OUT.PRINTLN (set);
  16. Map map = new HashMap ();
  17. Map.put ("1", "a");
  18. Map.put (' 2 ', ' B ');
  19. Map.put (' 3 ', ' C ');
  20. SYSTEM.OUT.PRINTLN (map);
  21. All values are output
  22. System.out.println (Map.keyset ());
  23. All keys are output
  24. System.out.println (Map.values ());
  25. Convert the value of map to list
  26. List List = new ArrayList (Map.values ());
  27. SYSTEM.OUT.PRINTLN (list);
  28. Convert the value of map to set
  29. Set set = new HashSet (Map.values ());
  30. SYSTEM.OUT.PRINTLN (set);


6.list Turn Array

      1. List List = Arrays.aslist ("A","B");
      2. SYSTEM.OUT.PRINTLN (list);
      3. String[] arr = (string[]) List.toarray (new String[list.size ()]);
      4. System.out.println (arrays.tostring (arr));

Java list map set array conversion

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.