简体   繁体   English

如何计算地图中复杂对象的唯一值

[英]How to count unique values ​in a map that are complex objects

I have the following sample structure:我有以下示例结构:

class MyObject {
    private String type;
    private String level;
    
}

Map<String,List<MyObject>> map = new HashMap<>();

MyObject myObject1 = new MyObject();
myObject1.setType("x");
myObject1.setLevel("5");

MyObject myObject2 = new MyObject();
myObject2.setType("y");
myObject2.setLevel("5");

List<MyObject> list1 = new ArrayList<>();
list1.add(myObject1);
list1.add(myObject2);

map.put("1",list1);

MyObject myObject3 = new MyObject();
myObject3.setType("x");
myObject3.setLevel("4");

List<MyObject> list2 = new ArrayList<>();
list2.add(myObject3);

map.put("2",list2);

MyObject myObject4 = new MyObject();
myObject4.setType("x");
myObject4.setLevel("5");

MyObject myObject5 = new MyObject();
myObject5.setType("y");
myObject5.setLevel("5");

List<MyObject> list3 = new ArrayList<>();
list3.add(myObject4);
list3.add(myObject5);

map.put("3",list3);

...

Based on this map, I need to create an intermediate object or some structure where I will store information about the unique values ​​of the map.基于这张地图,我需要创建一个中间对象或一些结构,我将在其中存储有关地图唯一值的信息。 In the example above, key 1 and key 3 are the same value so I need to store the information that the combination x = 5, y = 5 occurred twice in the map.在上面的示例中, key 1key 3是相同的值,因此我需要存储组合x = 5, y = 5在地图中出现两次的信息。 The combination x = 4 appeared once in the map.组合x = 4在地图中出现过一次。 There can be many combinations.可以有很多组合。

Any suggestions on how to do it the easiest way?关于如何以最简单的方式做到这一点的任何建议?

Since this looks like a homework question asking how to generally do the task I will not include code.由于这看起来像一个家庭作业问题,询问一般如何完成任务,我不会包含代码。

Think through what you have to do, write methods you'll need, implement them when you think you have all the pieces you need.仔细考虑你必须做什么,编写你需要的方法,当你认为你拥有所有你需要的部分时实施它们。 Start with a stub for the method that does what you want.从执行您想要的方法的存根开始。

The thing you can have duplicates of in the map (why are they in the map, no idea) are lists.您可以在地图中复制的东西(为什么它们在地图中,不知道)是列表。 Write a method that compares lists and returns whether they are the same.编写一个比较列表并返回它们是否相同的方法。

To write that method you need a method that can compare MyObject.要编写该方法,您需要一个可以比较 MyObject 的方法。 Best way would be to override equals() method.最好的方法是覆盖equals()方法。

Next, it'll be a question if order in the lists matters.接下来,列表中的顺序是否重要将是一个问题。 If yes, than List equals method will work for you (read the javadoc to see exactly what it does).如果是,那么 List equals 方法将适用于您(阅读javadoc以了解它的确切作用)。 If not you'll need to write custom code to handle that, or sort the lists before comparison (which would involve writing a comparator for MyObject), or use a library that has that functionality (there should be something in Apache Commons).如果不是,您将需要编写自定义代码来处理它,或者在比较之前对列表进行排序(这将涉及为 MyObject 编写比较器),或者使用具有该功能的库(Apache Commons 中应该有一些东西)。

Now that we have all that all we come back to the main method, use the ones we wrote appropriately, and all we need is do something with the results.现在我们已经有了所有我们回到 main 方法,使用我们编写的适当的方法,我们所需要的就是对结果做一些事情。 Generally anything will do, a map with the list as key and amount of occurences as value will be simplest unless you have some more constraints or operations to do on the results.通常任何事情都可以,除非您对结果有更多约束或操作,否则将列表作为键并将出现次数作为值的映射将是最简单的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM