妙用Commons良药<四>
谈一谈Math包的一点内容,和怎样对属性文件,xml文件进行访问
1、关于org.apache.commons.lang.math的应用
可使用Commons Lang的NumberUtils.max()和NumberUtils.min()方法来从基本类型数组(如
double[],float[],long[],int[],short[],byte[])中检索出最小或最大值.举例:
import org.apache.commons.lang.math.NumberUtils;
double[] array = {0.2, 0.4, 0.5, -3.0, 4.223, 4.226};
double max = NumberUtils.max( array ); // returns 4.226
double min = NumberUtils.min( array ); // returns -3.0
当然,你可以处理数学的范围,需要为某个变量定义一组可接受的值,并检测该变量值是否落在指定的范围内.Range是一个接口,定义了一个简单的数值范围,它有几个不同的实现,如NumberRange,DoubleRange,FloatRange,IntRange以及LongRange等等.
import org.apache.commons.lang.math.DoubleRange;
import org.apache.commons.lang.math.Range;
Range safeSpeed = new DoubleRange( 0.0, 65.0 );
double currentSpeed = getCurrentSpeed( );
if( !safeSpeed.containsDouble( currentSpeed ) ) {
System.out.println( "Warning, current speed is unsafe." );
}
如果你想生成一个随机数,可以这样写:
import org.apache.commons.lang.math.RandomUtils;
// Create a random integer between 0 and 30
int maxVal = 30;
int randomInt = RandomUtils.nextInt( maxVal );
其作用相当于:int randomInt = (int) Math.floor( (Math.random( ) * (double) maxVal) );
此外提示一点,RandomUtils.nextBoolean()可以返回一个随机的boolean变量
2、关于Jakarta Commons Math包一点应用
举一个例子,如下:
import org.apache.commons.math.stat.StatUtils;
double[] values = new double[] { 2.3, 5.4, 6.2, 7.3, 23.3 };
System.out.println( "min: " + StatUtils.min( values ) );
System.out.println( "max: " + StatUtils.max( values ) );
System.out.println( "平均值: " + StatUtils.mean( values ) );
System.out.println( "积: " + StatUtils.product( values ) );
System.out.println( "和: " + StatUtils.sum( values ) );
System.out.println( "方差: " + StatUtils.variance( values ) );
当然,这一个包也可以处理复数,矩阵,统计以及线性方程,具体的用法参考官方的文档
3、使用Commons Configuration包配置应用程序
如果想访问属性文件的话,可以运用PropertiesConfiguration这一个类:
属性文件如下:
speed=23.332
names=Bob,Gautam,Jarret,Stefan
correct=false
其代码如下:
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.PropertiesConfiguration;
Configuration config = new PropertiesConfiguration( "test.properties" );
float speed = config.getFloat("speed"));
List names = config.getList("names"));
boolean correct = config.getBoolean("correct");
如果想访问一个xml文件里面具体的属性,可以使用XMLConfiguration的一个实现从某个文档中加载配置参数。举例如下:
其配置文件:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<engine-config>
<start-criteria>
<criteria type="critical">
Temperature Above -10 Celsius
</criteria>
<criteria> Fuel tank is not empty </criteria>
</start-criteria>
<name>
<first>Tom</first>
<last>Payne</last>
</name>
<horsepower>42</horsepower>
</engine-config>
具体访问的代码如下:
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.DOMConfiguration;
String resource = "com/discursive/jccook/configuration/global.xml";
Configuration config = new DOMConfiguration(resource);
// Retrieve a list of all Criteria elements
List startCriteria = config.getList("start-criteria.criteria");
// Retrieve the value of the first criteria element
String firstCriteria = config.getString("start-criteria.criteria(0)");
// Retrieve the type attribute of the first criteria element
String firstCriteriaType = config.getString("start-criteria.criteria(0)[@type]");
// Retrieve the horsepower as an int
int horsepower = config.getInt("horsepower");
分享到:
相关推荐
<artifactId>commons-fileupload</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.5</version> </...
commons-attributes-2.2-src.zip<br>commons-attributes-2.2.zip<br>commons-beanutils-1.8.0-BETA-src.zip<br>commons-beanutils-1.8.0-BETA.zip<br>commons-betwixt-0.8-src.zip<br>commons-betwixt-0.8.zip<br>...
commons-attributes-2.2-src.zip<br>commons-attributes-2.2.zip<br>commons-beanutils-1.8.0-BETA-src.zip<br>commons-beanutils-1.8.0-BETA.zip<br>commons-betwixt-0.8-src.zip<br>commons-betwixt-0.8.zip<br>...
commons-attributes-2.2-src.zip<br>commons-attributes-2.2.zip<br>commons-beanutils-1.8.0-BETA-src.zip<br>commons-beanutils-1.8.0-BETA.zip<br>commons-betwixt-0.8-src.zip<br>commons-betwixt-0.8.zip<br>...
commons-attributes-2.2-src.zip<br>commons-attributes-2.2.zip<br>commons-beanutils-1.8.0-BETA-src.zip<br>commons-beanutils-1.8.0-BETA.zip<br>commons-betwixt-0.8-src.zip<br>commons-betwixt-0.8.zip<br>...
commons-attributes-2.2-src.zip<br>commons-attributes-2.2.zip<br>commons-beanutils-1.8.0-BETA-src.zip<br>commons-beanutils-1.8.0-BETA.zip<br>commons-betwixt-0.8-src.zip<br>commons-betwixt-0.8.zip<br>...
添加依赖: <dependency> <groupId>com.itextpdf</groupId> <artifactId>itext7-core... <groupId>org.apache.commons</groupId> <artifactId>commons-io</artifactId> <version>1.3.2</version> </dependency>
commons-attributes-2.2-src.zip<br>commons-attributes-2.2.zip<br>commons-beanutils-1.8.0-BETA-src.zip<br>commons-beanutils-1.8.0-BETA.zip<br>commons-betwixt-0.8-src.zip<br>commons-betwixt-0.8.zip<br>...
commons-attributes-2.2-src.zip<br>commons-attributes-2.2.zip<br>commons-beanutils-1.8.0-BETA-src.zip<br>commons-beanutils-1.8.0-BETA.zip<br>commons-betwixt-0.8-src.zip<br>commons-betwixt-0.8.zip<br>...
<artifactId>commons-httpclient</artifactId> <version>3.0</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </...
<version>${commons-lang3.version}</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.spring...
<artifactId>commons-collections4</artifactId> <version>4.1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </...
struts-1.3.8 包(<br><br>antlr-2.7.2.jar<br>bsf-2.3.0.jar<br>commons-beanutils-1.7.0.jar<br>commons-chain-1.1.jar<br>commons-digester-1.8.jar<br>commons-fileupload-1.1.1.jar<br>commons-io-1.1.jar<br>...
<artifactId>standard</artifactId> <version>1.1.2</version> <artifactId>jstl</artifactId> ... <artifactId>commons-io</artifactId> <version>2.11.0</version> <artifactId>javax.servlet-api</artifactId>
<artifactId>commons-io</artifactId> <version>2.11.0</version> <artifactId>mybatis</artifactId> <version>3.5.5</version> <artifactId>mysql-connector-java</artifactId> <version>8.0.28</version> ...
其中包含:shale-core.jar<br>commons-beanutils.jar<br>commons-chain.jar<br>commons-codec.jar<br>commons-collections.jar<br>commons-digester.jar<br>commons-el.jar<br>commons-fileupload.jar<br>commons-...
<module>cloud-api-commons</module> <module>cloud-eureka-server7001</module> <module>cloud-eureka-server7002</module> <module>cloud-comsumer-order80</module> <module>cloud-provider-payment8001</...
collections-2.1.1.jar<br>commons-logging-1.0.4.jar<br>dom4j-1.6.1.jar<br>ehcache-1.1.jar<br>hibernate3.jar<br>jaas.jar<br>jaxen-1.1-beta-7.jar<br>jdbc2_0-stdext.jar<br>jta.jar<br>log4j-1.2.11.jar<br>...
api.jar<br>jxl.jar<br>itext-1.3.jar<br>poi-2.5.1.jar<br>cos.jar<br>velocity-1.5.jar<br>velocity-tools-view-1.3.jar<br>commons-codec.jar<br>antlr.jar<br>commons-beanutils.jar<br>commons-digester.jar<br...
ssm在maven项目管理下的pom文件,jar包依赖 <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> ... <artifactId>commons-codec</artifactId> <version>1.10</ver