ALTE DOCUMENTE
|
||||||||
Java Collectors - Best Practices
This document is intended to present best practices in writing Java Collectors
Content
1 Extracting java.util.Date from XML Files 2
2 String.indexOf() vs. String.matches() and Pattern.find() 3
3 Using String.matches() method 4
4 Using String.replaceAll() and String.replace() method 5
Usually each XML feed has only one date format. Ex:
Betoto feed: D="9/21/2006 8:05:00 AM"
24hpoker feed: date=" " time=" "
We need to parse these Strings and to create java.util.Date objects. These objects will be used to setup events dates.
We will create a java.text.SimpleDateFormat instance based on the pattern found in the XML feed. Ex:
Betoto: private static final SimpleDateFormat _dateFormat=new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a"); 24hpoker: private static final SimpleDateFormat _dateFormat=new SimpleDateFormat("yyyy-MM-ddHH:mm");
Later, during match or outright iteration, we will create Date following the example bellow:
Date eventDate = DateUtil.parse(_dateFormat,dateStr);
Where dateStr is the String that represents data value extracted from the XML feed ("9/21/2006 8:05:00 AM")
Whatever is possible use String.indexOf() in place of String.matches(). Ex:
String.matches() |
Replace With |
String.indexOf() |
participant.matches("other" |
participant.indexOf("other" |
|
eventName.matches("top 3" |
eventName.indexOf("top 3" |
|
eventName.matches(".*top 3.*") && eventName.indexOf("other" |
Pattern p=Pattern.compile("top 3|other" p.macher(eventName).find() |
String.indexOf() is far away much faster than String.matches(). String.indexOf() is the fastest , pattern + find is mid range speed and String.matches() is the slowest .
There are situation when we wants to check if a string satisfy a pattern or not. Ex:
Betoto: if (eventName.matches(".*top.*.bet*"
Usually, this code is called in a loop (for or while).
The Java Doc says:So, each time we call eventName.matches(".*top.*.bet*" , behind the scene JVM calls Pattern.compile(".*top.*.bet*").matcher(eventName).matches().
Pattern.compile(".*top.*.bet*") is time consuming. An better way to remove this repetitive computation is to declare a class variable (private static final) in the following form:
private static final Pattern eventNameOutrightPattern = Pattern.compile(".*top.*.bet*");
if(eventNameOutrightPattern.matcher(eventName).matches())
The best way is to transform like this
private static final Pattern eventNameOutrightPattern = Pattern.compile("top.*.bet"
The "if" statement will be transformed as follows:if(eventNameOutrightPattern.matcher(eventName).find())
There are situation when we wants to check if a string satisfy a pattern or not. Ex:
Betoto: if (eventName.replaceAll("top",""
Usually, this code is called in a loop (for or while).
The Java Doc says:So, each time we call eventName.replaceAll ("top" , behind the scene JVM calls Pattern.compile("top").matcher(eventName).replaceAll ("").
Pattern.compile("top") is time consuming. An better way to remove this repetitive computation is to declare a class variable (private static final) in the following form:
private static final Pattern eventNameOutrightPattern = Pattern.compile("top");
The statement will be transformed as follows:eventNameOutrightPattern.matcher(eventName).replaceAll()
String.replace() uses the same algorithm (although it is not specified) so use
private static final Pattern eventNameOutrightPattern = Pattern.compile("top");
The statement will be transformed as follows:eventNameOutrightPattern.matcher(eventName).replaceAll()
|