ch.lambdaj.Lambda.collect()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(4.5k)|赞(0)|评价(0)|浏览(204)

本文整理了Java中ch.lambdaj.Lambda.collect()方法的一些代码示例,展示了Lambda.collect()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Lambda.collect()方法的具体详情如下:
包路径:ch.lambdaj.Lambda
类名称:Lambda
方法名:collect

Lambda.collect介绍

[英]Collects the items in the given iterable putting them in a List. Actually it handles also Maps, Arrays and Iterator by collecting their values. Note that this method accepts an Object in order to be used in conjunction with the Lambda#forEach(Iterable).
[中]收集给定iterable中的项并将其放入列表中。实际上,它还通过收集映射、数组和迭代器的值来处理它们。请注意,此方法接受一个对象,以便与Lambda#forEach(Iterable)一起使用。

代码示例

代码示例来源:origin: org.motechproject/motech-mobileforms-api

public List<FormBean> sortByDependency() {
  final ArrayList<FormBean> formBeansClone = new ArrayList<FormBean>(formBeans);
  List<FormBean> sortedFormBeans = new ArrayList<FormBean>();
  for (FormBean formBean : formBeans) {
    if (CollectionUtils.isEmpty(formBean.getDepends())) {
      sortedFormBeans.add(formBean);
      formBeansClone.remove(formBean);
    }
  }
  final List<String> namesOfForms = collect(formBeans, on(FormBean.class).getFormname());
  resolveDependency(sortedFormBeans, formBeansClone, namesOfForms);
  return sortedFormBeans;
}

代码示例来源:origin: mariofusco/lambdaj

/**
 * For each item in the given iterable collects the value defined by the given argument and put them in a List.
 * For example the following code:
 * <pre>
 *         List&lt;Person&gt; myFriends = asList(new Person("Biagio", 39), new Person("Luca", 29), new Person("Celestino", 29));
 *        List&lt;Integer&gt; ages = collect(meAndMyFriends, on(Person.class).getAge());
 * </pre>
 * extracts the ages of all the Persons in the list and put them in a List of Integer.
 * <p/>
 * Actually it handles also Maps, Arrays and Iterator by collecting their values.
 * Note that this method accepts an Object in order to be used in conjunction with the {@link Lambda#forEach(Iterable)}.
 * @param iterable The iterable of which the items should be collected
 * @param argument An argument defined using the {@link Lambda#on(Class)} method 
 * @return A List containing all the items collected from the give iterable
 * @throws RuntimeException if the iterable is not an Iterable or a Map
 */
public static <T> List<T> collect(Object iterable, T argument) {
  return (List<T>)collect(convert(iterable, new ArgumentConverter<Object, T>(argument)));
}

代码示例来源:origin: org.motechproject/motech-mobileforms-api

private void resolveDependency(List<FormBean> sortedFormBeans, List<FormBean> unprocessedFormBeans, List<String> namesOfForms) {
  if (!unprocessedFormBeans.isEmpty()) {
    int processedNodes = 0;
    for (FormBean unprocessedFormBean : new ArrayList<FormBean>(unprocessedFormBeans)) {
      boolean independent = true;
      for (String dependent : unprocessedFormBean.getDepends()) {
        if (namesOfForms.contains(dependent)) {
          List<String> processedFormNames = collect(sortedFormBeans, on(FormBean.class).getFormname());
          if (!processedFormNames.contains(dependent)) {
            independent = false;
            break;
          }
        }
      }
      if (independent) {
        sortedFormBeans.add(unprocessedFormBean);
        unprocessedFormBeans.remove(unprocessedFormBean);
        processedNodes++;
      }
    }
    if (processedNodes > 0) {
      resolveDependency(sortedFormBeans, unprocessedFormBeans, namesOfForms);
    } else {
      throw new MotechException("Detected cyclic mobile form dependencies");
    }
  }
}

代码示例来源:origin: org.motechproject/message-aggregator

private boolean canBeDelivered(MessageGroup group) {
    final Collection<Message<?>> messages = group.getMessages();

    if (messages.size() <= 0) {
      return false;
    }

    List<T> eventsInPayload = convert(collect(messages, on(Message.class).getPayload()), new Converter<Object, T>() {
      @Override
      public T convert(Object from) {
        return (T) from;
      }
    });

    boolean canBeDispatched = aggregationHandler.canBeDispatched(eventsInPayload);
    logger.debug("Events: " + eventsInPayload + ". Can be dispatched? " + canBeDispatched);
    return canBeDispatched;
  }
}

代码示例来源:origin: org.motechproject/motech-mobileforms-api

List<FormBean> allForms = flatten(collect(studies, on(Study.class).forms()));
for (Study study : studies) {
  for (FormBeanGroup group : study.groupedForms()) {

代码示例来源:origin: org.motechproject/motech-mobileforms-api

private void handleDownloadUsersAndForms(ByteArrayOutputStream byteStream, DataInputStream dataInput)
    throws IOException, SerializerException {
  try {
    serializer.serializeUsers(byteStream, usersService.getUsers());
  } catch (Exception e) {
    throw new SerializerException(e);
  }
  int studyIndex = dataInput.readInt();
  FormGroup groupNameAndForms = mobileFormsService.getForms(studyIndex);
  List<String> formsXmlContent = collect(groupNameAndForms.getForms(), on(Form.class).content());
  try {
    serializer.serializeForms(byteStream, formsXmlContent, studyIndex, groupNameAndForms.getName());
  } catch (Exception e) {
    throw new SerializerException(e);
  }
}

相关文章