用java实现,一个文件中有100万条商品id,求出商品最多的前100个...
发布网友
发布时间:2024-10-24 01:00
我来回答
共1个回答
热心网友
时间:2024-11-07 08:53
package com.wang;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Zuoye{
public static void main(String[] args) throws IOException {
File file = new File("C:\\Users\\xy\\Desktop\\text.txt");
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String str;
Map<String,Shanp> map =new HashMap<>();
List<Shanp> list = new ArrayList<>();
while((str=br.readLine())!=null){
if(map.get(str)==null){
map.put(str, new Shanp(str,0));
list.add(map.get(str));
}else{
map.get(str).sum++;
}
}
list.sort(new Comparator<Shanp>() {
@Override
public int compare(Shanp o1, Shanp o2) {
if(o1.sum<o2.sum){
return 0;
}return -1;
}
});
System.out.println(list);
}
}package com.wang;
class Shanp{
String id;
Integer sum;
public Shanp(String id, Integer sum) {
super();
this.id = id;
this.sum = sum;
}
@Override
public String toString() {
return "Shanp [id=" + id + ", sum=" + sum + "]";
}
}