找到目标对象的最近对象(之前和之后)

时间:2013-04-09 05:09:02

标签: java

我想实现下面给出的两种方法。 SomeObject 的字段 createdDate 类型为Date

private SomeObject getNearestObjectBeforeTargetObjectsCreatedDate(List<SomeObject> someObjectList, SomeObject targetObject){

}


private SomeObject getNearestObjectAfterTargetObjectsCreatedDate(List<SomeObject> someObjectList, SomeObject targetObject){

}

假设我按创建日期的升序排列了5个对象P1,P2,P3,P4,P5。而且目标对象是P3 ,那么第一种方法应该返回P2 第二种应该返回P4

目前我还有类似的东西

private SomeObject getNearestPortFolio(List<SomeObject> someObjectList, SomeObject targetObject){
    SomeObject returnObject = targetObject;

      for(SomeObject someObject : someObjectList) {
        // if the current iteration's date is "before" the target date
          if(someObject.getCreatedDate().compareTo(targetObject.getCreatedDate()) < 0) {

              if (someObject.getCreatedDate().compareTo(returnObject.getCreatedDate()) > 0){
                  returnObject = someObject;
          }

      }
     }
      return returnObject;
    }

2 个答案:

答案 0 :(得分:0)

你可以这样做。确保根据您的需要进行必要的更改,但核心逻辑将保持不变。

// Note that if the target is the first element, this will return null.
private static String getNearestBefore(List<String> test, String target) {
    String returnObj = null;
    String previous = null;
    for (String someObj : test) {
        if (someObj.equals(target)) { //Insert your condition here
            returnObj = previous;
        }
        previous = someObj;
    }
    return returnObj;
}

// Note that if the target is the last element, this will return null.
private static String getNearestAfter(List<String> test, String target) {
    String returnObj = null;
    boolean nextSatisfied = false;
    for (String someObj : test) {
        if (nextSatisfied) {
            returnObj = someObj;
            break;
        }
        if (someObj.equals(target)) { //Insert your condition here
            nextSatisfied = true;
        }
    }
    return returnObj;
}

答案 1 :(得分:0)

根据您的需要调整以下代码。它等同于getNearestObjectAfter。该函数返回下一个元素的索引,该索引大于目标key。目标列表中不必包含key。必须对目标列表进行排序。

public static <T extends Comparable<? super T>> int upperBound(List<T> list, T key)
{
  int index = Collections.binarySearch(list, key);
  return (0 <= index) ? upperBound(list, key, index) : -index - 1;
}

protected static <T extends Comparable<? super T>> int upperBound(List<T> list, T key, int index)
{
  // -- PRE: 0 <= index < size

  int size = list.size();
  if (list instanceof RandomAccess)
  {
    while (   (++index < size)
           && (0 == list.get(index).compareTo(key)))
    {
      // -- nothing additional to do
    }
  }
  else
  {
    for ( ListIterator<? extends T> I = list.subList(++index, size).listIterator()
        ; I.hasNext()
        ; )
    {
      if (0 == I.next().compareTo(key))
      {
        ++index;
      }
      else
      {
        break;
      }
    }
  }

  return index;
}

这两种方法自然延伸到“无法比较”的对象:

public static <T> int upperBound(List<? extends T> list, T key, Comparator<? super T> comparator)
{
  int index = Collections.binarySearch(list, key, comparator);
  return (0 <= index) ? upperBound(list, key, index, comparator) : -index - 1;
}

protected static <T> int upperBound(List<? extends T> list, T key, int index, Comparator<? super T> comparator)
{
  // -- PRE: 0 <= index < size

  int size = list.size();
  if (list instanceof RandomAccess)
  {
    while (   (++index < size)
           && (0 == comparator.compare(list.get(index), key)))
    {
      // nothing additional to do
    }
  }
  else
  {
    for ( ListIterator<? extends T> I = list.subList(++index, size).listIterator()
        ; I.hasNext()
        ; )
    {
      if (0 == comparator.compare(I.next(), key))
      {
        ++index;
      }
      else
      {
        break;
      }
    }
  }

  return index;
}