zoukankan      html  css  js  c++  java
  • java csvutil

    package com.test.util;
    import com.csvreader.CsvReader;
    import jdk.nashorn.internal.runtime.regexp.joni.exception.ValueException;
    import java.util.Random;
    import java.util.List;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Arrays;
    
    public class CsvUtil {
    
        public static Map  getUserId(String path,int uidNum,int colsNum){
            Map<String,String> map =new HashMap();
            Random rand =new Random();
            List idList= new ArrayList();
            try{
                CsvReader csvReader = new CsvReader(path);
                csvReader.readHeaders();
                List featureCols= new ArrayList();
                String[] headArray =csvReader.getHeaders();
                for (int i = 2; i < headArray.length; i++) {
                    featureCols.add(headArray[i]);
                }
                int colMax=featureCols.size()-2;
                if(colsNum<=colMax && colsNum>0){
    
                }else{
                    throw new ValueException("colNum must more than 0 and  less than "+colMax);
                }
                int colStart =rand.nextInt(featureCols.size()-colsNum+1);
                List temp =featureCols.subList(colStart,colStart+colsNum);
                //todo get header
                map.put("header",String.join(",",temp));
                while (csvReader.readRecord()) {
                    String uid= csvReader.get(0);
                    idList.add(uid);
                }
                }catch (Exception e){
                e.printStackTrace();
            }
            int uidMax=idList.size();
            if (uidNum<=uidMax && uidNum>0){
    
            }else{
                throw new ValueException("uidNum must more than 0 and less than  "+uidMax);
            }
            int uidStart =rand.nextInt(uidMax-uidNum+1);
            List temp2 =idList.subList(uidStart,uidStart+uidNum);
            map.put("userId",String.join(",",temp2));
            return map;
        }
    
    
    
        public  static List<String>  stringToList(String str){
            return  Arrays.asList(str.split(","));
        }
    
    }

    javacsv-2.0.jar

  • 相关阅读:
    Linux 文件及目录管理命令基础
    MHA高可用及读写分离
    MySQL的备份和回复
    mysql的主从复制
    MySQL索引管理及执行计划
    [LeetCode]Linked List Cycle II解法学习
    浅谈reverse_iterator的base()函数
    [LeetCode]LRU Cache有个问题,求大神解答【已解决】
    分享一篇不错的博文《写给准备参加秋招的学弟学妹们~一定要来看哦~》
    将博客搬至CSDN
  • 原文地址:https://www.cnblogs.com/SunshineKimi/p/14004197.html
Copyright © 2011-2022 走看看