简体   繁体   English

如何检查List是否按顺序包含多个元素

[英]How to check if List contains several elements in order

Let's say I have a List, 假设我有一个列表,

Arrays.asList("A","B","C","D");

Is there a convenience method in Java that allows me to check this List to see if the elements C and D are on this list in that order? Java中是否有一种便捷方法允许我检查此列表以查看元素C和D是否按此顺序列在此列表中?

Something along the lines of: 有点像:

Collections.containsOrderedElements(list,"C","D");
Collections.containsOrderedElements(list,"A","B","C");

Where the above methods will return true but 上述方法将返回true但是

Collections.containsOrderedElements(list,"A","C");

will not? 将不会?

Thank you for your time. 感谢您的时间。

There is a method that can help ( Collections.indexOfSubList(sourceList,subList) ): 有一个方法可以帮助( Collections.indexOfSubList(sourceList,subList) ):

List<String> list = Arrays.asList("A", "B", "C", "D");

System.out.println( (Collections.indexOfSubList(list, Arrays.asList("C", "D"))>= 0));
System.out.println( (Collections.indexOfSubList(list, Arrays.asList("A","B","C"))>= 0));
System.out.println( (Collections.indexOfSubList(list, Arrays.asList("A", "C"))>= 0));

It prints: 它打印:

true
true
false

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

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