String, List collection Collection.sort sort, Set/map collection

Source: Internet
Author: User

The difference between string and StringBuffer

1, string has an immutable length property, when the string is added or deleted, replace, does not change the contents of the string object, but produces a new string object

2, StringBuffer class: StringBuffer will open a space in the buffer, when the StringBuffer content to add or delete, replace, will not change the contents of the string object, No new objects will be generated: If you frequently modify the contents of the string then use StringBuffer, if you need a string then the last use of the StringBuffer to string () method

     Public Static void Main (string[] args) {        = "ABCD";         New StringBuffer ("ABCD");        Test (S,SB);        System.out.println (s+ "   " +SB);    }          Public Static void Test (String s,stringbuffer sb) {        = s + "&&&";        Sb.append ("* * *");    }} ABCD   ABCD* * *

<List> Collections Sort

 Public classCollectionstest { Public Static voidMain (string[] args) {List<Product> list =NewArraylist<product>(); List.add (NewProduct (2, "Bird's Nest", 130)); List.add (NewProduct (1, "Shark fin", 150)); List.add (NewProduct (5, "Bear Paw", 120)); List.add (NewProduct (4, "abalone", 90)); Collections.sort (list,NewComparator<product>() {@Override Public intCompare (product O1, product O2) {returnO1.getcode ()-o2.getcode ();//Ascending}//Descending O2-getcode ()-o1.getcode ()});//Rearrange collection elements randomlycollections.shuffle (list);  for(Product i:list) {System.out.println (i);} }}

Set Array Ordering

 Public classText { Public Static voidMain (string[] args) {//int []array={1,2,8,3,4,9,6,8};//    //set<integer> setarray=new hashset<integer> ();//for (int x:array) {//Setarray.add (x);//    }//For (Integer s:setarray) {//System.out.println (s);//    }//    //set<product> setarray=new hashset<product> ();//Setarray.add (New Product (2, "Apple", 5));//Setarray.add (New Product (2, "watermelon",));//Setarray.add (New Product (5, "banana", 4));//System.out.println (Setarray.size ());//For (Product s:setarray) {//System.out.println (s);//        }//Map<integer,product> map=NewHashmap<integer,product>(); Map.put (1,NewProduct (1, "Bird's Nest", 120)); Map.put (2,NewProduct (2, "Excuse Me", 45)); Map.put (3,NewProduct (3, "Spell over", 12)); //wait until the length of the mapSystem.out.println (Map.size ()); //directly delete the corresponding key and value according to the given keyMap.Remove (2); //quickly find the value of the keyProduct P=map.get (2);                SYSTEM.OUT.PRINTLN (P); Set<Integer> m=Map.keyset ();  for(Integer i:m) {Product h=Map.get (i); System.out.println ("Key" +i+ "value" +h); }

String, List collection Collection.sort sort, Set/map collection

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.