Java 组合框中可以出现一个相同的名称

Java 组合框中可以出现一个相同的名称,java,file,combobox,Java,File,Combobox,我的组合框中怎么可能只有一个相同的名称?在我的费用文本文件中有两个相同的名称,我想将费用文本文件中的名称输入组合框。但它显示两个相同的名称 我的代码中没有错误,我无法找到问题。我想我的组合框函数出了问题。下面是我的预期结果 //fee.txt john|123|0.00 john|456|0.00 //my expected result in combobox john //my result john john //filefuncion.java public class FileF

我的组合框中怎么可能只有一个相同的名称?在我的费用文本文件中有两个相同的名称,我想将费用文本文件中的名称输入组合框。但它显示两个相同的名称

我的代码中没有错误,我无法找到问题。我想我的组合框函数出了问题。下面是我的预期结果

//fee.txt
john|123|0.00
john|456|0.00

//my expected result in combobox
john

//my result
john
john

//filefuncion.java
public class FileFunction {

public static ArrayList getContent(File f) {
    ArrayList ls = null;
    try (BufferedReader in = new BufferedReader(new FileReader(f));) {
        String line;
        ls = new ArrayList();
        /*while ((line = in.readLine()) != null) {
            ls.add(line);
        }*/
        while ((line = in.readLine()) != null) {
        if (line.trim().length() > 0) {
            ls.add(line);
        }
  }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return ls;
 }

//my code   
private void combobox(){
    File file = new File("fee.txt");
    ArrayList al = FileFunction.getContent(file);
    for (Object obj : al) {
        String newobj = obj.toString();
        String text[] = newobj.split("\\|");
        String name = text[0];
        String status = text[2];
        if(status.equals("0.00")){
            comboboxResident.addItem(name);   
        }
    }
}

首先,使用泛型来确保更强的类型检查,并减少由于错误类型导致的错误。编写类似于
ArrayList
的内容,而不是简单的
ArrayList

如果要删除
ArrayList
实例中的重复元素,最方便的方法是构建
Set
(它是集合框架下的一个类)并将其转换回
ArrayList
(如果需要)

例如,假设您有一个ArrayList实例,那么您可以编写

ArrayList<String> list = ...
LinkedHashSet<String> set = new LinkedHashSet<>(list);
ArrayList=。。。
LinkedHashSet=新LinkedHashSet(列表);
只需在
集合中迭代即可。或者您可以通过
ArrayList newList=new ArrayList(set)将其转换回列表


LinkedHashSet
实现
Set
接口,不包含重复的元素。它还具有可预测的迭代顺序。如果要对元素进行进一步排序,请尝试使用
TreeSet

将文本文件中的值加载到SetIs this Swing、AWT、Java FX。。?为了更快地获得更好的帮助,可以发布一个(最小完整的可验证示例)或(简短、自包含、正确的示例)。