如何返回数组列表中的重复字符串数与另一个数组列表JAVA相比较

时间:2015-09-25 17:48:41

标签: java arraylist

如果另一个ArrayList中存在单词重复,我需要返回多少时间。

更多说明:如果另一个arrayList中存在第一个ArrayList中的单词,我想检查它在第二个数组列表中复制了多少次但它不起作用?

public int Duplication(ArrayList<String> studentList, ArrayList<String> teacherList){
    Set<String> uniqueStudent = new HashSet<String>(studentList);
    Set<String> uniqueTeacher = new HashSet<String>(teacherList);

    for (String studentKey : uniqueStudent) {
        for (String teacherKey : uniqueTeacher) {
            if(teacherKey.equals(studentKey)){
                return Collections.frequency(studentList, studentKey);
            }
        }
    }
 }    

4 个答案:

答案 0 :(得分:0)

尝试:

public Map<String, Integer> Duplication(ArrayList<String> studentList, 
                                        ArrayList<String> teacherList) {
    Map<String, Integer> nameCount = new HashMap<>();
    for (String studentKey : new HashSet<>(studentList)) {
        nameCount.put(studentKey, Collections.frequency(teacherList, studentKey));
    }
    return nameCount;
}

答案 1 :(得分:0)

这应该做:

  

如果您只想要一个返回重复值的方法,请参阅下面的代码

public Map<String,Integer> duplicateCount(ArrayList<String> studentList, 
                       ArrayList<String> teachersList) {
    Map<String, Integer> duplicateCount = new HashMap<>();
    for (String studentKey : new HashSet<>(studentList)) {
        duplicateCount.put(studentKey, Collections.frequency(teacherList, studentKey));
    }
    return duplicateCount;
}
  

如果要在两个列表中查找重复值和唯一值,则返回给出重复值的计数。

   public static void main(String[] args) throws IOException,
        InterruptedException {
    List<String> studentList=new ArrayList<String>();
    //Put Dummy  values in Student List
    studentList.add("a");
    studentList.add("b");
    studentList.add("c");
    studentList.add("d");
    List<String> teacherList=new ArrayList<String>();
    //Put Dummy  values in Teacher List
    teacherList.add("a");
    teacherList.add("1");
    teacherList.add("c");
    teacherList.add("c");
    teacherList.add("c");
    teacherList.add("c");
    teacherList.add("c");
    teacherList.add("2");
    Set<String> uniqueStudent = new HashSet<String>(studentList);

    Set<String> uniqueTeacher = new HashSet<String>(teacherList);
    Set<String> duplicateValue= new HashSet<String>();
    Set<String> uniqueValue= new HashSet<String>();
    uniqueStudent.retainAll(uniqueTeacher);
    duplicateValue=uniqueStudent; //As uniqueStudent Now has only the duplicate values due to above step assing this set to duplicateValue set
    uniqueStudent=new HashSet<String>(studentList);//reassign UniqueStudent Set
    uniqueStudent.removeAll(uniqueTeacher);
    uniqueValue=uniqueStudent;
    System.out.println("duplicateValue:"+duplicateValue);
    System.out.println("uniqueValue:"+uniqueValue);
    //Now Counting Occurance of Duplicate Values in Other List
    for(String duplicate:duplicateValue){
        int occurrences = Collections.frequency(teacherList, duplicate);
        System.out.println("Number of Occurance For "+duplicate+" is "+occurrences);
    }
} 

输出:

> duplicateValue:[c, a]
> uniqueValue:[d, b]
> Number of Occurrence For c is 5
> Number of Occurrence For a is 1
  

说明使用的方法:

  • 每个List都可以将另一个列表作为构造函数参数,并复制它的值。

  • retainAll(...)将删除...中不存在的所有条目。

  • removeAll(...)将删除...中确实存在的所有条目。

  • 我们不想删除/保留原始列表,因为这会修改它,所以我们在构造函数中复制它们。

  • Collections.frequency(--)会给出列表中单词出现的频率。

答案 2 :(得分:0)

用于存储重复项的正确数据结构被视为Map,以下是代码:

public static void main(String[] args) {
    Map<String, Integer> duplicatesMap = duplicate(
            new ArrayList<String>(Arrays.asList(new String[] { "A", "H",
                    "T", "P", "S", "O", "F", "X", "A" })),
            new ArrayList<String>(Arrays.asList(new String[] { "A", "H",
                    "T", "P", "S", "O", "F", "A", "O", "B", "X", "R" })));
    for (Map.Entry<String, Integer> entry : duplicatesMap.entrySet()) {
        System.out.println(entry.getKey() + " found " + entry.getValue()
                + " times");
    }
}

public static Map<String, Integer> duplicate(ArrayList<String> fromList,
        ArrayList<String> toList) {
    Map<String, Integer> returnMap = new HashMap<String, Integer>();
    Set<String> uniqueFromList = new HashSet<String>(fromList);
    for (String key : uniqueFromList) {
        if (toList.contains(key)) {
            returnMap.put(key, Collections.frequency(toList, key));
        }
    }
    return returnMap;
}

输出

T found 1 times F found 1 times A found 2 times P found 1 times S found 1 times O found 2 times H found 1 times X found 1 times

答案 3 :(得分:0)

怎么样:

public int Duplication(ArrayList<String> studentList, ArrayList<String> teacherList){
  Set<String> uniqueStudent = new HashSet<String>(studentList);
  Set<String> uniqueTeacher = new HashSet<String>(teacherList);

  int counter = 0;
  for (String studentKey : uniqueStudent) {
    if(uniqueTeacher.contains(studentKey)){
      for (String teacherKey : uniqueTeacher) {
         if(teacherKey.equals(studentKey)){
           counter ++;
         }
      }
    }
  }

  return counter;
}