繁体   English   中英

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

[英]Create an Array of Unique Values from Similar Objects

我试图基于许多相同对象的属性创建唯一值数组。 这些属性将具有相同的值,但其中一些将为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在这里可能有用,但是我不确定如何处理它

我认为你的对象看起来像这样:

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]

请尝试以下解决方案:

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;
    }
 }

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

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可以首先将它们命中。 当然,您将需要检查可选参数是否具有值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM