几个面试题

第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;

/*
 数组String[] strs中有很多重复的元素,找出出现最多的
 元素,并返回出现的次数,(30分)
 */
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;
/*
 有n个SQL任务List<SQL> task需要执行update,写一个方法
 要求最多100条SQL执行一次事务提交commit,直到执行完毕,不漏任务
 用到的知识点 批量的概念
 */
import java.sql.PreparedStatement;

public class Test2 {

    public static void main(String[] args) {
        test2();
    }
    //用jdbc的批量方法 
    private static void test2() {
        long start  =System.currentTimeMillis();
        //向表中添加1000条数据
        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 ); //1503毫秒
    }
    private static void test1() {
        long start  =System.currentTimeMillis();

        //向表中添加1000条数据
        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 ); //21503毫秒
    }
}

第3题

package com.demo;

import java.util.ArrayList;
import java.util.List;

/*
 1.写一个方法,将1000元现金随机发给20个客户,
 每个客户的金额在30~100之间(30分)
 */
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); // 0---1 0.999999
                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);

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值