Java 如何检查文本文件中域的实例数

Java 如何检查文本文件中域的实例数,java,Java,我有一个包含如下域的文本文件 ABC.COM ABC.COM DEF.COM DEF.COM XYZ.COM 我想从文本文件中读取域,并检查有多少个域实例。 从文本文件读取很容易,但我对如何检查域实例的数量感到困惑。 请提供帮助。按空格拆分(字符串实例有方法拆分),迭代结果数组并使用Map-当域在Map中时,而不是将Map中的计数增加1,当不在Map中时-将域名放入Map并将1设置为值。更好的解决方案是使用Map以频率映射单词Map Map frequency=newlinkedhashmap

我有一个包含如下域的文本文件

ABC.COM
ABC.COM
DEF.COM
DEF.COM
XYZ.COM
我想从文本文件中读取域,并检查有多少个域实例。 从文本文件读取很容易,但我对如何检查域实例的数量感到困惑。
请提供帮助。

按空格拆分(字符串实例有方法
拆分
),迭代结果数组并使用
Map
-当域在Map中时,而不是将Map中的计数增加1,当不在Map中时-将域名放入Map并将1设置为值。

更好的解决方案是使用Map以频率映射单词Map

Map frequency=newlinkedhashmap()

  • 读取文件

    BufferedReader in = new BufferedReader(new FileReader("infilename"));
    String str;
    while ((str = in.readLine()) != null) {
        buildMap(str);
    }
    in.close();
    
  • BuildMap方法:您可以在您的示例空间中拆分URL,并使用分隔符进行拆分(在您的示例空间中)

  • 使用以下内容查找特定域:

    frequency.get(域名)

Ref:

列出域=新的ArrayList();//文件中的值
域名。添加(“abc.com”);
域名。添加(“abc.com”);
domains.add(“xyz.com”);
//例如添加
Map domainCount=新HashMap();
用于(字符串域:域){
if(domainCount.containsKey(域)){
domainCount.put(domain,domainCount.get(domain)+1);
}否则
put(域,新整数(1));
}
Set entrySet=domainCount.entrySet();
for(条目:entrySet){
System.out.println(entry.getKey()+“:”+entry.getValue());
}

如果域未知,您可以执行以下操作:

// Field Declaration
private Map<String, Integer> mappedDomain = new LinkedHashMap<String, Integer>();
private static final List<String> domainList = new ArrayList<String>();
// Add all that you want to track
domainList.add("com");
domainList.add("net");
domainList.add("org");
...

// Inside the loop where you do a readLine
String[] words = line.split(" ");
for (String word : words) {

  String[] wordSplit = word.split(".");

  if (wordSplit.length == 2) {

    for (String domainCheck : domainList) {

      if (domainCheck.equals(wordSplit[1])) {

        if (mappedDomain.containsKey(word)) {
          mappedDomain.put(word, mappedDomain.get(word)+1);
        } else {
          mappedDomain.put(word, 1);
        }
      }
    }
  }
}
//字段声明
private Map mappedDomain=新建LinkedHashMap();
private static final List domainList=new ArrayList();
//添加所有要跟踪的内容
domainList.add(“com”);
domainList.add(“net”);
域名列表添加(“org”);
...
//在循环中,您可以在其中执行readLine
String[]words=line.split(“”);
for(字符串字:字){
String[]wordSplit=word.split(“.”);
if(wordSplit.length==2){
for(字符串域检查:域列表){
if(domainCheck.equals(wordSplit[1])){
if(mappedDomain.containsKey(word)){
mappedDomain.put(word,mappedDomain.get(word)+1);
}否则{
mappedDomain.put(单词,1);
}
}
}
}
}
注意:这将适用于类似xxx.xxx的内容;如果你需要处理复杂的格式,你需要修改wordSplit的逻辑


您应该测试
f
是否不为空,否则您将在第一次遇到单词时获得NPE。
    List<String> domains=new ArrayList<String>(); // values from your file
    domains.add("abc.com");
    domains.add("abc.com");
    domains.add("xyz.com");
   //added for example
    Map<String,Integer> domainCount=new HashMap<String, Integer>();
    for(String domain:domains){
        if(domainCount.containsKey(domain)){
            domainCount.put(domain, domainCount.get(domain)+1);
        }else
            domainCount.put(domain, new Integer(1));

    }
    Set<Entry<String, Integer>> entrySet = domainCount.entrySet();
    for (Entry<String, Integer> entry : entrySet) {
        System.out.println(entry.getKey()+" : "+entry.getValue());
    }
// Field Declaration
private Map<String, Integer> mappedDomain = new LinkedHashMap<String, Integer>();
private static final List<String> domainList = new ArrayList<String>();
// Add all that you want to track
domainList.add("com");
domainList.add("net");
domainList.add("org");
...

// Inside the loop where you do a readLine
String[] words = line.split(" ");
for (String word : words) {

  String[] wordSplit = word.split(".");

  if (wordSplit.length == 2) {

    for (String domainCheck : domainList) {

      if (domainCheck.equals(wordSplit[1])) {

        if (mappedDomain.containsKey(word)) {
          mappedDomain.put(word, mappedDomain.get(word)+1);
        } else {
          mappedDomain.put(word, 1);
        }
      }
    }
  }
}