档案日期2016的41

2016年10月10日 - 2016年10月16日

[LeetCode]Battleships in a board

题目描述:

LeetCode 419. Battleships in a board

Given an 2D board, count how many different battleships are in it. The battleships are represented with 'X's, empty slots are represented with '.'s. You may assume the following rules:

  • You ...

继续阅读

Java中怎样遍历HashMap效率最高

Java中遍历HashMap对象中的条目(Entry)时,通常有两种方式:

  1. 通过KeySet遍历
  2. 通过EntrySet遍历

下面的测试分别采用KeySet和EntrySet,通过forEach遍历同一个HashMap对象中的条目。

测试结果表明,方法2通过EntrySet遍历效率优于方法1通过KeySet遍历。

Java代码:

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.junit.BeforeClass;
import org.junit.Test;

public class TestMapTraversal {

    private static int MAXCOUNT = 3000000;
    private static Map<Integer, Integer> map = new HashMap<>(); 
    
    @BeforeClass
    public static void initMap() { ...

继续阅读

每日归档

上周

下周

归档