Map, list traversal, pattern matching, file reading, threadlocal

Source: Internet
Author: User
Tags date now

Every time, forget ~~ Mark it. You forget it. Hahaha ~~

Ah, old, getting useless ~~

Bytes ---------------------------------------------------------------------------------------------

Private Static Map <string, string> projectipinfo = new hashmap <string, string> (); // if the key is the same, the appended map overwrites the previously added one, put (), get (), contains ()

Private Static Map <string, string> projectipmaskedinfo = new hashmap <string, string> ();

Private Static Map <string, pattern> projectipmaskedpatterninfo = new hashmap <string, pattern> ();

Private Static Map <string, list <string> projectipblackinfo = new hashmap <string, list <string> ();

For (iterator it = projectipblackinfo. Get ("project1"). iterator (); it. hasnext ();){

System. Out. println (it. Next ());

}

 

For (iterator it = projectipmaskedinfo. entryset (). iterator (); it. hasnext ();){

Map. Entry entry = (Map. Entry) it. Next ();

System. Out. println (entry. getkey ());

System. Out. println (entry. getvalue ());

}

Bytes ---------------------------------------------------------------------------------------------

For (iterator it = projectipmaskedinfo. entryset (). iterator (); it. hasnext ();){

Map. Entry entry = (Map. Entry) it. Next ();

Pattern P = projectipmaskedpatterninfo. Get (entry. getkey ());

// If the IP address matches the Regular Expression

If (P. matcher (IP). Matches ()){

String TEM = "-" + IP;

// If the regular expression is matched, but the IP address is in the blacklist

If (checkblacklist (entry. getvalue (). tostring (), TEM )){

Continue;

} Else {// if the regular expression matches and the IP address is not in the blacklist

Projname2 = entry. getvalue (). tostring ();

Break;

}

}

}

Bytes ---------------------------------------------------------------------------------------------

Inputstream in = NULL;

File file = new file ("C:/hello7.txt ");

Try {

In = new fileinputstream (File );

} Catch (ioexception e ){

System. Out. println ("Read File exception ......................");

}

List <list <string> outputs = new arraylist <list <string> ();

Bufferedreader BR = new bufferedreader (New inputstreamreader (in ));

String line = NULL;

 

Line = Br. Readline ();

While (line )! = NULL &&! Line. Equals ("-----")){

Line = Br. Readline ();

}

Bytes ----------------------------------------------------------------------------------------------

Threadlocal usage: Reference (http://lavasoft.blog.51cto.com/62575/51926)

1. Create a threadlocal object threadxxx in a multi-threaded class (such as threaddemo class) to save the xxx object to be isolated and processed between threads. 2. In the threaddemo class, create a method getxxx () to obtain the data to be isolated, and Judge in the method. If the threadlocal object is null, it should be new () an object that isolates the access type and forcibly converts it to the type to be applied. 3. In the run () method of the threaddemo class, use the getxxx () method to obtain the data to be operated. This ensures that each thread corresponds to a data object, this object is operated at any time.

 

Instance from: (http://blog.csdn.net/qjyong/article/details/2158097)

Import Java. util. collections; import Java. util. hashmap; import Java. util. map; public class simplethreadlocal {private map valuemap = collections. synchronizedmap (New hashmap (); Public void set (Object newvalue) {valuemap. put (thread. currentthread (), newvalue); // ① the key is the thread object, and the value is the variable copy of the thread} public object get () {thread currentthread = thread. currentthread (); object o = valuemap. get (currentthread); // ② return the variable if (o = NULL &&! Valuemap. containskey (currentthread) {// ③ if the map does not exist, store it in map. O = initialvalue (); valuemap. put (currentthread, O);} return O;} public void remove () {valuemap. remove (thread. currentthread ();} public object initialvalue () {return NULL ;}}

  

Public class sequencenumber {// ① use an anonymous internal class to overwrite the initialvalue () method of threadlocal and specify the initial value Private Static threadlocal <integer> seqnum = new threadlocal <integer> () {public integer initialvalue () {return 0 ;}; // ② obtain the next sequence value public int getnextnum () {seqnum. set (seqnum. get () + 1); // seqnum map: <thread1, value1> return seqnum. get ();} public static void main (string [] ARGs) {sequencenumber Sn = new sequencenumber (); // A sequencenumber object // 3 threads share Sn, their respective serial numbers are testclient T1 = new testclient (SN); // put them in different threads and share the object between threads, however, a thread maintains a separate copy testclient t2 = new testclient (SN); testclient T3 = new testclient (SN); t1.start (); t2.start (); t3.start ();} Private Static class testclient extends thread {private sequencenumber Sn; Public testclient (sequencenumber Sn) {This. sn = Sn;} public void run () {for (INT I = 0; I <3; I ++) {// ④ each thread generates three sequence values: system. out. println ("thread [" + thread. currentthread (). getname () + "] Sn [" + Sn. getnextnum () + "]") ;}}}

  

Date Acquisition:

// Query the number of days of the week on Friday

Date Now = new date ();

Date start = dateutil. adddays (now, Monday); // Monday

Start = dateutil. sethourstoempty (start); // on Monday

Date end = dateutil. adddays (now, Monday + 1); // Tuesday

End = dateutil. sethourstoempty (end); // on Tuesday

End = dateutil. addseconds (end,-1); // Tuesday 59 minutes 59 seconds

 

// Query the quantity of Monday of last week

Date lastweeknow = dateutil. adddays (now, lastmonday );

Date lastweekstart = dateutil. adddays (lastweeknow, 0); // last Monday

Lastweekstart = dateutil. sethourstoempty (lastweekstart); // last Monday

Date lastweekend = dateutil. adddays (now, lastmonday + 1); // last Tuesday

Lastweekend = dateutil. sethourstoempty (lastweekend); // last Tuesday 0

Lastweekend = dateutil. addseconds (lastweekend,-1); // last Tuesday 59 minutes 59 seconds

 

New bigdecimal (mvusef8. avgvalue). setscale (2, bigdecimal. round_half_up). toengineeringstring ()

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.