Java: Best way to remove duplicated list in a list [duplicate]

佐手、 提交于 2020-01-06 04:50:27

问题


I have a list of list:

List<List<Integer>> myList = new ArrayList<>();

What would be the best way to remove the duplicated list in myList?

For example, in the following list of list:

[[-1,0,1],[-1,-1,2],[-1,0,1]]

I would like to reduce it to:

[[-1,0,1],[-1,-1,2]]

Thanks!


回答1:


The easiest way is to copy it into an order-preserving set (or, more generally, any kind of set, if you don't care about the ordering), and then back into the list:

myList = new ArrayList<>(new LinkedHashSet<>(myList));



回答2:


Use a Set instead of a list. Sets do not allow duplicates. What is the difference between Set and List?



来源:https://stackoverflow.com/questions/46531811/java-best-way-to-remove-duplicated-list-in-a-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!