Java 从表中删除重复项(List<;List<;Integer>;table=new ArrayList…)

Java 从表中删除重复项(List<;List<;Integer>;table=new ArrayList…),java,Java,我有一张桌子 List<List<Integer>> table = new ArrayList<>() 对不起,我的英语很差,谢谢你的快速回复 使用java-8 //Find duplicates Map<Integer, Long> counts = table.stream() .flatMap(Collection::stream) .collect(Collectors.groupingBy

我有一张桌子

List<List<Integer>> table = new ArrayList<>()
对不起,我的英语很差,谢谢你的快速回复

使用java-8

//Find duplicates
Map<Integer, Long> counts = 
    table.stream()
         .flatMap(Collection::stream)
         .collect(Collectors.groupingBy(i->i, Collectors.counting()));

// and remove them
table.stream().forEach(row -> row.replaceAll(i-> counts.get(i) > 1 ? null : i));
//查找重复项
地图计数=
表1.stream()
.flatMap(集合::流)
.collect(Collectors.groupingBy(i->i,Collectors.counting());
//并移除它们
table.stream().forEach(row->row.replaceAll(i->counts.get(i)>1?null:i));
将(稍微不清楚的)问题解释为重复数据消除(删除而不是“置零”):

List dedup=table.stream().distinct().collect(toList());
或java 7:

List<List<Integer>> dedup = new ArrayList<>(new LinkedHashSet<>(table));
List dedup=new ArrayList(new LinkedHashSet(table));
这通常更有用

但是,如果确实需要调零:

Set<List<Integer>> set = new HashSet<>();
List<List<Integer>> dedup = table.stream()
  .filter(list -> set.add(list) ? list : null)
  .collect(toList());
Set Set=newhashset();
列表重复数据消除=table.stream()
.filter(列表->设置添加(列表)?列表:空)
.collect(toList());

集合的
add()
方法返回
true
是在集合中添加更改的元素,即如果它是以前未见过的元素。

试试看,这种方法是为
列表中存在的所有元素构建索引映射
,如果为一个键找到了多个值,则将值设置为
null

    List<List<Integer>> table = new ArrayList<>();
    table.add(Arrays.asList(1, 2, 3));
    table.add(Arrays.asList(4, 1, 5));
    table.add(Arrays.asList(1, 6, 9));

    System.out.println("Before " + table);

    Map<Integer, List<List<Integer>>> indices = new HashMap<>();
    for (int i = 0; i < table.size(); i++) {
        for (int j = 0; j < table.get(i).size(); j++) {
            int el = table.get(i).get(j);
            if (indices.get(el) == null) {
                indices.put(el, new ArrayList<>());
            }
            indices.get(el).add(Arrays.asList(i, j));
        }
    }

    indices.keySet().stream()
                    .filter(k -> indices.get(k).size() > 1)
                    .flatMap(k -> indices.get(k).stream())
                    .forEach(li -> table.get(li.get(0)).set(li.get(1), null));

    System.out.println("After  " + table);

是否要将引用设置为
null
或删除它们?一种更简单的方法是设置一个
,它将隐式删除任何重复项。是否要删除重复的行或值?假设
中的第一个列表包含整数
整数(1)
,第三个列表也包含这样一个对象:是要同时为两个对象设置空值,还是仅为第一个“唯一”对象之后的对象设置空值?是否要保持表的大小相同?每行的行数和条目数相同?@PeterLawrey如何计算
列表
的哈希值,并在
集合
列表
成员之间进行比较?例如,集合
{[1,3],[1,4]}
如何定义重复的内部列表值?(请原谅语法,我只是刚开始学习Java,出于好奇!)@PeterLawrey添加了这个必须如何工作的示例:/
Set<List<Integer>> set = new HashSet<>();
List<List<Integer>> dedup = table.stream()
  .filter(list -> set.add(list) ? list : null)
  .collect(toList());
    List<List<Integer>> table = new ArrayList<>();
    table.add(Arrays.asList(1, 2, 3));
    table.add(Arrays.asList(4, 1, 5));
    table.add(Arrays.asList(1, 6, 9));

    System.out.println("Before " + table);

    Map<Integer, List<List<Integer>>> indices = new HashMap<>();
    for (int i = 0; i < table.size(); i++) {
        for (int j = 0; j < table.get(i).size(); j++) {
            int el = table.get(i).get(j);
            if (indices.get(el) == null) {
                indices.put(el, new ArrayList<>());
            }
            indices.get(el).add(Arrays.asList(i, j));
        }
    }

    indices.keySet().stream()
                    .filter(k -> indices.get(k).size() > 1)
                    .flatMap(k -> indices.get(k).stream())
                    .forEach(li -> table.get(li.get(0)).set(li.get(1), null));

    System.out.println("After  " + table);
Before [[1, 2, 3], [4, 1, 5], [1, 6, 9]]
After  [[null, 2, 3], [4, null, 5], [null, 6, 9]]