阿里云简单笔试题

目录

 

数据脱敏

文件操作

多线程

数据处理

停止与唤醒

数据统计


数据脱敏

写个函数,把下面字符中的邮箱地址脱敏成******格式 待匹配字符:今天天气很好,我发了个邮件给[email protected],[email protected][email protected]

import org.apache.commons.lang3.StringUtils;

/**   
  *  
  *
  * @Description:  邮件格式脱敏  
  * @Author:       zhang_fan  
  * @CreateDate:   2019-03-20 10:58   
  * @Version:      v1.0 
  *    
  */
public class EmailDesensitization {

    /**
     * 程序入口
     *
     * @param args
     */
    public static void main(String[] args){
        String content = "今天天气很好,我发了个邮件给[email protected],[email protected][email protected]";
        System.out.println(emailDesensitization(content));
    }

    /**
     * 邮件格式脱敏
     *
     * @param content
     *  :要脱敏的内容
     * @return
     *  :脱敏后的内容
     */
    public static String emailDesensitization(String content){
        if(StringUtils.isEmpty(content)){
            return "要脱敏的数据不能为空";
        }
        char[] chars = content.toCharArray();
        // 无多线程环境
        StringBuilder stringBuilder = new StringBuilder();
        boolean isDesensitization = false;
        for (int i = 0; i < chars.length ; i++) {
            if("给".toCharArray()[0] == chars[i]){
                isDesensitization = true;
                stringBuilder.append(chars[i]);
                continue;
            }
            // 不需要脱敏的字符【这里是可变的部分,还可以再优化一下。变与不变分离】
            if(",".toCharArray()[0] == chars[i] ||
                    ",".toCharArray()[0] == chars[i] ||
                    "和".toCharArray()[0] == chars[i]){
                stringBuilder.append(chars[i]);
                continue;
            }
            if(isDesensitization){
                stringBuilder.append("*");
                continue;
            }
            stringBuilder.append(chars[i]);

        }
        return stringBuilder.toString();
    }
}

文件操作

有一个文本文件backup.txt,写个程序把这个文件每行行尾的三个字符都写入到新的文件里面

import org.apache.commons.lang3.StringUtils;

import java.io.*;

/**   
  *  
  *
  * @Description:  有一个文本文件backup.txt,写个程序把这个文件每行行尾的三个字符都写入到新的文件里面  
  * @Author:       zhang_fan  
  * @CreateDate:   2019-03-21 10:52   
  * @Version:      v1.0 
  *    
  */
public class Rewrite {

    /**
     * 程序入口
     *
     * @param args
     */
    public static void main(String[] args){
        String input = "/Users/zhangfan/log";
        String output = "/Users/zhangfan/out";
        try {
            rewrite(input,output);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 有一个文本文件backup.txt,写个程序把这个文件每行行尾的三个字符都写入到新的文件里面
     * (可以考虑开启多线程来优化,用LineNumberReader来实现)
     *
     * @param input
     *  :原始文件路径
     * @param output
     *  :输入文件路径
     * @return
     */
    public static void rewrite(String input,String output) throws Exception {
        if(StringUtils.isEmpty(input) || StringUtils.isEmpty(output)){
            return;
        }
        File fin = new File(input);
        BufferedReader br = new BufferedReader(new FileReader(fin));

        String line = null;
        BufferedWriter bw = new BufferedWriter(
                new OutputStreamWriter(
                        new FileOutputStream(new File(output)), "UTF-8"));
        try{
            while ((line = br.readLine()) != null) {
                if((line.length()-3) <= 0){
                    bw.write(line);
                    bw.newLine();
                    continue;
                }
                bw.write(line.substring(line.length()-3));
                bw.newLine();
            }
        } finally {
            bw.close();
        }
    }
}

多线程

数据处理

用多线程去处理"123","456",“789”这个三个字符串,然后以"147","258","369"这种形式去输出,请写出代码实现

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.FutureTask;


/**
 *
 * @Description:  用多线程去处理"123","456",“789”这个三个字符串,
 * 然后以"147","258","369"这种形式去输出,请写出代码实现  
 * @Author:       zhang_fan  
 * @CreateDate:   2019-03-21 11:10   
 * @Version:      v1.0 
 *     
 */
public class Format {

    /**
     * 程序入口
     *
     * @param args
     */
    public static void main(String[] args) {
        try {
            format(new String[]{"123","456","789"});
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 用多线程去处理"123","456",“789”这个三个字符串,
     * 然后以"147","258","369"这种形式去输出,请写出代码实现 
     *
     * @param numbers
     * @return
     */
    public static String[] format(final String[] numbers) throws Exception {
        // 存储执行结果的List
        List> results = new ArrayList>();
        final CountDownLatch latch = new CountDownLatch(numbers.length);
        for (int i = 0; i < numbers.length; i++) {
            final int finalI = i;
            FormatThread formatThread = new FormatThread(numbers,finalI,latch);
            FutureTask faeature = new FutureTask(formatThread);
            new Thread(faeature).start();//注意启动方式,FutureTask将被作为Runnable被线程执行
            results.add(faeature);
        }
        //主线程
        latch.await();//阻塞当前线程直到latch中数值为零才执行
        for (FutureTask futureTask : results) {
            System.out.println(futureTask.get());
        }
        return null;
    }

    public static class FormatThread implements Callable {
        String[] numbers;
        Integer index;
        CountDownLatch countDownLatch;

        public FormatThread(){}

        public FormatThread(String[] numbers, Integer index, CountDownLatch latch){
            this.numbers = numbers;
            this.index = index;
            this.countDownLatch = latch;
        }

        @Override
        public String call() throws Exception {
            try {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append(numbers[0].toCharArray()[index]);
                stringBuilder.append(numbers[1].toCharArray()[index]);
                stringBuilder.append(numbers[2].toCharArray()[index]);
                return stringBuilder.toString();
            } finally {
                countDownLatch.countDown();
            }
        }
    }
}

停止与唤醒

【参考文章】https://blog.csdn.net/jianggujin/article/details/50458486

有多个线程,代表行使的汽车,写一个红绿灯函数,等红灯亮时把所有汽车线程都暂停,当绿灯亮时把所有线程都启动起来,

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**   
  *  
  *
  * @Description:  有多个线程,代表行使的汽车,写一个红绿灯函数,
  * 等红灯亮时把所有汽车线程都暂停,当绿灯亮时把所有线程都启动起来  
  * @Author:       zhang_fan  
  * @CreateDate:   2019-03-22 12:58   
  * @Version:      v1.0 
  *    
  */
public class CarAndRrafficLight {

    /**
     * 程序入口
     * @param args
     */
    public static void main(String[] args) {
        RrafficLight rrafficLight1 = new RrafficLight();
        RrafficLight rrafficLight2 = new RrafficLight();
        RrafficLight rrafficLight3 = new RrafficLight();
        Car car1 = new Car("小汽车1",rrafficLight1);
        Car car2 = new Car("小汽车2",rrafficLight2);
        Car car3 = new Car("小汽车3",rrafficLight3);
        car1.start();
        car2.start();
        car3.start();
        List rrafficLightList = new ArrayList();
        rrafficLightList.add(rrafficLight1);
        rrafficLightList.add(rrafficLight2);
        rrafficLightList.add(rrafficLight3);
        //创建定时器对象
        Timer t = new Timer();
        //在2秒后执行MyTask类中的run方法
        t.schedule(new ModifyColorOfLightTask(rrafficLightList), 5000, 5000);

    }

    static class Car extends Thread {
        RrafficLight rrafficLight;

        public Car(String name,RrafficLight rrafficLight) {
            super(name);
            this.rrafficLight = rrafficLight;
        }

        public void run() {
            while (true) {
                synchronized (rrafficLight) {
                    try {
                        if (rrafficLight.color.hashCode() == Color.READ.hashCode()) {
                            System.out.println("红灯:"+Thread.currentThread().getName() + "停止行驶");
                            rrafficLight.wait();
                        } else if(rrafficLight.color.hashCode() == Color.GREEN.hashCode()){
                            System.out.println("绿灯:"+Thread.currentThread().getName() + "继续行驶");
                            sleep(500);
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    /**
     * 红绿灯
     */
    static class RrafficLight {

        Color color = Color.GREEN;

        public void changingBlinker() {
            if (color.hashCode() == Color.READ.hashCode()) {
                color = Color.GREEN;
            } else {
                color = Color.READ;
            }
        }
    }

    /**
     * 颜色枚举
     */
    enum Color {
        READ, GREEN;

        @Override
        public String toString() {
            switch (this) {
                case READ:
                    return "红灯";

                case GREEN:
                    return "绿灯";
            }
            return "异常灯";
        }
    }

    /**
     * 修改灯的颜色
     */
    static class ModifyColorOfLightTask extends TimerTask {
        List rrafficLights;

        public ModifyColorOfLightTask(List rrafficLights) {
            this.rrafficLights = rrafficLights;
        }

        @Override
        public void run() {
            for (RrafficLight rrafficLight : rrafficLights) {
                rrafficLight.changingBlinker();
                synchronized (rrafficLight){
                    rrafficLight.notifyAll();
                }
            }
        }
    }
}

数据统计

有一个非常大的文件,计算一下文件里面出现单词alibaba的次数

 

 

用hadoop的mapreduce.

 

 

你可能感兴趣的:(面试相关)