私は大量のデータを解析するプログラムを書いています(データセット自体の例はここで入手できます:https ://explore.data.gov/Geography-and-Environment/Worldwide-M1-Earthquakes-Past-7- Days / 7tag-iwnu)。
次のクラスは完全に正常に機能しますが、メソッドの各項目の間に余分な時間を呼び出す必要がある理由がわかりません。何故ですか?それは私が対処しなければならない通常の癖ですか、それともパターン/マッチャーを間違って設定しましたか?matcher.find()
parseEarthquake()
このメソッドは、データ行の1つ(たとえばnc,71958020,1,"Thursday, March 21, 2013 17:13:34 UTC",38.8367,-122.8298,1.4,2.60,28,"Northern California"
)を含む文字列を受け取り、データの地震オブジェクトを返します。
import java.text.DecimalFormat;
import java.text.FieldPosition;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Earthquake {
String src="xx";
String eqid="00000000";
short version;
long dateTime;
float lat, lon;
float mag, dep;
short nst;
String region="Nowhere";
private Earthquake(){
date.setTimeZone(TimeZone.getTimeZone("UTC"));
}
private static DecimalFormat
coords = new DecimalFormat( "##0.0000" ),
magnitude = new DecimalFormat( "###0.0" ),
depth = new DecimalFormat( "###0.00" );
private static SimpleDateFormat date = new SimpleDateFormat("'\"'EEEE', 'MMMM' 'dd', 'yyyy' 'HH':'mm':'ss' 'zzz'\"'");
// Src, Eqid, Version, Datetime, Lat, Lon, Magnitude, Depth, NST, Region;
public static Earthquake parseEarthquake(String string){
Earthquake result = new Earthquake();
Matcher matcher = Pattern.compile("(\".*?\")|([^,]*)").matcher(string);
try {
matcher.find(); result.src = matcher.group();
matcher.find(); matcher.find(); result.eqid = matcher.group();
matcher.find(); matcher.find(); result.version = Short.parseShort(matcher.group());
matcher.find(); matcher.find(); result.dateTime = date.parse(matcher.group()).getTime();
matcher.find(); matcher.find(); result.lat = coords.parse(matcher.group()).floatValue();
matcher.find(); matcher.find(); result.lon = coords.parse(matcher.group()).floatValue();
matcher.find(); matcher.find(); result.mag = magnitude.parse(matcher.group()).floatValue();
matcher.find(); matcher.find(); result.dep = depth.parse(matcher.group()).floatValue();
matcher.find(); matcher.find(); result.nst = Short.parseShort(matcher.group());
matcher.find(); matcher.find(); result.region = matcher.group();
} catch (ParseException e) {
e.printStackTrace();
} catch (NumberFormatException e) {
e.printStackTrace();
}
return result;
}
public String toString(){
StringBuffer buf = new StringBuffer();
buf.append(src);
buf.append(','); buf.append(eqid);
buf.append(','); buf.append(version);
buf.append(','); date.format(dateTime, buf, new FieldPosition(0));
buf.append(','); coords.format(lat, buf, new FieldPosition(0));
buf.append(','); coords.format(lon, buf, new FieldPosition(0));
buf.append(','); magnitude.format(mag, buf, new FieldPosition(0));
buf.append(','); depth.format(dep, buf, new FieldPosition(0));
buf.append(','); buf.append(nst);
buf.append(','); buf.append('"'); buf.append(region); buf.append('"');
return buf.toString();
}
}