从类似对象创建唯一值数组

时间:2018-03-14 19:09:38

标签: java

我正在尝试根据许多相同对象的属性创建一个唯一值数组。这些属性具有相同的值,但其中一些属性为null,如下所示:

Object obj1 = new Object("Value 1", "Value 2", null);
Object obj2 = new Object("Value 1", null, "Value 3");
Object obj3 = new Object(null, "Value2", "Value3")

对象类

public class Object {
   private String value1;
   private String value2;
   private String value3;

   // Constructor
   public Object(String value1, String value2, String value3){ // this.value1... }

   // Getters & Setters
}

(这些对象可以有n个属性,但假设这个问题只有3个)

如何获取上述3个对象(或任何数字对象)并快速组合(或采样)其每个属性以创建以下数组?

["Value 1", "Value 2", "Value 3"] 

我认为Set在这里很有用,但我不太确定如何接近它

3 个答案:

答案 0 :(得分:1)

我假设您的对象看起来像这样:

class MyObject {

    private String[] inputs;

    public MyObject(String... inputs) {
        this.inputs = inputs;
    }
    //Getters Setter

}

如果您使用的是Java 8,则可以使用:

MyObject obj1 = new MyObject("Value 1", "Value 2", null);
MyObject obj2 = new MyObject("Value 1", null, "Value 3");
MyObject obj3 = new MyObject(null, "Value 2", "Value 3");

Set<String> result = Arrays.asList(obj1, obj2, obj3)
        .stream()
        .flatMap(item -> Stream.of(item.getInputs()))
        .filter(item -> item != null)
        .collect(Collectors.toSet());

System.out.println(result);

输出

[Value 3, Value 1, Value 2]

答案 1 :(得分:1)

尝试以下解决方案:

public class Main {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        MyObject obj1 = new MyObject("Value 1", "Value 2", null);
        MyObject obj2 = new MyObject("Value 1", null, "Value 3");
        MyObject obj3 = new MyObject(null, "Value 2", "Value 3");

        System.out.println(myObjectPropToStringArray(obj1,obj2,obj3));


    }

    public static Set<String> myObjectPropToStringArray(MyObject... myObject) {
        Set<String> result = new HashSet<>();
        Arrays.stream(myObject).forEach(e ->{

            result.add(e.getValue1());
            result.add(e.getValue2());
            result.add(e.getValue3());
        });
        return result;
    }
}

 class MyObject {
       private String value1;
       private String value2;
       private String value3;



    public MyObject(String value1, String value2, String value3) {
        super();
        this.value1 = value1;
        this.value2 = value2;
        this.value3 = value3;
    }

    public String getValue1() {
        return value1;
    }

    public void setValue1(String value1) {
        this.value1 = value1;
    }

    public String getValue2() {
        return value2;
    }

    public void setValue2(String value2) {
        this.value2 = value2;
    }

    public String getValue3() {
        return value3;
    }

    public void setValue3(String value3) {
        this.value3 = value3;
    }
 }

答案 2 :(得分:0)

如果您只想在这些对象的每个字段中选择所有不同的值,那么您可以使用每个字段值的列表:

List<Object> objects = Arrays.asList(new Object("Value 1", "Value 2", null), 
          new Object("Value 1", null, "Value 3"), 
          new Object(null, "Value2", "Value3");

//This should give you the first list...
List<String> values = objects.stream().map(o ->
             Arrays.asList(o.getVal1(), o.getVal2(), o.getVal3()))
             .distinct()
             .findFirst().get();

//If you need to prefer non-null values, then you can use
//an ordered stream:
List<String> values = objects.stream()
            .map(o ->
                 Arrays.asList(o.getVal1(), o.getVal2(), o.getVal3()))
            .distinct().ordered(list -> 
                (list.get(0) != null ? -1 : 1)
              + (list.get(1) != null ? -1 : 1)
              + (list.get(2) != null ? -1 : 1))
            .findFirst()
            .get();

比较器实现只是为了使非空值出现在流的开头,以便findFirst可以先命中它们。 当然,您需要检查可选项是否具有值。