gpt4 book ai didi

java - 无需为每个元素迭代列表

转载 作者:行者123 更新时间:2023-12-02 03:29:08 25 4
gpt4 key购买 nike

我构建了一个相当简单的程序,它从 .txt 获取信息。文件并将其放入列表中,对其进行排序,然后通过将其放入 TreeSet 中来删除重复项。 .

如果你看countInstance()以及它的调用方式,您将看到对于 TreeSet 的每次迭代运行该方法,然后迭代列表 dataToSplit多次。我相信在这种特定情况下它会迭代列表 30 次。

问题
有没有一种方法可以消除多次迭代列表的需要并获得相同的结果?

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public class TallyCounter {
private void tallyCount(File commaSeperated) {
List<String> dataToSplit = new ArrayList<String>();
Set<String> set;

try {
BufferedReader br = new BufferedReader(new FileReader(commaSeperated));
String currentLine;
while ((currentLine = br.readLine()) != null) {
String[] tempArray = currentLine.split(",");
for(String s : tempArray) {
dataToSplit.add(s.replaceAll("\t", "").replaceAll("\n", "").trim());
}
}

br.close();
} catch(Exception e) {
e.printStackTrace();
}

dataToSplit.sort(new AlphanumComparator());
set = new TreeSet<String>(dataToSplit);


System.out.println("String Tally Count");
for(String s : set) {
System.out.println(countInstance(s, dataToSplit));
}
}

private String countInstance(String s, List<String> l) {
int count = 0;

for(String temp : l) {
if(s.equals(temp)) {
count++;
}
}

int rSpace = (10 - count) / 2;

String repeated = new String(new char[count]).replace("\0", "|");
String space = new String(new char[rSpace]).replace("\0", " ");

return " " + s + " " + space + repeated + " " + space + Integer.toString(count);
}


public static void main(String[] args) {
TallyCounter tC = new TallyCounter();
tC.tallyCount(new File("src/txt.txt"));
}
}
<小时/>

额外详细信息

AlphanumComparator.java

import java.util.Comparator;

public class AlphanumComparator implements Comparator<Object>
{
private final boolean isDigit(char ch)
{
return ch >= 48 && ch <= 57;
}

/** Length of string is passed in for improved efficiency (only need to calculate it once) **/
private final String getChunk(String s, int slength, int marker)
{
StringBuilder chunk = new StringBuilder();
char c = s.charAt(marker);
chunk.append(c);
marker++;
if (isDigit(c))
{
while (marker < slength)
{
c = s.charAt(marker);
if (!isDigit(c))
break;
chunk.append(c);
marker++;
}
} else
{
while (marker < slength)
{
c = s.charAt(marker);
if (isDigit(c))
break;
chunk.append(c);
marker++;
}
}
return chunk.toString();
}

public int compare(Object o1, Object o2)
{
if (!(o1 instanceof String) || !(o2 instanceof String))
{
return 0;
}
String s1 = (String)o1;
String s2 = (String)o2;

int thisMarker = 0;
int thatMarker = 0;
int s1Length = s1.length();
int s2Length = s2.length();

while (thisMarker < s1Length && thatMarker < s2Length)
{
String thisChunk = getChunk(s1, s1Length, thisMarker);
thisMarker += thisChunk.length();

String thatChunk = getChunk(s2, s2Length, thatMarker);
thatMarker += thatChunk.length();

// If both chunks contain numeric characters, sort them numerically
int result = 0;
if (isDigit(thisChunk.charAt(0)) && isDigit(thatChunk.charAt(0)))
{
// Simple chunk comparison by length.
int thisChunkLength = thisChunk.length();
result = thisChunkLength - thatChunk.length();
// If equal, the first different number counts
if (result == 0)
{
for (int i = 0; i < thisChunkLength; i++)
{
result = thisChunk.charAt(i) - thatChunk.charAt(i);
if (result != 0)
{
return result;
}
}
}
} else
{
result = thisChunk.compareTo(thatChunk);
}

if (result != 0)
return result;
}

return s1Length - s2Length;
}
}

txt.txt

5.00,   5.14,   5.01,   4.90,   5.02,   5.18,   5.04,   5.07,   4.95,   5.05
5.05, 4.82, 4.97, 5.04, 4.98, 5.12, 5.08, 4.96, 5.02, 4.93
5.12, 5.04, 5.13, 4.94, 5.06, 5.00, 4.92, 5.17, 5.08, 4.99
5.07, 5.15, 5.01, 4.95, 5.11, 5.22, 5.08, 4.86, 4.97, 5.14
5.03, 5.14, 5.06, 4.88, 4.96, 5.04, 4.96, 5.09, 4.93, 5.03
<小时/>

脚注

对于运行时输出看起来多么奇怪,我深表歉意。那只是因为我还没有实现 JTable存储信息

最佳答案

使用 HashMap<String,Integer> countMap并遍历字符串一次。每个temp你会发现,将 1 添加到相应的值 countMap.get(temp)在 HashMap 中。

关于java - 无需为每个元素迭代列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38336886/

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