如何使用java为输入逗号分隔文件筛选具有高于平均值的特定列值的行
假设我有一个文件,其中包含逗号分隔格式的数据,如下所示如何使用java为输入逗号分隔文件筛选具有高于平均值的特定列值的行,java,csv,lambda,java-8,generic-collections,Java,Csv,Lambda,Java 8,Generic Collections,假设我有一个文件,其中包含逗号分隔格式的数据,如下所示 TIMESTAMP,COUNTRYCODE,RESPONSETIME 1544190995,US,500 1723922044,GB,370 1711557214,US,750 如何使用java按高于平均值的RESPONSETIME筛选行?即此处RESPONSETIME的平均值为526。因此,我需要显示RESPONSETIME大于526的所有行。此处数据线不保证按任何特定顺序排列。我们可以在单个方法中同时执行(查找平均值
TIMESTAMP,COUNTRYCODE,RESPONSETIME
1544190995,US,500
1723922044,GB,370
1711557214,US,750
如何使用java按高于平均值的RESPONSETIME筛选行?即此处RESPONSETIME的平均值为526。因此,我需要显示RESPONSETIME大于526的所有行。此处数据线不保证按任何特定顺序排列。我们可以在单个方法中同时执行(查找平均值和筛选RESPONSETIME高于平均值的行)吗
目前我发现平均值如下。如何在同一方法中应用筛选器并作为集合返回?据我所知,在同一方法中不可能读取文件两次
public static Collection<?> filterByResponseTimeAboveAverage(Reader source) {
BufferedReader br = new BufferedReader(source);
String line = null;
Collection<String> additionalList = new ArrayList<String>();
int iteration = 0;
String[] myArray = null;
long count=0;
long responseTime=0;
long sum=0;
int numOfResponseTime=0;
long average=0;
List<String> myList = new ArrayList<String>();
try
{
while ((line = br.readLine()) != null) {
System.out.println("Inside while");
if (iteration == 0) {
iteration++;
continue;
}
myArray = line.split(",");
for (String eachval:myArray)
{
boolean isNumeric = eachval.chars().allMatch(x -> Character.isDigit(x));
//since input dataline is not guaranted to be in any particular order I am finding RESPONSETIME like this
if (isNumeric)
{
count=eachval.chars().count();
if (count<10)
{
responseTime=Integer.parseInt(eachval);
sum=sum+responseTime;
numOfResponseTime++;
}
}
myList.add(eachval);
}
}
average=sum/numOfResponseTime;
System.out.println("Average -- "+average);
---------------
---------------
}
公共静态集合筛选器ByResponseetimeabovaverage(读卡器源){
BufferedReader br=新的BufferedReader(源);
字符串行=null;
集合additionalList=新的ArrayList();
int迭代=0;
字符串[]myArray=null;
长计数=0;
长响应时间=0;
长和=0;
int numOfResponseTime=0;
长期平均=0;
List myList=new ArrayList();
尝试
{
而((line=br.readLine())!=null){
System.out.println(“内部while”);
如果(迭代==0){
迭代++;
继续;
}
myArray=line.split(“,”);
for(字符串eachval:myArray)
{
boolean isNumeric=eachval.chars().allMatch(x->Character.isDigit(x));
//因为输入数据线不能保证以任何特定的顺序排列,所以我发现响应时间如下
如果(是数字)
{
count=eachval.chars().count();
如果(计算)
如何在同一方法中应用筛选器并作为集合返回
使用
如何在同一方法中应用筛选器并作为集合返回
使用
据我所知,一个文件不可能在里面读两遍
同样的方法
public static Collection<?> filterByResponseTimeAboveAverage(Reader source) {
BufferedReader br = new BufferedReader(source);
String line = null;
Collection<String> additionalList = new ArrayList<String>();
int iteration = 0;
String[] myArray = null;
long count=0;
long responseTime=0;
long sum=0;
int numOfResponseTime=0;
long average=0;
List<String> myList = new ArrayList<String>();
try
{
while ((line = br.readLine()) != null) {
System.out.println("Inside while");
if (iteration == 0) {
iteration++;
continue;
}
myArray = line.split(",");
for (String eachval:myArray)
{
boolean isNumeric = eachval.chars().allMatch(x -> Character.isDigit(x));
//since input dataline is not guaranted to be in any particular order I am finding RESPONSETIME like this
if (isNumeric)
{
count=eachval.chars().count();
if (count<10)
{
responseTime=Integer.parseInt(eachval);
sum=sum+responseTime;
numOfResponseTime++;
}
}
myList.add(eachval);
}
}
average=sum/numOfResponseTime;
System.out.println("Average -- "+average);
---------------
---------------
}
你可以这样做,但你不应该这样做,因为这是没有效率的
你主要有两种方法
优化方式:
- 读取文件中的所有值并计算
响应时间的平均值
- 过滤高于平均值的值
您可以引入一个由FilterByResponseTimeabovaverage()
调用的私有方法,从源代码中检索所有值并计算它们的平均值。
功能性方式(开销稍贵):
- 从文件中读取所有值
- 使用
IntStream.average()
计算响应时间的平均值
- 过滤高于平均值的值
对于第二个也是最后一个步骤,它可能类似于:
double average = list
.stream()
.mapToInt(MyObject::getAverage)
.average()
.getAsDouble();
List<MyObject> filteredElements = list
.stream()
.filter(o-> o.getAverage() > average)
.collect(Collectors.toList());
double average=list
.stream()
.mapToInt(MyObject::getAverage)
.average()
.getAsDouble();
列表过滤器删除=列表
.stream()
.filter(o->o.getAverage()>average)
.collect(Collectors.toList());
据我所知,一个文件不可能在里面读两遍
同样的方法
public static Collection<?> filterByResponseTimeAboveAverage(Reader source) {
BufferedReader br = new BufferedReader(source);
String line = null;
Collection<String> additionalList = new ArrayList<String>();
int iteration = 0;
String[] myArray = null;
long count=0;
long responseTime=0;
long sum=0;
int numOfResponseTime=0;
long average=0;
List<String> myList = new ArrayList<String>();
try
{
while ((line = br.readLine()) != null) {
System.out.println("Inside while");
if (iteration == 0) {
iteration++;
continue;
}
myArray = line.split(",");
for (String eachval:myArray)
{
boolean isNumeric = eachval.chars().allMatch(x -> Character.isDigit(x));
//since input dataline is not guaranted to be in any particular order I am finding RESPONSETIME like this
if (isNumeric)
{
count=eachval.chars().count();
if (count<10)
{
responseTime=Integer.parseInt(eachval);
sum=sum+responseTime;
numOfResponseTime++;
}
}
myList.add(eachval);
}
}
average=sum/numOfResponseTime;
System.out.println("Average -- "+average);
---------------
---------------
}
你可以这样做,但你不应该这样做,因为这是没有效率的
你主要有两种方法
优化方式:
- 读取文件中的所有值并计算
响应时间的平均值
- 过滤高于平均值的值
您可以引入一个由FilterByResponseTimeabovaverage()
调用的私有方法,从源代码中检索所有值并计算它们的平均值。
功能性方式(开销稍贵):
- 从文件中读取所有值
- 使用
IntStream.average()
计算响应时间的平均值
- 过滤高于平均值的值
对于第二个也是最后一个步骤,它可能类似于:
double average = list
.stream()
.mapToInt(MyObject::getAverage)
.average()
.getAsDouble();
List<MyObject> filteredElements = list
.stream()
.filter(o-> o.getAverage() > average)
.collect(Collectors.toList());
double average=list
.stream()
.mapToInt(MyObject::getAverage)
.average()
.getAsDouble();
列表过滤器删除=列表
.stream()
.filter(o->o.getAverage()>average)
.collect(Collectors.toList());