gpt4 book ai didi

java - 阅读独特的值(value)观

转载 作者:行者123 更新时间:2023-12-01 11:53:36 26 4
gpt4 key购买 nike

我编写了一段代码,用于从文本文件的列中读取值。为了输出值的数量,我使用了“长度”,效果很好......但我只需要计算唯一值的数量。

public class REading_Two_Files {

public static void main(String[] args) {

try {
readFile(new File("C:\\Users\\teiteie\\Desktop\\RECSYS\\yoochoose-test.csv"), 0,( "C:\\Users\\teiteie\\Desktop\\RECSYS\\yoochoose-buys.csv"), 3);
//readFile(new File(File1,0, File2,3);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

////0 - 将打印 file1 中的列 //3 - 将打印文件 2 中的列

private static void readFile(File fin1,int whichcolumnFirstFile,String string,int whichcolumnSecondFile) throws IOException {
//private static void readFile(File fin1,int whichcolumnFirstFile,String string,int whichcolumnSecondFile) throws IOException
// code for this method.
//open the two files.

int noSessions = 0;
int noItems = 0;
// HashSet<String> uniqueLength = new HashSet<String>();


FileInputStream fis = new FileInputStream(fin1); //first file
FileInputStream sec = new FileInputStream(string); // second file

//Construct BufferedReader from InputStreamReader
BufferedReader br1= new BufferedReader(new InputStreamReader(fis));
BufferedReader br2= new BufferedReader(new InputStreamReader(sec));

String lineFirst = null, first_file[];
String lineSec = null, second_file [];

while ((lineFirst = br1.readLine()) != null && (lineSec = br2.readLine()) != null) {
first_file= lineFirst.split(",");
second_file = lineSec.split(",");

//int size = data[].size();

System.out.println(first_file[0]+" , "+second_file[0]);



if(first_file.length != 0){
noSessions++;
}

if(second_file.length != 0) {
noItems ++;
}



}

br1.close();
br2.close();
System.out.println("no of sessions "+noSessions+"\nno of items "+noItems );


}


}

最佳答案

为了仅计算唯一值,我们通常使用 Set因为它们被指定为仅包含唯一值。

A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction.

本质上 - 将所有值放入 Set 中(通常 HashSet 是最有效的,但如果您想要并发性,有更好的选择),然后采用 Set.size() 作为您输入的唯一值的数量。

关于java - 阅读独特的值(value)观,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28605772/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com