第1题
package com .demo
import java.util .ArrayList
import java.util .Collection
import java.util .Collections
import java.util .HashMap
import java.util .List
import java.util .Map
import java.util .Map .Entry
import java.util .Set
public class Test1 {
public static void main(String[] args) {
String[] strs={"张无忌" ,"赵敏" ,"aaa" ,"小昭" ,"张无忌" ,"赵敏" ,"aaa" ,"张无忌" }
Map<String,Integer> map = new HashMap<>()
for (String data : strs) {
//检查map中是否包括data
if(map.containsKey (data)){
//把对应的value加1
Integer count = map.get (data)
count++
map.put (data, count)
}else{ //不包括
map.put (data, 1 )
}
}
System.out .println (map)
Collection<Integer> values = map.values ()
List<Integer> list = new ArrayList<>(values)
Collections.sort (list)
int count = list.get (list.size ()-1 )
Set <Entry<String, Integer>> entrySet = map.entrySet ()
for (Entry<String, Integer> entry : entrySet) {
if(entry.getValue () == count){
System.out .println (entry.getKey ()+"-->" +entry.getValue ())
break
}
}
}
}
第2题
package com.demo;
import java.sql.Connection;
import java.sql.PreparedStatement;
public class Test2 {
public static void main (String[] args) {
test2();
}
private static void test2 () {
long start =System.currentTimeMillis();
String sql="insert into dept(dname,loc) values(?,?)" ;
Connection conn =DaoFactory.getConnection();
PreparedStatement stmt = null ;
try {
conn.setAutoCommit(false );
stmt = conn.prepareStatement(sql);
for (int i=1 ; i<=1000 ; i++){
stmt.setString(1 , "a" +i);
stmt.setString(2 , "b" +i);
stmt.addBatch();
if ( i % 100 == 0 ){
stmt.executeBatch();
stmt.clearBatch();
}
}
conn.commit();
} catch (Exception e) {
e.printStackTrace();
}finally {
DaoFactory.closeAll(null , stmt, conn);
}
long end =System.currentTimeMillis();
System.out .println( end-start );
}
private static void test1 () {
long start =System.currentTimeMillis();
String sql="insert into dept(dname,loc) values(?,?)" ;
Connection conn =DaoFactory.getConnection();
PreparedStatement stmt = null ;
try {
stmt = conn.prepareStatement(sql);
for (int i=0 ; i<1000 ; i++){
stmt.setString(1 , "a" +i);
stmt.setString(2 , "b" +i);
stmt.executeUpdate();
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DaoFactory.closeAll(null , stmt, conn);
}
long end =System.currentTimeMillis();
System.out .println( end-start );
}
}
第3题
package com.demo;
import java.util.ArrayList;
import java.util.List;
public class Test3 {
public static void main (String[] args) {
boolean flag = true ;
while (flag) {
int sum = 0 ;
List<Integer> list = new ArrayList<Integer>();
for (int i = 1 ; i <= 20 ; i++) {
int num = (int ) (Math.random() * 70 + 30 );
sum += num;
list.add(num);
if (sum == 1000 && i == 20 ) {
System.out .println(list);
flag = false ;
}
}
}
}
}
package com.demo;
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class Test4 {
public static void main (String[] args) {
double num = Math.random();
System.out .println(num);
System.out .println(String.format("%.2f" , num));
System.out .format("%.2f\n" , num);
DecimalFormat format = new DecimalFormat("0.00" );
String strNum = format.format(num);
System.out .println(strNum);
}
}