第一题
public static void main ( String [ ] args) {
System . out. println ( sum ( 100 ) ) ;
}
public static int sum ( int n) {
if ( n == 1 ) {
return 1 ;
} else {
return sum ( n - 1 ) + n;
}
}
第二题
public static void main ( String [ ] args) {
List < String > list = Arrays . asList ( "tom" , "jack" , "marry" , "vicky" ) ;
for ( String str : list) {
if ( str. contains ( "ck" ) ) {
str = str. toUpperCase ( ) ;
str += "@1000phone.com" ;
}
System . out. println ( str) ;
}
}
第三题
public class Product {
private Integer id;
private String name;
private Double price;
private String message;
public Product ( ) {
}
public Product ( Integer id, String name, Double price, String message) {
this . id = id;
this . name = name;
this . price = price;
this . message = message;
}
public Integer getId ( ) {
return id;
}
public void setId ( Integer id) {
this . id = id;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public Double getPrice ( ) {
return price;
}
public void setPrice ( Double price) {
this . price = price;
}
public String getMessage ( ) {
return message;
}
public void setMessage ( String message) {
this . message = message;
}
@Override
public int hashCode ( ) {
final int prime = 31 ;
int result = 1 ;
result = prime * result + ( ( id == null ) ? 0 : id. hashCode ( ) ) ;
return result;
}
@Override
public boolean equals ( Object obj) {
if ( this == obj)
return true ;
if ( obj == null )
return false ;
if ( getClass ( ) != obj. getClass ( ) )
return false ;
Product other = ( Product ) obj;
if ( id == null ) {
if ( other. id != null )
return false ;
} else if ( ! id. equals ( other. id) )
return false ;
return true ;
}
@Override
public String toString ( ) {
return "编号:" + id + ", 名称:" + name + ", 价格:" + price + ", 详细描述:" + message;
}
}
public static void main ( String [ ] args) {
List < Product > list = new ArrayList < > ( ) ;
list. add ( new Product ( 23001 , "产品1" , 5500.0 , "描述--" ) ) ;
list. add ( new Product ( 23002 , "产品2" , 3500.0 , "描述--" ) ) ;
list. add ( new Product ( 23003 , "产品3" , 4500.0 , "描述--" ) ) ;
list. sort ( new Comparator < Product > ( ) {
@Override
public int compare ( Product o1, Product o2) {
int res = o1. getPrice ( ) . compareTo ( o2. getPrice ( ) ) ;
return res;
}
} ) ;
for ( Product p : list) {
System . out. println ( p) ;
}
}
第四题
public static void main ( String [ ] args) throws IOException {
Map < Integer , Product > map = new HashMap < > ( ) ;
File file = new File ( "E:\\QQdown\\test.txt" ) ;
FileReader reader = new FileReader ( file) ;
BufferedReader br = new BufferedReader ( reader) ;
String str;
while ( ( str = br. readLine ( ) ) != null ) {
String [ ] arr = str. split ( "/" ) ;
map. put ( Integer . parseInt ( arr[ 0 ] ) ,
new Product ( Integer . parseInt ( arr[ 0 ] ) , arr[ 1 ] , Double . parseDouble ( arr[ 2 ] ) , arr[ 3 ] ) ) ;
}
Set < Entry < Integer , Product > > set = map. entrySet ( ) ;
set. stream ( ) . sorted ( ( o1, o2) -> o2. getKey ( ) . compareTo ( o1. getKey ( ) ) )
. forEach ( t -> System . out. println ( "Key:" + t. getKey ( ) + " " + t. getValue ( ) ) ) ;
}
第五题
public static void main ( String [ ] args) throws InterruptedException , ExecutionException {
ExecutorService es = Executors . newFixedThreadPool ( 3 ) ;
long t1 = System . currentTimeMillis ( ) ;
Future < Integer > f1 = es. submit ( ( ) -> {
int sum = 0 ;
for ( int i = 1 ; i <= 10000 ; i++ ) {
sum += i;
}
return sum;
} ) ;
Future < Integer > f2 = es. submit ( ( ) -> {
int sum = 0 ;
for ( int i = 10001 ; i <= 20000 ; i++ ) {
sum += i;
}
return sum;
} ) ;
Future < Integer > f3 = es. submit ( ( ) -> {
int sum = 0 ;
for ( int i = 20001 ; i <= 30000 ; i++ ) {
sum += i;
}
return sum;
} ) ;
int sum = f1. get ( ) + f2. get ( ) + f3. get ( ) ;
long t2 = System . currentTimeMillis ( ) ;
System . out. println ( "总和:" + sum) ;
System . out. println ( "耗时:" + ( t2 - t1) ) ;
es. shutdown ( ) ;
}