Java Code Examples for java.util.LinkedList#peekFirst()

The following examples show how to use java.util.LinkedList#peekFirst() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SQFSyntaxChecker.java    From arma-intellij-plugin with MIT License 6 votes vote down vote up
@Nullable
private ValueType getPeekType(@NotNull LinkedList<ExprPart> parts,
							  @NotNull LinkedList<ExprPart> removedParts,
							  @NotNull ProblemsHolder problems,
							  @NotNull LinkedList<PotentialProblem> potentialProblems,
							  @NotNull Counter reportCount) {
	ExprPart peekPart = parts.peekFirst();
	if (peekPart == null) {
		return null;
	}
	if (peekPart.isOperatorPart()) {
		return getReturnTypeForCommand(
				parts,
				null,
				problems,
				potentialProblems,
				reportCount,
				true
		);
	} else {
		removedParts.push(parts.removeFirst());
		return peekPart.getArgument().getType(this, true, cluster);
	}
}
 
Example 2
Source File: SQFSyntaxChecker.java    From arma-intellij-plugin with MIT License 6 votes vote down vote up
@Nullable
private ValueType getPeekType(@NotNull LinkedList<ExprPart> parts,
							  @NotNull LinkedList<ExprPart> removedParts,
							  @NotNull ProblemsHolder problems,
							  @NotNull LinkedList<PotentialProblem> potentialProblems,
							  @NotNull Counter reportCount) {
	ExprPart peekPart = parts.peekFirst();
	if (peekPart == null) {
		return null;
	}
	if (peekPart.isOperatorPart()) {
		return getReturnTypeForCommand(
				parts,
				null,
				problems,
				potentialProblems,
				reportCount,
				true
		);
	} else {
		removedParts.push(parts.removeFirst());
		return peekPart.getArgument().getType(this, true, cluster);
	}
}
 
Example 3
Source File: StackUtils.java    From xlsmapper with Apache License 2.0 6 votes vote down vote up
/**
 * スタックの先頭の要素(一番上の要素)が引数で指定した文字列の何れかと等しいかどうか比較する。
 * @param stack
 * @param strs 比較する文字列の配列
 * @return
 */
public static boolean equalsAnyTopElement(final LinkedList<String> stack, final String[] strs) {
    
    ArgUtils.notNull(stack, "stack");
    ArgUtils.notEmpty(strs, "strs");
    
    if(stack.isEmpty()) {
        return false;
    }
    
    final String top = stack.peekFirst();
    for(String str : strs) {
        if(str.equals(top)) {
            return true;
        }
    }
    
    return false;
    
}
 
Example 4
Source File: StackUtils.java    From super-csv-annotation with Apache License 2.0 6 votes vote down vote up
/**
 * スタックの先頭の要素(一番上の要素)が引数で指定した文字列の何れかと等しいかどうか比較する。
 * @param stack
 * @param strs 比較する文字列の配列
 * @return
 */
public static boolean equalsAnyTopElement(final LinkedList<String> stack, final String[] strs) {
    
    Objects.requireNonNull("stack should not be null.");
    Objects.requireNonNull("strs should not be null.");
    
    if(stack.isEmpty()) {
        return false;
    }
    
    final String top = stack.peekFirst();
    for(String str : strs) {
        if(str.equals(top)) {
            return true;
        }
    }
    
    return false;
    
}
 
Example 5
Source File: SlidingWindowMaxArray.java    From Project with Apache License 2.0 5 votes vote down vote up
public static int[] getMaxWindow(int[] arr, int w) {
    if (arr == null || w < 1 || arr.length < w) {
        return null;
    }
    // LinkedList 就是一个标准的双向链表
    LinkedList<Integer> maxList = new LinkedList<Integer>();
    // 生成的结果数组
    int[] res = new int[arr.length - w + 1];
    int index = 0;
    for (int i = 0; i < arr.length; i++) {
        // 更新双端队列,如果双端队列不为空,并且尾结点(存的是下标)对应数组中的值是否小于等于当前值
        while (!maxList.isEmpty() && arr[maxList.peekLast()] <= arr[i]) {
            maxList.pollLast();
        }
        // 上面一直弹出,直到不符合然后加上当前值。
        maxList.addLast(i);
        // 上面加法是通用的,但是减法是针对该题定制的
        // 当过期的时候(当窗口形成之后再扩充才算过期)即窗口长度 > w,窗口形成过程中不会过期, i - w表示过期的下标
        if (maxList.peekFirst() == i - w) {
            maxList.pollFirst();
        }
        // 判断下标过期
        if (i >= w - 1) {
            // 当窗口已经形成了,记录每一步的res
            res[index++] = arr[maxList.peekFirst()];
        }
    }
    return res;
}
 
Example 6
Source File: AllLessNumSubArray.java    From Project with Apache License 2.0 5 votes vote down vote up
/**
 * 使用双向最大最小值更新结构,时间复杂度为 O(N)
 */
public static int getNum(int[] arr, int num) {
    if (arr == null || arr.length == 0) {
        return 0;
    }
    // 分别准备最大值和最小值更新结构
    LinkedList<Integer> qmax = new LinkedList<Integer>();
    LinkedList<Integer> qmin = new LinkedList<Integer>();
    int L = 0;
    int R = 0;
    int res = 0;
    while (L < arr.length) {
        while (R < arr.length) {
            while (!qmin.isEmpty() && arr[qmin.peekLast()] >= arr[R]) {
                qmin.pollLast();
            }
            qmin.addLast(R);
            while (!qmax.isEmpty() && arr[qmax.peekLast()] <= arr[R]) {
                qmax.pollLast();
            }
            qmax.addLast(R);
            // 不达标
            if (arr[qmax.getFirst()] - arr[qmin.getFirst()] > num) {
                break;
            }
            R++;
        }
        if (qmin.peekFirst() == L) {
            qmin.pollFirst();
        }
        if (qmax.peekFirst() == L) {
            qmax.pollFirst();
        }
        res += R - L;
        // 换一个开头
        L++;
    }
    return res;
}
 
Example 7
Source File: Offer59I.java    From Project with Apache License 2.0 5 votes vote down vote up
public int[] maxSlidingWindow(int[] nums, int k) {
    if (nums == null || k < 1 || nums.length < k) {
        return new int[0];
    }

    LinkedList<Integer> maxList = new LinkedList<Integer>();
    int[] res = new int[nums.length - k + 1];
    int index = 0;
    for (int i = 0; i < nums.length; i++) {
        // 更新双端队列,如果双端队列不为空,并且尾结点(存的是下标)对应数组中的值是否小于等于当前值
        while (!maxList.isEmpty() && nums[maxList.peekLast()] <= nums[i]) {
            maxList.pollLast();
        }
        // 上面一直弹出,直到不符合然后加上当前值。
        maxList.addLast(i);
        // 上面加法是通用的,但是减法是针对该题定制的
        // 当过期的时候(当窗口形成之后再扩充才算过期)即窗口长度 > k,窗口形成过程中不会过期, i - k 表示过期的下标
        if (maxList.peekFirst() == i - k) {
            maxList.pollFirst();
        }
        // 判断下标过期
        if (i >= k - 1) {
            // 当窗口已经形成了,记录每一步的res
            res[index++] = nums[maxList.peekFirst()];
        }
    }
    return res;
}
 
Example 8
Source File: LastSameInTree.java    From coding-interviews with MIT License 5 votes vote down vote up
/**
 * 两个链表前面的结点都是相同的,找到最后一个相同的结点就是最低公共祖先
 */
private Node getLastSameNode(LinkedList<Node> path1, LinkedList<Node> path2) {
    Node lastSameNode = null;
    while (!path1.isEmpty() && !path2.isEmpty()) {
        if (path1.peekFirst() == path2.removeFirst()) {
            lastSameNode = path1.removeFirst();
        } else {
            return lastSameNode;
        }
    }
    return lastSameNode;
}
 
Example 9
Source File: DefaultMemoryEventStore.java    From super-cloudops with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized Partition least() {
	if (!this.records.isEmpty()) {
		// Expired clean.
		this.checkExpiredSampleClean();

		LinkedList<Partition> records0 = this.getASCRecords();
		Partition part = records0.peekFirst();
		if (part != null) {
			part.setSamples(records0.size());
			return part;
		}
	}
	return null;
}
 
Example 10
Source File: CommandInjection.java    From Android_Code_Arbiter with GNU Lesser General Public License v3.0 5 votes vote down vote up
private String transferThroughList(String in, int index) {
    LinkedList<String> list = new LinkedList<String>();
    list.add(System.getenv("")); // taints the list
    list.clear(); // makes the list safe again
    list.add(1, "xx");
    list.addFirst(in); // can taint the list
    list.addLast("yy");
    list.push(in);
    return list.element() + list.get(index) + list.getFirst() + list.getLast()
            + list.peek() + list.peekFirst() + list.peekLast() + list.poll()
            + list.pollFirst() + list.pollLast() + list.pop() + list.remove()
            + list.remove(index) + list.removeFirst() + list.removeLast()
            + list.set(index, "safe") + list.toString();
}
 
Example 11
Source File: TreeLineRenderer.java    From text-ui with GNU General Public License v3.0 4 votes vote down vote up
@Override
public LineReader reader(final int width) {


  final LinkedList<LineReader> readers  = new LinkedList<LineReader>();
  for (LineRenderer child : children) {
    readers.addLast(child.reader(width - 2));
  }

  //
  return new LineReader() {

    /** . */
    LineReader value = TreeLineRenderer.this.value != null ? TreeLineRenderer.this.value.reader(width) : null;

    /** . */
    boolean node = true;

    public boolean hasLine() {
      if (value != null) {
        if (value.hasLine()) {
          return true;
        } else {
          value = null;
        }
      }
      while (readers.size() > 0) {
        if (readers.peekFirst().hasLine()) {
          return true;
        } else {
          readers.removeFirst();
          node = true;
        }
      }
      return false;
    }

    public void renderLine(RenderAppendable to) {
      if (value != null) {
        if (value.hasLine()) {
          value.renderLine(to);
        } else {
          value = null;
        }
      }
      if (value == null) {
        while (readers.size() > 0) {
          LineReader first = readers.peekFirst();
          if (first.hasLine()) {
            if (node) {
              to.append("+-");
              node = false;
            } else {
              Iterator<LineReader> i = readers.descendingIterator();
              boolean rest = false;
              while (i.hasNext()) {
                LineReader renderer = i.next();
                if (i.hasNext()) {
                  if (renderer.hasLine()) {
                    rest = true;
                    break;
                  }
                }
              }
              if (rest) {
                to.append("| ");
              } else {
                to.append("  ");
              }
            }
            first.renderLine(to);
            break;
          }
        }
      }
    }
  };
}