如何在Java中比较列表元素和映射元素?

如何在Java中比较列表元素和映射元素?,java,list,collections,Java,List,Collections,我有以下代码(带有一些示例数据),并希望检查是否有更好或性能更好的方法将映射列表中的每个元素与后续元素进行比较: import java.util.*; public class CompareElements { private static List<Map<String, String>> sample = new ArrayList<>(0); private static int MIN = 0; private static int MAX =

我有以下代码(带有一些示例数据),并希望检查是否有更好或性能更好的方法将映射列表中的每个元素与后续元素进行比较:

import java.util.*;

public class CompareElements {

private static List<Map<String, String>> sample = new ArrayList<>(0);
private static int MIN = 0;
private static int MAX = 10;

static {
    populateListOfMaps();
}

/*
* This is the main part of the question, rest is just to generate test data..
*/
public static void main(String[] args){
    // Can we simplify this part using lambda's or any library?
    for (int i = 0; i < sample.size() -1; i++) {
        for (int j = i+1; j < sample.size(); j++) {
            Map<String, String> referenceMap = sample.get(i);
            Map<String, String> candideMap = sample.get(j);
            if(referenceMap.get("key").equalsIgnoreCase(candideMap.get("key"))){
                System.out.println("Equal : " + i + " || "  + referenceMap.get("key") + " and "+ j + " || " + candideMap.get("key") + " are pairs");
            } else {
                System.out.println("Not equal : " + i + " || "  + referenceMap.get("key") + " and "+ j + " || " + candideMap.get("key") + " are pairs");
            }
        }
    }   
}

private static void populateListOfMaps(){
    if(sample.size() <= 10){
        Map<String, String> someMap = new HashMap<>(0);
        someMap.put("key", "value" + randInt(MIN, MAX));
        sample.add(someMap);
        populateListOfMaps();
    } 
}

public static int randInt(int min, int max) {
    Random rand = new Random();
    int randomNum = rand.nextInt((max - min) + 1) + min;
    return randomNum;
}
import java.util.*;
公共类比较元素{
私有静态列表样本=新的ArrayList(0);
私有静态int MIN=0;
私有静态int MAX=10;
静止的{
populateListOfMaps();
}
/*
*这是问题的主要部分,其余的只是生成测试数据。。
*/
公共静态void main(字符串[]args){
//我们可以使用lambda或任何库来简化这个部分吗?
对于(int i=0;i如果(sample.size()当您从MongoDB获取数据时,我假设您无法控制模式,因此使用POJO不是一个简单的选项。(可以通过生成的代码来完成,但您可能不想去那里)

您可以使用
groupingBy
将此
O(n^2)
循环更改为
O(n)


我不确定您的具体要求是什么,所以每次只解决一部分问题:

检查是否有更好或性能更好的方法将映射列表中的每个元素与后续元素进行比较:

import java.util.*;

public class CompareElements {

private static List<Map<String, String>> sample = new ArrayList<>(0);
private static int MIN = 0;
private static int MAX = 10;

static {
    populateListOfMaps();
}

/*
* This is the main part of the question, rest is just to generate test data..
*/
public static void main(String[] args){
    // Can we simplify this part using lambda's or any library?
    for (int i = 0; i < sample.size() -1; i++) {
        for (int j = i+1; j < sample.size(); j++) {
            Map<String, String> referenceMap = sample.get(i);
            Map<String, String> candideMap = sample.get(j);
            if(referenceMap.get("key").equalsIgnoreCase(candideMap.get("key"))){
                System.out.println("Equal : " + i + " || "  + referenceMap.get("key") + " and "+ j + " || " + candideMap.get("key") + " are pairs");
            } else {
                System.out.println("Not equal : " + i + " || "  + referenceMap.get("key") + " and "+ j + " || " + candideMap.get("key") + " are pairs");
            }
        }
    }   
}

private static void populateListOfMaps(){
    if(sample.size() <= 10){
        Map<String, String> someMap = new HashMap<>(0);
        someMap.put("key", "value" + randInt(MIN, MAX));
        sample.add(someMap);
        populateListOfMaps();
    } 
}

public static int randInt(int min, int max) {
    Random rand = new Random();
    int randomNum = rand.nextInt((max - min) + 1) + min;
    return randomNum;
}
使用密钥集怎么样

Set<String> s1 = new HashSet< String >(referenceMap.values());
Set<String> s2 = new HashSet< String >(candideMap.values());

// Get intersection of values
s1.retainAll(s2);

// You can also get corresponding keys for each value later
Set s1=newhashset(referenceMap.values());
Set s2=newhashset(candideMap.values());
//获取值的交集
s1.保留(s2);
//以后还可以为每个值获取相应的键
这将使您的复杂性从
O(n^2)
降低到
O(n)

实时应用程序中的每个映射都有2个键值(但都是字符串..没有自定义POJO对象)

不知道你所说的实时是什么意思。地图是实时变化的吗?你的解决方案和我的解决方案都不是线程安全的

您的意思是每个条目有两个键值吗?如果您的意思是每个键有两个值,您可能会覆盖
hashcode()
equals()
,您的代码应该可以工作


如果我误解了您的问题,请告诉我。

任何指针..意思是?“实时”意思是;具有一致的延迟。“真实世界”意思是;在真实世界中,即不是理论上的。“上面的代码可以工作,但我希望使这段代码更简洁、性能更好。”-我认为您的问题应该发布在中,因为它与StackOverflow无关。您似乎正在竭尽全力避免POJO,这是最简单的解决方案。使用映射既不简洁,也不高效。当您知道所需的容量(即
2
2)时,为什么要创建容量为
0
的哈希映射
Set<String> s1 = new HashSet< String >(referenceMap.values());
Set<String> s2 = new HashSet< String >(candideMap.values());

// Get intersection of values
s1.retainAll(s2);

// You can also get corresponding keys for each value later