Collection, Map

Source: Internet
Author: User

Collection Collection

Collection derives two sub-interfaces, one is list and the other is set.

list: called a repeatable set, as the name implies, the collection is allowed to hold repeating elements, then what is the repeating element? A repeating element refers to an element that is not the same element, but rather the Equals method that compares to true.

set: called a non-repeating set, so that the same element cannot be stored in the collection two times, the same as the list, where the same refers to the two element equals the result of a comparison is true.

List: interface has two implementation classes:ArrayList is more suitable for random access and LinkedList is more suitable for inserting and deleting

    • E get (int index): Gets the element that corresponds to the specified subscript in the collection, starting with the subscript 0.
    • e Set (int index, E elment): Places the given element in the given position and returns the element from the original position
      1. void Add(int index,E element):

      Inserts the given element into the specified position, and the original position and subsequent elements are moved backwards in order.

      1. E Remove(int index):

      Deletes the element at the given position and returns the element that was deleted.

sublist Gets a list that occupies the same storage space as the original list, and the operation of the sub list affects the original list.

  1. list<integer> List = new ArrayList<integer> ();
  2. For (int i = 0; I < ten; I+ +) {
  3. List. Add(i);
  4. }
  5. System. Out. println(list); //[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  6. list<integer> sublist = List. Sublist(3, 8);
  7. System. Out. println(sublist); //[3, 4, 5, 6, 7]
  8. The list and source list obtained by sublist occupy the same data space
  9. For (int i = 0; I < sublist. Size(); I+ +) {
  10. Sublist. Set(i, sublist. Get(i) * ten);
  11. }
  12. System. Out. println(sublist); //[ +, +, +]
  13. System. Out. println(list); //[0, 1, 2, +, 8, 9]
  14. Can be used to delete continuous elements list.sublist (3, 8). Clear ();
  15. System. Out. println(list);
List conversions to arrays
  1. list<string> List = new ArrayList<string> ();
  2. List. Add("a");
  3. List. Add("B");
  4. List. Add("C");
  5. Usually we pass in an array that does not require a given length
  6. String[] strarr = List.         ToArray(new String[] {}); System. Out. println(Arrays. ToString(Strarr)); //[A, B, c]
Array conversion to List
  1. String[] strarr = { "a", "B", "C" };
  2. list<string> List = Arrays. Aslist(Strarr);
  3. System. Out. println(list); //[A, B, c]
  4. List.add ("D"); Will throw Unsupportedoperationexception
  5. Java.util.arrays$arraylist
  6. System. Out. println(list. GetClass().         GetName());
  7. list<string> List1 = new ArrayList<string> ();
  8. List1. AddAll(Arrays. Aslist(Strarr));

Collection, Map

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.