private void loadEntities(InputStream is) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); String line; Set<String> uniqueDisplayNames = new HashSet<String>(); // TODO(dkogan): We're currently dropping entities with the same // name on the floor (e.g. we keep only one rib). Need to come // up with a better scheme so we can cycle through 'dups'. while ((line = reader.readLine()) != null) { String layer = line; int layerId = Layers.fromName(layer); while ((line = reader.readLine()) != null) { if ("".equals(line)) break; // Using substring() instead of split() is about 30% faster. // Explicitly use |new String()| because just substring() makes // the substring a view of the original string, which wastes // memory. int startIndex = 0, endIndex = line.indexOf(FIELD_SEPARATOR); String entityId = new String(line.substring(startIndex, endIndex)); startIndex = endIndex + 1; endIndex = line.indexOf(FIELD_SEPARATOR, startIndex); String entityName = new String(line.substring(startIndex, endIndex)); // Map of entity id to coordinate info (bbox, ctr). EntityInfo entityInfo = parseEntityInfo(line, endIndex + 1); entityInfo.layer = layerId; entityInfo.displayName = entityName; mEntities.put(entityId, entityInfo); // Map of display name to entity id. if (!uniqueDisplayNames.contains(entityName)) { mSearchList.add(entityName); uniqueDisplayNames.add(entityName); mSearchToEntity.put(entityName, new ArrayList<String>()); } mSearchToEntity.get(entityName).add(entityId); } } }