Java 查找文件中最常见的字节
所以基本上我有C:/180301.txt文件,其中我有1 2 3 4 5个数字\字节,我认为输出(49)是正确的。我的问题是如何打印出所有重复出现的元素,现在我只有一个Java 查找文件中最常见的字节,java,arrays,inputstream,fileinputstream,Java,Arrays,Inputstream,Fileinputstream,所以基本上我有C:/180301.txt文件,其中我有1 2 3 4 5个数字\字节,我认为输出(49)是正确的。我的问题是如何打印出所有重复出现的元素,现在我只有一个 private static ArrayList<Integer> list1 = new ArrayList<>(); private static ArrayList<Integer> list2 = new ArrayList<>(); public
private static ArrayList<Integer> list1 = new ArrayList<>();
private static ArrayList<Integer> list2 = new ArrayList<>();
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
FileInputStream fileReader = new FileInputStream(br.readLine());
while (fileReader .available() > 0)
{
list1.add(fileReader .read());
}
int element = 0;
int count = 0;
for (int i = 0; i < list1.size(); i++)
{
if (same_element(list1.get(i)))
{
for (int j = 0; i < list1.size(); i++)
{
if (list1.get(i).equals(list1.get(j)))
{
count++;
element = list1.get(j);
list2.add(list1.get(i));
}
}
}
}
if (count > 1)
System.out.println(element);
fileReader.close();
}
private static boolean same_element(int list_i) {
for (Integer aList2 : list2) if (list_i == aList2) return false;
return true;
}
}
private static ArrayList list1=new ArrayList();
私有静态ArrayList list2=新ArrayList();
公共静态void main(字符串[]args)引发异常{
BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in));
FileInputStream fileReader=新的FileInputStream(br.readLine());
while(fileReader.available()>0)
{
list1.add(fileReader.read());
}
int元素=0;
整数计数=0;
对于(int i=0;i1)
系统输出打印项次(元素);
fileReader.close();
}
私有静态布尔相同元素(int list_i){
for(整数aList2:list2)if(list_i==aList2)返回false;
返回true;
}
}
在同一个元素中,必须切换true和false。49是因为你从不增加j。因此,内部循环也是有缺陷的。但无论如何都应该删除它,因为同一个_元素现在应该完成这项工作。第三个问题是,如果最近的值已经存在,则只将其添加到已经看到的值列表中。那是不可能发生的。因此,稍加修改,您的代码可能如下所示:
List<Integer> fromFile = new ArrayList<>();
InputStream fileReader = new ByteArrayInputStream("71123456".getBytes("utf-8"), 0, 8);
while (fileReader.available() > 0)
{
fromFile.add(fileReader.read());
}
int element = 0;
int count = 0;
List<Integer> seen = new ArrayList<>();
for (int i = 0; i < fromFile.size(); i++)
{
Integer recent = fromFile.get(i);
if (seen.contains(recent))
{
count++;
element = recent;
}
seen.add(recent);
}
if (count > 1) System.out.println(element);
fileReader.close();
Map<Byte, Integer> counters = new HashMap<>();
Path path = FileSystems.getDefault().getPath(args[0]);
// build a map with byte value as a key refering to a counter in the value
for (Byte b: Files.readAllBytes(path)) {
Integer old = counters.get(b);
counters.put(b, (old == null ? 1 : old + 1));
}
// create a comparator that orders Map.Entry objects by their value. I.E. the
// occurences of the respective byte. The order is ascending.
Comparator<Entry<Byte, Integer>> byVal = Comparator.comparingInt(e -> e.getValue());
// create a stream of Map.Entry objects. The stream is a new concept of Java8.
// That's somehow like a collection, but more powerful. While the collection
// stores data he stream has a focus on manipulating
counters.entrySet().stream()
// Use the comaparator in reversed form. That means the number of
// occurences is now descending
.sorted(byVal.reversed())
// only use the first Map.Entry. I.E. the one with most occurences
// a similar functionality is by filter.
// .filter(e -> e.getValue() > 1) would use all duplicates
.limit(1)
// print out the results. Of course the argument for println can be
// concatenated from several parts like:
// e.getKey() + "\tcount: " + e.getValue()
.forEach(e -> System.out.println(e.getKey()));
List fromFile=new ArrayList();
InputStream fileReader=新的ByteArrayInputStream(“71123456”.getBytes(“utf-8”),0,8;
while(fileReader.available()>0)
{
fromFile.add(fileReader.read());
}
int元素=0;
整数计数=0;
所见列表=新的ArrayList();
对于(int i=0;i1)系统输出打印项次(元素);
fileReader.close();
这将打印最后一个副本,但仍然不是出现次数最多的字节。今天我们这样写:
List<Integer> fromFile = new ArrayList<>();
InputStream fileReader = new ByteArrayInputStream("71123456".getBytes("utf-8"), 0, 8);
while (fileReader.available() > 0)
{
fromFile.add(fileReader.read());
}
int element = 0;
int count = 0;
List<Integer> seen = new ArrayList<>();
for (int i = 0; i < fromFile.size(); i++)
{
Integer recent = fromFile.get(i);
if (seen.contains(recent))
{
count++;
element = recent;
}
seen.add(recent);
}
if (count > 1) System.out.println(element);
fileReader.close();
Map<Byte, Integer> counters = new HashMap<>();
Path path = FileSystems.getDefault().getPath(args[0]);
// build a map with byte value as a key refering to a counter in the value
for (Byte b: Files.readAllBytes(path)) {
Integer old = counters.get(b);
counters.put(b, (old == null ? 1 : old + 1));
}
// create a comparator that orders Map.Entry objects by their value. I.E. the
// occurences of the respective byte. The order is ascending.
Comparator<Entry<Byte, Integer>> byVal = Comparator.comparingInt(e -> e.getValue());
// create a stream of Map.Entry objects. The stream is a new concept of Java8.
// That's somehow like a collection, but more powerful. While the collection
// stores data he stream has a focus on manipulating
counters.entrySet().stream()
// Use the comaparator in reversed form. That means the number of
// occurences is now descending
.sorted(byVal.reversed())
// only use the first Map.Entry. I.E. the one with most occurences
// a similar functionality is by filter.
// .filter(e -> e.getValue() > 1) would use all duplicates
.limit(1)
// print out the results. Of course the argument for println can be
// concatenated from several parts like:
// e.getKey() + "\tcount: " + e.getValue()
.forEach(e -> System.out.println(e.getKey()));
Map counters=newhashmap();
Path Path=FileSystems.getDefault().getPath(args[0]);
//构建一个映射,将字节值作为引用值中计数器的键
for(字节b:Files.readAllBytes(路径)){
整数old=counters.get(b);
put(b,(old==null?1:old+1));
}
//创建一个比较器,根据Map.Entry对象的值对其进行排序。即
//相应字节的出现次数。顺序在上升。
Comparator byVal=Comparator.comparingit(e->e.getValue());
//创建Map.Entry对象流。流是Java8的一个新概念。
//这有点像一个集合,但更强大。当收集
//存储数据流的重点在于操作
counters.entrySet().stream()
//以相反的形式使用comaparator。这意味着
//发生率正在下降
.sorted(byVal.reversed())
//仅使用第一个Map.Entry。即发生最多的一个
//过滤器也提供了类似的功能。
//.filter(e->e.getValue()>1)将使用所有重复项
.限额(1)
//把结果打印出来。当然println的参数可以是
//由以下几个部分连接而成:
//e.getKey()+“\t帐户:”+e.getValue()
.forEach(e->System.out.println(e.getKey());
Java8对于解决这样的问题有很大帮助。用以前的版本编写同样的代码需要更多的代码。好的,但它仍然只打印出一个元素,这是最常见的,但我想要的是,如果我有51 51 52 52 53 54 55 56,打印出51和52,我该怎么做?@Predict\u it我留下了更多的评论。只需稍作调整,查找比较器和流之类的概念。这个原则非常灵活,我认为你可以很快地进入其中。