如何为这些场景包装util方法

时间:2016-09-04 10:27:30

标签: java

在日常开发中,我发现在某些情况下java非常不方便,例如。

示例1

    String[] descArray = {"aaa", "bbb", "ccc"};  // coupon desciption
    List<String> codeList = newArrayList("111", null, "333"); // coupon code
    // find those coupon which does not have code
    List<String> nullElementList = newArrayList();
    for (int i = 0; i < codeList.size(); i++) {
        if (codeList.get(i) == null) {
            nullElementList.add(descArray[i]);
        }
    }
    assertThat(nullElementList).containsExactly("bbb");

示例2

    String[] descArray = {"aaa", "bbb", "ccc"}; // coupon description
    List<String> codeList = newArrayList("111", "222", "333"); // coupon code
    Map<String,CouponInfo> descCouponInfoMap = ImmutableMap.of("aaa", new CouponInfo("aaa", 1), "bbb", new CouponInfo("bbb", 2), "ccc", new CouponInfo("ccc", 3)); // desc -- couponInfo

    // to generate new Map<code, count>
    Map<String,Integer> codeCountMap = new HashMap<>();
    for (int i = 0; i < codeList.size(); i++) {
        codeCountMap.put(codeList.get(i), descCouponInfoMap.get(descArray[i]).getCount());
    }


    assertThat(codeCountMap).containsExactly(new DefaultMapEntry("111",1),new DefaultMapEntry("222",2),new DefaultMapEntry("333",3));

示例3

    List<Foo> fooList = newArrayList(new Foo("aaa"), new Foo("bbb"), new Foo("ccc"));
    List<Bar> barList = newArrayList(new Bar("111"), new Bar("222"), new Bar("333"));
    Map<String,String> descCodeMap = new HashMap<>();

    for (int i = 0; i < fooList.size(); i++) {
        descCodeMap.put(fooList.get(i).getDesc(), barList.get(i).getCode());
    }

    assertThat(descCodeMap).contains(new DefaultMapEntry("aaa","111"),new DefaultMapEntry("bbb","222"),new DefaultMapEntry("ccc","333"));

如例1所示,可以提供以下包装的util方法来实现它

static <T>List<T> findNullElementList(List<T> srcList, List<T> destList)

但是最后两个怎么样?开发人员可以动态指定对象的某些属性。

2 个答案:

答案 0 :(得分:2)

看看你给出的例子,我怀疑你发现它不方便,因为你并没有真正充分发挥OO设计的潜力。

以下为例:

String[] descArray = {"aaa", "bbb", "ccc"};  // coupon desciption
List<String> codeList = newArrayList("111", null, "333"); // coupon code

您将3个对象的属性存储在2个单独的数组中。如果您只有一个Coupon类,您可以开始封装对象的一些行为并导致更好的设计:

for(Coupon coupon : coupons) {
    if(coupon.getDescription() == null) {
        nullElementList.add(coupon);
    }
}

答案 1 :(得分:0)

如果在这些情况下使用java8 + Pair可能有所帮助,请参阅下文

首先提供一个util方法来合并两个列表,

<L,R>List<Pair<L,R>> merge(List<L> list1, List<R> list2){
   List<Pair<L,R>> pairList = new ArrayList<>(list1.size());
    for (int i = 0; i < list1.size(); i++) {
        Pair pair = Pair.of(list1.get(i),list2.get(i));
        pairList.add(pair);
    }
    return pairList;
}

然后 例1

    String[] descArray = {"aaa", "bbb", "ccc"};  
    List<String> codeList = newArrayList("111", null, "333"); 
    List<String> nullElementList = merge(asList(descArray), codeList).stream().filter(p -> p.getRight() == null).map(p->p.getLeft()).collect(toList());
    assertThat(nullElementList).containsExactly("bbb");

示例2

    String[] descArray = {"aaa", "bbb", "ccc"}; 
    List<String> codeList = newArrayList("111", "222", "333"); 
    Map<String,CouponInfo> descCouponInfoMap = ImmutableMap.of("aaa", new CouponInfo("aaa", 1), "bbb", new CouponInfo("bbb", 2), "ccc", new CouponInfo("ccc", 3)); 

    Map<String, Integer> codeCountMap = merge(asList(descArray), codeList).stream().collect(toMap(p -> p.getRight(), p -> descCouponInfoMap.get(p.getLeft()).getCount()));

    assertThat(codeCountMap).containsExactly(new DefaultMapEntry("111",1),new DefaultMapEntry("222",2),new DefaultMapEntry("333",3));

示例3

    List<Foo> fooList = newArrayList(new Foo("aaa"), new Foo("bbb"), new Foo("ccc"));
    List<Bar> barList = newArrayList(new Bar("111"), new Bar("222"), new Bar("333"));
    Map<String, String> descCodeMap = merge(fooList, barList).stream().collect(toMap(p -> p.getLeft().getDesc(), p -> p.getRight().getCode()));