检查列表中的整数是否包含在整数列表的另一个列表中的Java方法 [英] Java method to check if integers in list are contained in another list of a list of integers

查看:62
本文介绍了检查列表中的整数是否包含在整数列表的另一个列表中的Java方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

*作为序言,我发布了一个类似的问题,但是这次,我想要的输出将有所不同. 假设我们有一个叫Bob的人,他有一个他写下的整数列表:

*To preface, I posted a similar question but this time, the output I want is going to be different. Suppose we have a person named Bob, and he has a list of integers he wrote down:

bobList = [10, 25, 30, 50]

假设系统还生成了3个其他随机整数列表,并将它们放入1个主列表中:

Say there are 3 other lists of random integers that was generated by a system and put into 1 master list:

list1 = [1, 10, 20, 25, 33, 55]
list2 = [2, 3, 5, 6, 9, 30]
list3 = [25, 30, 50, 100]
List<List<Integer>> masterList = new ArrayList<>()
masterList.add(list1)
masterList.add(list2)
masterList.add(list3)

可以假定系统生成的3个数字列表是从最小到最大的顺序,而鲍勃的列表也是从最小到最大的顺序.目的是浏览Bob的列表,并查看Bob写下的每个数字是否包含在主列表的每个列表中.我用Java 11的流进行了尝试,尝试通过像这样输出来显示Bob列表中的每个整数是否属于3个列表中的每个列表

It can be assumed that the 3 list of numbers generated by a system are in order from smallest to greatest and Bob's list is also from smallest to greatest. The goal is to go through Bob's list and see if each number written down by Bob is contained in each list within the master list. I tried this with Java 11 with streams to try and show whether or not each integer in Bob's list belongs in each of the 3 lists by trying to output it like

{10=[true, false, false], 25=[true, false, true], 30=[false, true, true] , 50=[false, false, true]}

我认为这类似于Bob的列表,但他写的每个整数都是键,而值是Boolean的列表,其中每个索引都是对应的列表,如果为true,则Bob编写的数字位于列出,反之亦然,否则为false.但是问题是,我对编程还是有点陌生​​,在我看来,所有这些数据结构和算法都让我很头疼,而在过去的一天中,我一直坚持这一点.有人可以发布解决方案,使输出看起来像所描述的那样吗?或者,如果您对具有更简单视图的输出提出了更好的建议,那么我绝对是在寻求改进!谢谢!

I think this will be something like Bob's list but with each integer he wrote being the key, and the value being a list of Boolean where each index is the corresponding list, and if its true then that number Bob wrote is in the list and vice versa for false. But the problem is, I'm sort of new to programming and in my mind, all this data structure and algorithms is really messing with me and I've been stuck on this for the past day now. Could someone post a solution to this where the output will look like the one described? Or if you have a better recommendations for a output that has a easier view then by all means, I'm definitely looking to improve it! Thank you!

更新:我完全忘记添加自己的尝试,但是我玩了这本书:

Update: I totally forgot to add my attempt but I played around with this piece:

List<Map<Boolean, List<Integer>>> test = bobList.stream()
       .map(list -> list.stream()
                        .collect(Collectors.partitioningBy(masterList::contains)))
       .collect(Collectors.toList());

test.forEach(System.out::println);     

输出类似于:

{false=[1,20,33,55], true=[10,25]}
{false=[2,3,5,6,9], true=[30]}
{false=[100], true=[25,30,50]}

如您所见,这种方式将masterList中的3个列表与bobList进行比较,并像false和true一样输出它,并拆分bobList中的数字和不拆分的数字.但是,我尝试将其翻转,查看bobList中的哪些数字在masterList的列表中,并按照我想要的方式输出它,但是我只是被卡住并尝试不同的方法

As you can see, this way compares the 3 lists in masterList to bobList and outputs it like false and true and splits whichever numbers are in bobList and whichever are not. However, I tried flipping it and seeing which numbers in bobList are in the masterList's lists and outputting it like the way I wanted, but I just get stuck and circle around with different attempts

我使用的部分解决方案是使用以下方法从boblist转到仅1个列表:

A partial solution I had was going from boblist to just 1 list using :

masterList.stream().map(l -> l.contains(i)).collect(Collectors.toList()))));

但是我不确定从何处获取列表的相应密钥.

But i'm unsure where to get that respective key for the list.

更新:谢谢Holger,我忘记了您可以两次使用.collect()

Update: Thank you to Holger, I forgot you could use .collect() twice

推荐答案

如果要对流执行此操作,则解决方案是对bobList进行流处理并将流作为地图收集.映射中的值本身可以通过流式传输masterList并映射到表示键是否在关联列表中的布尔值来生成.

If you would like to do this with streams then the solution is to stream bobList and collect the stream as a map. The values in the map can themselves be generated by streaming masterList and mapping to boolean representing whether the key is in the associated list.

将所有内容放在一起:

Map<Integer,List<Boolean>> result = bobList.stream()
    .collect(Collectors.toMap(
        i -> i,
        i -> masterList.stream().map(l -> l.contains(i)).collect(Collectors.toList()))));

如Holger所建议:l.contains(i)可以替换为Collections.binarySearch(l, i) >= 0,这可以依靠预先排序的事实来加快搜索速度.但是除非您的清单太长,否则此地图将被构建很多次,我希望差异不会太大.

As suggested by Holger: l.contains(i) could be replaced by Collections.binarySearch(l, i) >= 0 which speeds up the search by relying on the fact that it is pre-sorted. But unless your lists are going to be long or this map will be constructed many times I would expect the difference would be minimal.

您还在注释中(而不是原始问题)提到要维护键顺序.您可以通过显式使用LinkedHashMap来做到这一点.

You also mention in comments (rather than original question) that you want key order to be maintained. You could do this by explicitly using LinkedHashMap.

这篇关于检查列表中的整数是否包含在整数列表的另一个列表中的Java方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆