Get Random Key

Source: Internet
Author: User

ImportJava.util.Set;ImportIo.netty.util.internal.ConcurrentSet;/*** Get Key * * @project common-utils * @fileName Getkey.java * @Description *@authorLight-zhang * @date 2018-10-12 17:10:33 *@version1.0.0*/ Public Final classGetKey {Private Static FinalString basedigits = "0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz-_"; Private Static Final intBASE =basedigits.length (); Private Static Final Char[] Digitschar =Basedigits.tochararray (); Private Static Final intfast_size = ' Z '; Private Static Final int[] Digitsindex =New int[Fast_size + 1]; Static {         for(inti = 0; i < fast_size; i++) {Digitsindex[i]=-1; }         for(inti = 0; i < BASE; i++) {Digitsindex[digitschar[i]]=i; }    }    /*** Parameter decoding * *@paramparamter *@return     */     Public Static Longdecode (String paramter) {Longresult = 0L; synchronized(paramter) {LongMultiplier = 1;  for(intpos = Paramter.length ()-1; POS >= 0; pos--) {result+ = GetIndex (paramter, POS) *multiplier; Multiplier*=BASE; }        }        returnresult; }    /*** Incoming system nanoseconds to prevent duplication * *@paramNumber *@return     */     Public StaticString Encode (Long number) {FinalStringBuilder Builder =NewStringBuilder (); synchronized(number) {if(Number < 0)                NewIllegalArgumentException ("number (BASE64) must be positive:". Concat (Number.tostring ())); if(Number = = 0)                return"0";  while(Number! = 0) {builder.append (digitschar[(int) (number%BASE)]); number/=BASE; }        }        returnbuilder.tostring (); }    Private Static intGetIndex (String paramter,intPmindex) {        Charc =Paramter.charat (Pmindex); if(C >fast_size) {            NewIllegalArgumentException ("Unknow character for Base64:". Concat (paramter)); }        intindex =Digitsindex[c]; if(Index = =-1) {            NewIllegalArgumentException ("Unknow character for Base64:". Concat (paramter)); }        returnindex; }     Public Static voidMain (string[] args) {Set<String> data =NewConcurrentset<string>();  for(inti = 0; i < 20000000; i++) {            //System.out.println (Encode (System.nanotime ()));Data.add (Encode (System.nanotime ())); //System.out.println (Decode (Uuid.randomuuid (). toString ()));} System.out.println (Data.size ()); //System.out.println (Decode ("6c7p6dzh"));    }}

Get Random Key

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.