java string数组 个数字_按多个数字排序Java String数组

我可能会破坏一些学生的家庭作业,但是这里……

正如问题所示,Java中的自然方式是创建一个表示数据的类.然后实现一个Comparator传递给实用程序方法Collections.sort.

在运行带有Java 8的Parallels虚拟机的MacBook Pro 2.3 GHz Intel Core i7上,42,000个元素的数据集需要45-90毫秒才能进行排序.

我将您的示例数据更改为更有趣,介绍了一些不同的日期和重复的纬度.

20000101,34.6920,-116.3550,12.30,1.21

20000101,34.4420,-116.2280,7.32,1.01

20000101,34.6920,-121.7667,5.88,1.14

20000101,-41.1300,174.7600,27.00,1.90

20000101,37.6392,-119.0482,2.40,1.03

20000101,32.1790,-115.0730,6.00,2.44

20000101,34.6920,-152.2192,86.34,1.48

20000102,34.6320,-116.2410,11.63,1.61

20000102,59.5369,-153.1360,100.15,1.62

20000102,44.7357,-110.7932,4.96,2.20

20000102,34.6320,-116.2950,9.00,1.73

20000102,34.6320,-110.7938,5.32,1.75

20000102,34.6320,-117.6320,4.15,1.45

20000102,41.9270,20.5430,10.00,4.80

我的GeoReading类来表示数据.

class GeoReading

{

LocalDate localDate = null;

BigDecimal latitude = null;

BigDecimal longitude = null;

BigDecimal depth = null;

BigDecimal magnitude = null;

public GeoReading( String arg )

{

// String is comma-separated values of: Date,Lat,Lon,Depth,Mag

List items = Arrays.asList( arg.split( "\\s*,\\s*" ) ); // Regex explained here: https://stackoverflow.com/a/7488676/642706

this.localDate = ISODateTimeFormat.basicDate().parseLocalDate( items.get( 0 ) );

this.latitude = new BigDecimal( items.get( 1 ) );

this.longitude = new BigDecimal( items.get( 2 ) );

this.depth = new BigDecimal( items.get( 3 ) );

this.magnitude = new BigDecimal( items.get( 4 ) );

}

@Override

public String toString()

{

return "GeoReading{" + "localDate=" + localDate + ", latitude=" + latitude + ", longitude=" + longitude + ", depth=" + depth + ", magnitude=" + magnitude + '}';

}

}

这是比较器实现.

class GeoReadingAscendingComparator implements Comparator

{

@Override

public int compare( GeoReading o1 , GeoReading o2 )

{

int localDateCompare = o1.localDate.compareTo( o2.localDate );

if ( localDateCompare != 0 ) { // If not equal on this component, so compare on this.

return localDateCompare;

}

int latitudeCompare = o1.latitude.compareTo( o2.latitude );

if ( latitudeCompare != 0 ) { // If not equal on this component, so compare on this.

return latitudeCompare;

}

return o1.longitude.compareTo( o2.longitude );

}

}

主要代码.

Path path = Paths.get( "/Users/basil/lat-lon.txt" ); // Path for Mac OS X.

try {

List list = new ArrayList<>();

Stream lines = Files.lines( path );

lines.forEach( line -> list.add( new GeoReading( line ) ) );

// Take those 14 lines and multiply to simulate large text file. 14 * 3,000 = 42,000.

int count = 3000;

List bigList = new ArrayList<>( list.size() * count ); // Initialze capacite to expected number of elements.

for ( int i = 0 ; i < count ; i++ ) {

bigList.addAll( list );

}

long start = System.nanoTime();

Collections.sort( bigList , new GeoReadingAscendingComparator() );

long elapsed = ( System.nanoTime() - start );

System.out.println( "Done sorting the GeoReading list. Sorting " + bigList.size() + " took: " + TimeUnit.MILLISECONDS.convert( elapsed , TimeUnit.NANOSECONDS ) + " ms ( " + elapsed + " nanos )." );

System.out.println( "Dump…" );

for ( GeoReading g : bigList ) {

System.out.println( g );

}

} catch ( IOException ex ) {

System.out.println( "ERROR - ex: " + ex );

}

在现实世界中,我会添加一些防御性编程代码来验证传入的数据.来自外部来源的数据总是有缺陷和/或变化.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值