Documente online.
Zona de administrare documente. Fisierele tale
Am uitat parola x Creaza cont nou
 HomeExploreaza
upload
Upload




Java Collectors - Best Practices

Java en


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

Extracting java.util.Date from XML Files

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")

String.indexOf() vs. String.matches() and Pattern.find()

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 .

Using String.matches() method

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:
String.matches(regex)

behaves in exactly the same way as the expression

Pattern.compile(regex).matcher(input).matches().

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*");


The "if" statement will be transformed as follows:

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())


Using String.replaceAll() and String.replace() method

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:
String.replaceAll(regex,replacement)

behaves in exactly the same way as the expression

Pattern.compile(regex).matcher(input).replaceAll(replacement).

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()


Document Info


Accesari: 1168
Apreciat: hand-up

Comenteaza documentul:

Nu esti inregistrat
Trebuie sa fii utilizator inregistrat pentru a putea comenta


Creaza cont nou

A fost util?

Daca documentul a fost util si crezi ca merita
sa adaugi un link catre el la tine in site


in pagina web a site-ului tau.




eCoduri.com - coduri postale, contabile, CAEN sau bancare

Politica de confidentialitate | Termenii si conditii de utilizare




Copyright © Contact (SCRIGROUP Int. 2024 )