Java 以后如何在PriorityQueue中隐式使用集合?

Java 以后如何在PriorityQueue中隐式使用集合?,java,set,priority-queue,hashset,Java,Set,Priority Queue,Hashset,我从LeetCode中的注释中得到了这段旧代码。我想了解如果后面没有明确使用变量Set,2 for循环在这里是如何工作的。我能猜到的最好情况是,在以下过程中,记录中的值以某种方式向后引用(从左到右)到Set: Set=((HashSet)值[0]) 我以前从未见过这样的事情。所以我想知道这是否与PriorityQueue有关。为了更好地理解它,我已经通过一个调试器运行了它,但是我能看到的最接近它的引用可能是稍后将值添加到队列中并出现在PriorityQueuelambda中的时候 我已经删除了s

我从LeetCode中的注释中得到了这段旧代码。我想了解如果后面没有明确使用变量
Set
,2 for循环在这里是如何工作的。我能猜到的最好情况是,在以下过程中,记录中的值以某种方式向后引用(从左到右)到
Set

Set=((HashSet)值[0])

我以前从未见过这样的事情。所以我想知道这是否与
PriorityQueue
有关。为了更好地理解它,我已经通过一个调试器运行了它,但是我能看到的最接近它的引用可能是稍后将值添加到队列中并出现在
PriorityQueue
lambda中的时候

我已经删除了
set.add(I)
,认为如果没有显式引用,它什么也做不了。我发现它确实会影响最终的计数,而预期的结果并不是按这个顺序排列的[新闻商店,时尚节拍]

class TopNCompetitors {
    public static void main(String[] args) {
        System.out.println(topNCompetitors(6, 2, Arrays.asList("newshop", "shopnow", "afshion", "fashionbeats", "mymarket", "tcellular"), 6, Arrays.asList("newshop is providing good services in the city; everyone should use newshop", "best services by newshop", "fashionbeats has great services in the city", "I am proud to have fashionbeats", "mymarket has awesome services", "Thanks Newshop for the quick delivery.")));
    }

    public static ArrayList<String> topNCompetitors(int numCompetitors,
                                                    int topNCompetitors,
                                                    List<String> competitors,
                                                    int numReviews,
                                                    List<String> reviews) {

        ArrayList<String> ans = new ArrayList<>();

        List<String[]> str = new ArrayList<>();

        for (String review : reviews) {
            str.add(review.trim().toLowerCase().replaceAll("[\\!?,;.]{1,}", "").split("[ ]{1,}"));
        }

        Map<String, Object[]> records = new HashMap<>();


        for (int i = 0; i < numCompetitors; i++) {
            records.put(competitors.get(i).trim().toLowerCase(), new Object[]{new HashSet<Integer>(), competitors.get(i)});
        }


        for (int i = 0; i < numReviews; i++) {
            String[] review = str.get(i);

            for (int j = 0; j < review.length; j++) {

                String in = review[j];

                if (records.containsKey(in)) {
                    Object[] values = records.get(in);
                    Set<Integer> set = ((HashSet<Integer>) values[0]);
                    set.add(i);
                }
            }
        }

        Collection<Object[]> values = records.values();

        PriorityQueue<Object[]> q = new PriorityQueue<>((t1, t2) -> {
            int diff = ((Set<Integer>) t2[0]).size() - ((Set<Integer>) t1[0]).size();
            if (diff == 0) {
                return ((String) t1[1]).compareTo((String) t2[1]);
            }
            return diff;

        });

        for (Object[] val : values) {
            q.add(val);
        }


        for (int i = 0; i < topNCompetitors; i++) {
            ans.add((String) q.poll()[1]);
        }

        return ans;

    }
}
class-topncompetitor{
公共静态void main(字符串[]args){
System.out.println(topNCompetitors(6,2,Arrays.asList)(“newshop”、“shopnow”、“afshion”、“fashionbeats”、“mymarket”、“Tcell”))6,Arrays.asList(“新闻商店在城市中提供良好的服务;每个人都应该使用新闻商店”,“新闻商店提供的最佳服务”,“fashionbeats在城市中有很好的服务”,“我为拥有fashionbeats感到自豪”,“mymarket有很棒的服务”,“感谢Newshop的快速交付。”);
}
公共静态阵列列表顶级竞争对手(国际数字竞争对手,
国际顶级竞争对手,
列出竞争对手,
国际新闻网,
清单审查){
ArrayList ans=新的ArrayList();
List str=new ArrayList();
for(字符串审阅:审阅){
str.add(review.trim();
}
映射记录=新的HashMap();
对于(int i=0;i{
int diff=((集)t2[0]).size()-((集)t1[0]).size();
如果(差异==0){
返回((字符串)t1[1])。比较((字符串)t2[1]);
}
返回差;
});
对于(对象[]值:值){
q、 添加(val);
}
for(int i=0;i
这与
PriorityQueue
无关。只是
Set-Set
不是一个对象,它只是一个对象的引用。该对象仍然在其他地方被引用(在
记录所引用的
HashMap
的值中)太长了,读不下去了,因为它们是对象的引用。