java - Java:在数组中查找重复项
问题描述
我需要在我的程序中实现重复搜索方法,但出现错误。如果我注释掉重复的搜索部分,程序运行良好。
错误(我注释掉了重复的搜索部分):
do{
System.out.print("Please enter an integer or -1 to stop: ");
input=Integer.parseInt(scan.nextLine()); //parse library function
/**
* for(int i=0; i<A.length; i++){
* for(int j=i+1; j<A.length; j++{
* if(A[i].equals(A[j]))
* System.out.println("Duplicate input. Please enter another
value: ");
* }
* }
*/
if(input != -1) //if input is
Display.userInput(input);
}
while(input != -1);
解决方案
int A[] = {1, 5, 9, 5, 6};
if (Arrays.stream(A).boxed()
.collect(Collectors.groupingBy(Function.identity()))
.entrySet()
.stream()
.map(Map.Entry::getValue)
.map(Collection::size)
.anyMatch(s -> s > 1)) {
System.out.println("Duplicate input. Please enter another value:");
}
推荐阅读
- java - KafkaListener 监听多个主题时,能否优先考虑单个主题?
- json - Pyspark - 如何分组和创建键值对列
- c# - 折叠层次结构中选定的游戏对象
- javascript - Javascript:变量声明上下文中的语法
- sql - SQL比较2列的值并逐行选择具有最大值的列
- android - 我想在将数据添加到 RoomDatabase 后更改按钮文本
- php - How to duplicate and auto increment columns in sql?
- java - 如何在@InjectMocks 中发送@Autowired 字段
- python - 在 Pandas 中查找列并为其赋值
- google-cloud-platform - 如何生成作为 POST 请求到达 Confluent Kafka 主题的事件