利用java.io 和集合等知识点解答深交所关于按行读取本地数据文件并排序的问题

本文详细介绍了使用Java处理股票数据的过程,包括从本地文件读取数据、按日期排序股票价格,以及识别并打印出股价的单调递增区间。通过具体代码示例,展示了如何利用Java的IO和集合框架完成数据处理任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近去深交所进行了一次面试,结果还有机试题,感觉还有点技术含量的,所以回来把自己实现的代码逻辑整理到博客上来

机试题1:现在本地C盘中存有股票数据的文件stock_data.txt文件(我把次文件改放在D盘中的mysql5.6文件夹中),有一系列股价在前 日期在后的原始数据如下(数据是自己编的)

3.79 2018-10-03
3.86 2018-10-04
3.65 2018-10-02
4.22 2018-10-01
3.72 2018-10-05
5.25 2018-10-10
4.75 2018-10-09
4.15 2018-10-08
3.45 2018-10-06
3.00 2018-10-07

现要求你实现去读该文件中的数据,并按日期前后顺序重新排列好并打印出来,效果如下所示

2018-10-01 4.22
2018-10-02 3.65
2018-10-03 3.79
2018-10-04 3.86
2018-10-05 3.72
2018-10-06 3.45
2018-10-07 3.0
2018-10-08 4.15
2018-10-09 4.75
2018-10-10 5.25
机试题2:在第1题实现股票价格按日期升序排列后的基础上找出其中股票价格处于单调递增区间的项目,并一数组的形式将其打印出来。如2018-10-02~2018-10-04日的股价处理单调递增区间,打印出来的格式如下

[2018-10-02 3.65,2018-10-03 3.79,2018-10-04 3.86]

本人解决这两道机试题时主要用到了java.io包和java.util包下的File、FileReadder、BufferedReader、Date和List、Map等工具类

采用IntelliJ IDEA开发工具新建一个java project, 工程结构如下

贴上代码如下

测试类及实体类代码

package com.company;
import com.company.com.company.model.StockItemTO;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


import java.io.*;

public class Main {
    private static List<StockItemTO> stockItemList = new ArrayList<>();
    private static List<String> datePriceList = new ArrayList<>();
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    public static void main(String[] args) {
        readStockFileAndSort("D:\\mysql5.6\\stock_data.txt");
        // selectIncreaseItems(stockItemList);
    }

    /**
     * 实现读取本地文件D:\mysql5.6\stock_data.txt中的股价数据并实现按日期排序
     * @param filePath
     */

    public static void readStockFileAndSort(String filePath){
        File file = new File(filePath);
        try{
            FileReader fileReader = new FileReader(file);
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            String str = null;
            StockItemTO stockTO = null;
            String[] datePriceArrr = null;
            while((str=bufferedReader.readLine())!=null){
                datePriceArrr = str.split(" ");
                stockTO = new StockItemTO();
                stockTO.setPrice(Float.parseFloat(datePriceArrr[0]));
                stockTO.setDate(parseDate(datePriceArrr[1]));
                stockItemList.add(stockTO);
            }
            Collections.sort(stockItemList, new Comparator<StockItemTO>() {
                @Override
                public int compare(StockItemTO o1, StockItemTO o2) {
                    return o1.getDate().compareTo(o2.getDate());
                }
            });
            String itemStr = null;
            for(StockItemTO stockItemTO:stockItemList){
                itemStr = parseDateStr(stockItemTO.getDate())+" "+String.valueOf(stockItemTO.getPrice());
                datePriceList.add(itemStr);
                System.out.println(itemStr);
            }

        }catch(FileNotFoundException e1){
            e1.printStackTrace();
        }catch(IOException e2){
            e2.printStackTrace();
        }

    }

    /**
     * 找出股票上涨区间的数据,并按日期 股价的格式(如[2018-10-02 3.65,2018-10-03 3.79,2018-10-04 3.86])
     * 打印出来
     * @param sortedStockItems
     */
    public static void selectIncreaseItems(List<StockItemTO> sortedStockItems){
        //是否需要new 一个List标识
        boolean needNewFlag = false;
        int count = 0;
        boolean hasFirstNew = false;
        Map<String,List<StockItemTO>> stockItemsMap = new HashMap<String,List<StockItemTO>>();
        List<StockItemTO> increList = new ArrayList<>();
        StockItemTO compareTO = sortedStockItems.get(0);
        for(int i=1;i<sortedStockItems.size();i++){
            if(i==1 && sortedStockItems.get(i).getPrice()>compareTO.getPrice()){
                increList.add(sortedStockItems.get(0));
                increList.add(sortedStockItems.get(1));
                hasFirstNew = true;
            }else if(i>1 && i!=sortedStockItems.size()-1 && sortedStockItems.get(i).getPrice()>compareTO.getPrice()){
                if(!hasFirstNew){
                    increList.add(sortedStockItems.get(i-1));
                    hasFirstNew = true;
                }
                if(!needNewFlag){
                    increList.add(sortedStockItems.get(i));
                }else{
                    stockItemsMap.put(String.valueOf(count),increList);
                    increList = new ArrayList<>();
                    increList.add(sortedStockItems.get(i-1));
                    increList.add(sortedStockItems.get(i));
                }
            }else if(i>1 && i!=sortedStockItems.size()-1 && sortedStockItems.get(i).getPrice()<compareTO.getPrice()){
                //遇到连续递减区间时,只需在第一次开始递减时将increList放到stockItemsList中,并设置needNewFlag = true
                if(compareTO.getPrice()>sortedStockItems.get(i-2).getPrice()){
                    stockItemsMap.put(String.valueOf(count),increList);
                    needNewFlag = true;
                    count ++;
                }
            }else if(i==sortedStockItems.size()-1){
                if(sortedStockItems.get(i).getPrice()>compareTO.getPrice()){
                    increList.add(sortedStockItems.get(i));
                    stockItemsMap.put(String.valueOf(count),increList);
                }else{
                    stockItemsMap.put(String.valueOf(count),increList);
                }
            }
            compareTO = (StockItemTO) sortedStockItems.get(i);
        }
        if(!stockItemsMap.isEmpty()){
            List<List<String>> stockStrList = new ArrayList<List<String>>();
            List<String> itemStrList = null;
            List<StockItemTO> increItemList = null;
            StockItemTO increItemTO = null;

            for(Map.Entry<String,List<StockItemTO>> entry:stockItemsMap.entrySet()){
                itemStrList = new ArrayList<>();
                increItemList = entry.getValue();
                itemStrList = new ArrayList<>();
                for(int k=0;k<increItemList.size();k++){
                    increItemTO = increItemList.get(k);
                    itemStrList.add(parseDateStr(increItemTO.getDate())+" "+increItemTO.getPrice());
                }
                stockStrList.add(itemStrList);
            }
            for(int i=0;i<stockStrList.size();i++){
                System.out.println(stockStrList.get(i).toString());
            }
        }

    }

    public static Date parseDate(String dateStr){
        Date date = null;
        try{
            date = sdf.parse(dateStr);

        }catch (ParseException e){
            e.printStackTrace();
        }

        return date;
    }

    public static String parseDateStr(Date date){
        return sdf.format(date);
    }


}
package com.company.com.company.model;
import java.util.Date;

public class StockItemTO {
    private Date date;

    private float price;

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }
}

3. 解决机试题1时在Main函数中调用静态方法

readStockFileAndSort("D:\\mysql5.6\\stock_data.txt");

运行后控制台打印内容如下:

打印内容表明代码实现了机试题1中要求的功能

4. 解决机试题2时屏蔽

readStockFileAndSort(String filePath){}静态函数中打印信息的代码,第51行:System.out.println(itemStr);
同时调用静态方法readStockFileAndSort("D:\\mysql5.6\\stock_data.txt");和
selectIncreaseItems(stockItemList);

运行后控制台打印内容如下,表明代码实现了机试题2要求的功能

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

heshengfu1211

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值