The difference between Collection and collections |, the GetBytes method of string

Source: Internet
Author: User

1. Compare the differences between collection  and collections =====   (1), java.util.collection  is a collection interface. It provides a common interface method for basic manipulation of collection objects. The collection interface has many specific implementations in the java  class library. The meaning of the collection interface is to provide a maximum unified operation for a variety of specific collections .  (2) 2, java.util.collections  is a wrapper class. It contains a variety of static polymorphic methods related to set operations. This class cannot be instantiated, just like a tool class that serves the Java collection framework.  package com.pf; import java.util.arraylist;import  java.util.Collections;import java.util.List;import java.util.Random;public class  Collectionsdemo {  public static void main (String[] args)  {     List<Integer> list=new ArrayList<Integer> (   //); Generates a random integer within 20 and places it in the list collection     list.add (New random (). Nextint ();     list.add (New random (). Nextint ())     list.add (New random (). NextInt (20));     list.add (New random () nextint);     for (Integer inta:List)     {      system.out.println (INTA);     }    system.out.println ("After sorting:");    //  Call the Collections tool class to manipulate the list object     collections.reverse (list);//Invert      Collections.sort (list);//Ascending     for (integer inta:list)     {       system.out.println (INTA);    }     SYSTEM.OUT.PRINTLN ("Maximum number:" +collections.max (list));

2 ====.==getbytes method ===== function: Gets the byte array of the string
This method uses the default character set of the platform to encode this string into a byte sequence and store the result in a new byte array.

Syntax 1 getBytes ()

The method returns the resulting byte array.

The example uses the GetBytes () function to encode the string strcom into a byte sequence, assign the return value to the STR array, and output the return value.
String strcom = "Java"; Define a string
byte[] str = strcom.getbytes (); Encodes the specified string into a byte sequence
for (int i=0;i<str.length;i++) {//Output return array
System.out.println (Str[i]);
}

Syntax 2 getBytes (Charset Charset)

CharSet: The charset used to encode a string.

The example uses the GetBytes () function to encode the string strcom with the default character set of the Java Virtual machine as a byte sequence, assigns the return value to the STR array, and outputs the return value.
String strcom = "Java"; Define a string
byte[] str = strcom.getbytes (Charset.defaultcharset ());
Encodes the specified string with the default encoding of the Java Virtual machine as a byte sequence
for (int i=0;i<str.length;i++) {//Output return array
System.out.println (Str[i]);
}

Syntax 3 getBytes (String charsetname)

The Charsetname:charsetname is a supported charset.

The example uses the GetBytes () function to encode the string strcom with GBK as a byte sequence, assigns the return value to the STR array, and outputs the return value.
String strcom = "Java"; Define a string
Byte[] STR;
try {
Encodes the specified string with GBK as a byte sequence
str = strcom.getbytes ("GBK");
for (int i = 0; i < str.length; i++) {//Output return array
System.out.println (Str[i]);
}
} catch (Unsupportedencodingexception e) {
E.printstacktrace ();
}

The difference between Collection and collections |, the GetBytes method of string

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.