在Java中分别打印重复值和非重复值

时间:2019-07-24 01:27:02

标签: java printing java-8 duplicates printf

我想分别打印重复值和非重复值。

String[] values= { "Java","C", "JavaScript", "JavaScript", "Java" ,"Java", "Java" , "JavaScript", "Java", "Ruby"};

我的输出应该在下面。

String[] duplicate = {"Java", "Java", "Java", "Java", "Java", "JavaScript", "JavaScript", "Java"}

String[] unique = {"C", "Ruby"} 

我尝试了几种方法,但我做不到。

public static void main(String[] args) {

        String[] a = { "Java","C", "JavaScript", "JavaScript", "Java" ,"Java", "Java" , "JavaScript", "Java", "Ruby"};

         List<String> uniq = new ArrayList<String>();
         List<String> dupe = new ArrayList<String>();

        int count = 0;
        for (int j = 0; j < a.length; j++) {
            for (int k = j + 1; k < a.length; k++) {
                if (a[j] == a[k]) {
                    count++;
                }
            }
            if (count == 1){
                System.out.println("Dupe: "+a[j]);
                dupe.add(a[j]);
            } 
            count = 0;

        }
    }

输出


String[] Duplicate = {"Java", "Java", "Java", "Java", "Java", "JavaScript", "JavaScript"}

String[] Unique = {"C", "Ruby"}

6 个答案:

答案 0 :(得分:1)

answer可能对您有所帮助。

它表示您可以使用集合来创建输入数组唯一值的集合,例如:

int length= a.length;
Set<String> set = new HashSet<String>();

for(int i = 0; i < length; i++){
  set.add(a[i]);
}

然后,您可以通过从原始数组a创建一个ArrayList并从新ArrayList中删除所有唯一值来确定重复值,例如:

ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(a));
arrayList.removeAll(set)

这两个命令的最终结果是arrayList仅包含a最初具有的重复值,而set仅包含a的唯一值。

答案 1 :(得分:1)

您可以使用Collectors.partitioningBy一次迭代,但是返回类型为Map<Boolean, List<String>>

List<String> l = Arrays.asList(values);
Map<Boolean, List<String>> d = Arrays.stream(values)
             .collect(Collectors.partitioningBy(i->Collections.frequency(l, i)>1));
List键相关的

输出 true在重复数组中,其他不在重复数组中

{false=[C, Ruby], true=[Java, JavaScript, JavaScript, Java, Java, Java, JavaScript, Java]}

答案 2 :(得分:0)

以下代码将为您提供一个包含字符串和出现次数的Map:

import java.util.HashMap;
import java.util.Map;

public class TestSample {
    public static void main(String[] args) {
        String[] values =
                { "Java", "C", "JavaScript", "JavaScript", "Java", "Java", "Java", "JavaScript", "Java", "Ruby" };

        Map<String, Integer> decisionMap = new HashMap<>();
        for (String str : values) {
            if (decisionMap.containsKey(str)) {
                decisionMap.put(str, decisionMap.get(str) + 1);
                continue;
            }
            decisionMap.put(str, 0);
        }

        System.out.println(decisionMap.toString());

    }
}

唯一字符串对键的值为0。 现在,您可以选择打印方式。

答案 3 :(得分:0)

可能不是最有效的,而是一种方法:

// input: String []  a
// initialize empty:
Set<String> u = new HashSet<>(a.length);
// initialize with all values of a:
List<String> d = new ArrayList<>(Arrays.asList(a));
for(String s:a) {
  if (u.add(a)) {// s is added to u for the first time:
    d.remove(a);
  } else { // s is duplicate in u:
    u.remove(a);
  }
}
String [] us = u.toArray(new String[0]);
String [] ds = d.toArray(new String[0]); 

...利用集合的特征(元素唯一性)和Set.add():boolean方法,当“通过添加操作修改集合时”向true发信号。

答案 4 :(得分:0)

要打印唯一值,可以使用 Collection框架

    HashSet<String> h = new HashSet<String>(); 
   //Adding elements into HashSet add() 
   for(int i = 0; i < length; i++)
   {
   h.add(a[i]);           //adding element in the set
   }
      // Iterating over hash set items 
   for (String i : h)
   {  
      System.out.println(i);     //printing unique elements  
    }

答案 5 :(得分:0)

这是使用Java 8的另一种方法

public class Test {
    public static void main(String[] args) {
        String[] values = { "Java", "C", "JavaScript", "JavaScript", "Java", "Java", "Java", "JavaScript", "Java",
                "Ruby" };
        List<String> l = new LinkedList<>(Arrays.asList(values));// you can not use List<String> l = Arrays.asList(values); because we will be removing the duplicate values later so you will get java.lang.UnsupportedOperationException
        Set<String> allItems = new HashSet<String>();
        List<String> duplicates = Arrays.stream(values).filter(n -> !allItems.add(n)).collect(Collectors.toList());
        System.out.println(duplicates);//this will print all the duplicate values
        l.removeAll(duplicates);// input=duplicate+unique so removing the duplicate will give the unique values
        System.out.println(l);

    }

}

输出:

[JavaScript, Java, Java, Java, JavaScript, Java]
[C, Ruby]