Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在arraylist中查找重复元素的索引_Java_Arraylist_Binary Search - Fatal编程技术网

Java 在arraylist中查找重复元素的索引

Java 在arraylist中查找重复元素的索引,java,arraylist,binary-search,Java,Arraylist,Binary Search,我试图在字符串的arraylist中找到重复项的索引位置。我很难找到一种有效循环arraylist并报告副本索引的方法。我最初的想法是使用Collections.binarySearch()查找重复项,但我不确定如何使用binarySearch将arraylist的元素相互比较。我唯一的另一个想法是循环浏览这个列表,这个列表非常庞大,很多次甚至都不可行。我的java知识有限,因此非常感谢您的帮助。听起来您运气不好 您将检查每个元素(即迭代整个列表)。从逻辑上考虑——如果可以避免这种情况,这意味着

我试图在字符串的arraylist中找到重复项的索引位置。我很难找到一种有效循环arraylist并报告副本索引的方法。我最初的想法是使用Collections.binarySearch()查找重复项,但我不确定如何使用binarySearch将arraylist的元素相互比较。我唯一的另一个想法是循环浏览这个列表,这个列表非常庞大,很多次甚至都不可行。我的java知识有限,因此非常感谢您的帮助。

听起来您运气不好

您将检查每个元素(即迭代整个列表)。从逻辑上考虑——如果可以避免这种情况,这意味着有一个元素您没有检查过。但是这个元素可以是任何值,也可以是另一个列表元素的副本

当您知道列表中存在某种关系时,二进制搜索是一种减少检查元素数量的智能方法,因此检查一个元素可以为您提供其他元素的信息。例如,对于排序列表,如果中间的元素大于5,则知道后面的每个元素也大于5


然而,我不认为有一种方法可以在重复检查时做出这样的推断。您必须根据“重复的元素数量”对列表进行排序(这是一个很难回答的问题),否则您对元素
x
执行的任何测试都不会让您了解
y
是否重复。

听起来您运气不好

您将检查每个元素(即迭代整个列表)。从逻辑上考虑——如果可以避免这种情况,这意味着有一个元素您没有检查过。但是这个元素可以是任何值,也可以是另一个列表元素的副本

当您知道列表中存在某种关系时,二进制搜索是一种减少检查元素数量的智能方法,因此检查一个元素可以为您提供其他元素的信息。例如,对于排序列表,如果中间的元素大于5,则知道后面的每个元素也大于5


然而,我不认为有一种方法可以在重复检查时做出这样的推断。您必须按照“重复的元素数量”对列表进行排序(这是个问题),否则您对元素
x
执行的任何测试都不会让您了解
y
是否是重复的。

不优雅,但应该可以:

Map<String, List<Integer>> indexList = new HashMap<String, List<Integer>>();
for (int i = 0; i < yourList.size(); i++) {
    String currentString = yourList.get(i);
    List<String> indexes = indexList.get(currentString);
    if (indexes == null) {
         indexList.put(currentString, indexes = new LinkedList<Integer>());
    }
    indexes.add(i);
    if (indexes.size() > 1) {
        // found duplicate, do what you like
    }
}
// if you skip the last if in the for loop you can do this:
for (String string : indexList.keySet()) {
    if (indexList.get(string).size() > 1) {
        // String string has multiple occurences
        // List of corresponding indexes:
        List<Integer> indexes = indexList.get(string);
        // do what you want
    }
}
Map indexList=newhashmap();
对于(int i=0;i1){
//找到重复的,做你喜欢做的
}
}
//如果跳过for循环中的最后一个if,则可以执行以下操作:
for(字符串:indexList.keySet()){
if(indexList.get(string).size()>1){
//字符串多次出现
//相应索引列表:
列表索引=indexList.get(字符串);
//做你想做的
}
}

不优雅,但应该可以:

Map<String, List<Integer>> indexList = new HashMap<String, List<Integer>>();
for (int i = 0; i < yourList.size(); i++) {
    String currentString = yourList.get(i);
    List<String> indexes = indexList.get(currentString);
    if (indexes == null) {
         indexList.put(currentString, indexes = new LinkedList<Integer>());
    }
    indexes.add(i);
    if (indexes.size() > 1) {
        // found duplicate, do what you like
    }
}
// if you skip the last if in the for loop you can do this:
for (String string : indexList.keySet()) {
    if (indexList.get(string).size() > 1) {
        // String string has multiple occurences
        // List of corresponding indexes:
        List<Integer> indexes = indexList.get(string);
        // do what you want
    }
}
Map indexList=newhashmap();
对于(int i=0;i1){
//找到重复的,做你喜欢做的
}
}
//如果跳过for循环中的最后一个if,则可以执行以下操作:
for(字符串:indexList.keySet()){
if(indexList.get(string).size()>1){
//字符串多次出现
//相应索引列表:
列表索引=indexList.get(字符串);
//做你想做的
}
}

现在,这可能不是一个节省内存的解决方案,但是的,我想这就是您想要的。。也许这个计划可以进一步改进

import java.io.*;
import java.util.*;

class ArrayList2_CountingDuplicates
{
public static void main(String[] args)throws IOException
{

ArrayList<String> als1=new ArrayList<String>();
ArrayList<String> als2=new ArrayList<String>();
int arr[];
int n,i,j,c=0;
String s;

BufferedReader p=new BufferedReader(new InputStreamReader(System.in));

n=Integer.parseInt(p.readLine());

arr=new int[n];

for(i=0;i<n;i++)
als1.add(p.readLine());

for(i=0;i<n;i++)
{

s=als1.get(i);
als1.remove(i);
als2.add(s);

arr[c]=1;

while(als1.contains(s))
{
j=als1.indexOf(s);
als1.remove(j);
arr[c]=arr[c]+1;
}
n=n-arr[c];
c=c+1;
i=-1;
}

    for(i=0;i<c;i++)
    System.out.println(als2.get(i)+" has frequency  "+arr[i]);
    }

}
import java.io.*;
导入java.util.*;
类ArrayList2_计数重复项
{
公共静态void main(字符串[]args)引发IOException
{
ArrayList als1=新的ArrayList();
ArrayList als2=新的ArrayList();
int-arr[];
int n,i,j,c=0;
字符串s;
BufferedReader p=新的BufferedReader(新的InputStreamReader(System.in));
n=Integer.parseInt(p.readLine());
arr=新整数[n];

对于(i=0;i现在,这可能不是一个内存效率高的解决方案,但是的,我想这就是您所寻找的..可能是这个程序可以进一步改进

import java.io.*;
import java.util.*;

class ArrayList2_CountingDuplicates
{
public static void main(String[] args)throws IOException
{

ArrayList<String> als1=new ArrayList<String>();
ArrayList<String> als2=new ArrayList<String>();
int arr[];
int n,i,j,c=0;
String s;

BufferedReader p=new BufferedReader(new InputStreamReader(System.in));

n=Integer.parseInt(p.readLine());

arr=new int[n];

for(i=0;i<n;i++)
als1.add(p.readLine());

for(i=0;i<n;i++)
{

s=als1.get(i);
als1.remove(i);
als2.add(s);

arr[c]=1;

while(als1.contains(s))
{
j=als1.indexOf(s);
als1.remove(j);
arr[c]=arr[c]+1;
}
n=n-arr[c];
c=c+1;
i=-1;
}

    for(i=0;i<c;i++)
    System.out.println(als2.get(i)+" has frequency  "+arr[i]);
    }

}
import java.io.*;
导入java.util.*;
类ArrayList2_计数重复项
{
公共静态void main(字符串[]args)引发IOException
{
ArrayList als1=新的ArrayList();
ArrayList als2=新的ArrayList();
int-arr[];
int n,i,j,c=0;
字符串s;
BufferedReader p=新的BufferedReader(新的InputStreamReader(System.in));
n=Integer.parseInt(p.readLine());
arr=新整数[n];

对于(i=0;i我一直在寻找这样一种方法,最终我提出了自己的解决方案,用一种更实用的方法来解决这个问题

public <T> Map<T, List<Integer>> findDuplicatesWithIndexes(List<T> elems) {
    return IntStream.range(0, elems.size())
            .boxed()
            .collect(Collectors.groupingBy(elems::get))
            .entrySet().stream()
            .filter(e -> e.getValue().size() > 1)
            .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
公共地图查找副本索引(列表元素){
返回IntStream.range(0,elems.size())
.boxed()
.collect(收集器.groupingBy(elems::get))
.entrySet().stream()
.filter(e->e.getValue().size()>1)
.collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue));
}

它返回一个由重复元素组成的映射,作为键,重复元素的所有索引列表作为值。

我一直在寻找这样一个方法,最终我用一个更实用的