如何在java8中使用if有效地创建列表?

时间:2016-03-05 05:29:42

标签: java-8

我已经开始学习Java8.I一直按照传统的方式使用if条件创建一个列表(对于下面的代码)。我的代码工作但有没有有效的方法来实现这个使用java8?请让我知道

private List<String> getDeliveryAddressTypeList() {
    List<String> deliveryAddressTypeList = new ArrayList<>();
    deliveryAddressTypeList.add(DeliveryAddressType.MY_DEFAULT_ADDRESS.getValue());
    if (SecurityUtils.getSubject().isPermitted(PermissionConstant.PHARMISA_DELIVERY_PFDEPORTS)) {
        deliveryAddressTypeList.add(DeliveryAddressType.DEPOT_ADDRESS.getValue());
    }
    if (SecurityUtils.getSubject().isPermitted(PermissionConstant.PHARMISA_DELIVERY_ALL_CONTACTS)) {
        deliveryAddressTypeList.add(DeliveryAddressType.CONTACT_ADDRESS.getValue());
    }
    if (SecurityUtils.getSubject().isPermitted(PermissionConstant.PHARMISA_DELIVERY_FREE_FROM_ADDRESS)) {
        deliveryAddressTypeList.add(DeliveryAddressType.AN_ALTERNATIVE_ADDRESS.getValue());
    }
    return deliveryAddressTypeList;
}

3 个答案:

答案 0 :(得分:4)

您可以分开代码和数据。像这样初始化静态地图:

Map<DeliveryAddressType, Predicate<Subject>> permissions = new HashMap<>();
permissions.put(DeliveryAddressType.MY_DEFAULT_ADDRESS, s -> true);
permissions.put(DeliveryAddressType.DEPOT_ADDRESS, 
    s -> s.isPermitted(PermissionConstant.PHARMISA_DELIVERY_PFDEPORTS));
permissions.put(DeliveryAddressType.CONTACT_ADDRESS, 
    s -> s.isPermitted(PermissionConstant.PHARMISA_DELIVERY_ALL_CONTACTS));
permissions.put(DeliveryAddressType.AN_ALTERNATIVE_ADDRESS, 
    s -> s.isPermitted(PermissionConstant.PHARMISA_DELIVERY_FREE_FROM_ADDRESS));

现在你可以使用

private List<String> getDeliveryAddressTypeList() {
    Subject subject = SecurityUtils.getSubject();
    return permissions.entrySet().stream()
        .filter(entry -> entry.getValue().test(subject))
        .map(entry -> entry.getKey().getValue())
        .collect(Collectors.toList());
}

答案 1 :(得分:1)

这里的问题是映射PermissionConstant -> DeliveryAddressType。 假设您已经定义了常量Map<PermissionConstant, DeliveryAddressType>(如果插入顺序很重要,您也可以使用LinkedHashMap

Map<PermissionConstant, DeliveryAddressType> mappings = new HashMap<>();
mappings.put(PermissionConstant.PHARMISA_DELIVERY_PFDEPORTS, DeliveryAddressType.DEPOT_ADDRESS);
mappings.put(PermissionConstant.PHARMISA_DELIVERY_ALL_CONTACTS, DeliveryAddressType.CONTACT_ADDRESS);
mappings.put(PermissionConstant.PHARMISA_DELIVERY_FREE_FROM_ADDRESS, DeliveryAddressType.AN_ALTERNATIVE_ADDRESS);

然后你可以简单地连接并流式传输地图以获得结果:

private static List<String> getDeliveryAddressTypeList() {
  Subject subject = SecurityUtils.getSubject();
  return Stream.concat(
      Stream.of(DeliveryAddressType.MY_DEFAULT_ADDRESS.getValue()),
        mappings.entrySet().stream()
          .filter(m -> subject.isPermitted(m.getKey()))
          .map(m -> m.getValue().getValue())
      ).collect(Collectors.toList());
}

答案 2 :(得分:1)

我相信最干净的方式就是拥有它。尽管如此,您可以使用辅助方法来避免重复的if语句:

static void addIfPermitted(List<String> list, DeliveryAddressType addressType, 
    PermissionConstant permission) {

    if (SecurityUtils.getSubject().isPermitted(permission) {
        list.add(addressType.getValue());
    }
}

现在你可以这样使用这个辅助方法:

List<String> deliveryAddressTypeList = new ArrayList<>(Arrays.asList(
    DeliveryAddressType.MY_DEFAULT_ADDRESS.getValue()));

addIfPermitted(deliveryAddressTypeList, 
    DeliveryAddressType.DEPOT_ADDRESS, 
    PermissionConstant.PHARMISA_DELIVERY_PFDEPORTS);

addIfPermitted(deliveryAddressTypeList, 
    DeliveryAddressType.CONTACT_ADDRESS, 
    PermissionConstant.PHARMISA_DELIVERY_ALL_CONTACTS);

addIfPermitted(deliveryAddressTypeList, 
    DeliveryAddressType.AN_ALTERNATIVE_ADDRESS, 
    PermissionConstant.PHARMISA_DELIVERY_FREE_FROM_ADDRESS);

如果你想使用java 8的东西,你可以创建一个Map<DeliveryAddressType, PermissionConstant>

List<String> deliveryAddressTypeList = new ArrayList<>(Arrays.asList(
    DeliveryAddressType.MY_DEFAULT_ADDRESS.getValue()));

Map<DeliveryAddressType, PermissionConstant> map = new HashMap<>();

map.put(DeliveryAddressType.DEPOT_ADDRESS, 
    PermissionConstant.PHARMISA_DELIVERY_PFDEPORTS);
map.put(DeliveryAddressType.CONTACT_ADDRESS, 
    PermissionConstant.PHARMISA_DELIVERY_ALL_CONTACTS);
map.put(DeliveryAddressType.AN_ALTERNATIVE_ADDRESS, 
    PermissionConstant.PHARMISA_DELIVERY_FREE_FROM_ADDRESS);

然后:

map.forEach((addressType, permission) -> 
    addIfPermitted(deliveryAddressList, adressType, permission));

另一种方法,使用流和相同的地图,但从地图键创建最终列表:

List<String> deliveryAddressTypeList = map.entrySet().stream()
    .filter(e -> SecurityUtils.getSubject().isPermitted(e.getValue()))
    .map(Map.Entry::getKey)
    .map(DeliveryAddressType::getValue)
    .collect(Collectors.toList());

deliveryAddressTypeList.add(DeliveryAddressType.MY_DEFAULT_ADDRESS.getValue());

在此处过滤地图条目,并仅保留安全性允许其值(PermissionConsdtant)的条目。然后,将每个已过滤的条目转换为其键(DeliveryAddressType),然后将每个DeliveryAddressType转换为其值。最后,将所有这些传递地址类型值收集到列表中。