canonical example by Tabnine
private void mappingWordsLength(List<String> wordsList) { Map<Integer, Set<String>> mapping = new HashMap<>(); for (String word : wordsList) { mapping.computeIfAbsent(word.length(), HashSet::new).add(word); } List<Integer> lengths = new LinkedList<>(mapping.keySet()); Collections.sort(lengths); lengths.forEach(n -> System.out.println(mapping.get(n).size() + " words with " + n + " chars")); }
@Override public List<FieldError> getFieldErrors() { List<FieldError> result = new LinkedList<>(); for (ObjectError objectError : this.errors) { if (objectError instanceof FieldError) { result.add((FieldError) objectError); } } return Collections.unmodifiableList(result); }
public void appendReferring(Referring currentReferring) { if (_referringProperties == null) { _referringProperties = new LinkedList<Referring>(); } _referringProperties.add(currentReferring); }
// Collection of items to process in parallel Collection<Integer> elems = new LinkedList<Integer>(); for (int i = 0; i < 40; ++i) { elems.add(i); } Parallel.For(elems, // The operation to perform with each item new Parallel.Operation<Integer>() { public void perform(Integer param) { System.out.println(param); }; });
public static Map bucket(Collection c, Transformer t) { Map buckets = new HashMap(); for (Iterator it = c.iterator(); it.hasNext();) { Object value = (Object)it.next(); Object key = t.transform(value); List bucket = (List)buckets.get(key); if (bucket == null) { buckets.put(key, bucket = new LinkedList()); } bucket.add(value); } return buckets; }
public static List<BlockNode> buildSimplePath(BlockNode block) { List<BlockNode> list = new LinkedList<>(); BlockNode currentBlock = block; while (currentBlock != null && currentBlock.getCleanSuccessors().size() < 2 && currentBlock.getPredecessors().size() == 1) { list.add(currentBlock); currentBlock = getNextBlock(currentBlock); } if (list.isEmpty()) { return Collections.emptyList(); } return list; }
private List<ShardingExecuteGroup<StatementExecuteUnit>> getSQLExecuteGroups( final String dataSourceName, final List<SQLUnit> sqlUnits, final SQLExecutePrepareCallback callback) throws SQLException { List<ShardingExecuteGroup<StatementExecuteUnit>> result = new LinkedList<>(); int desiredPartitionSize = Math.max(0 == sqlUnits.size() % maxConnectionsSizePerQuery ? sqlUnits.size() / maxConnectionsSizePerQuery : sqlUnits.size() / maxConnectionsSizePerQuery + 1, 1); List<List<SQLUnit>> sqlUnitPartitions = Lists.partition(sqlUnits, desiredPartitionSize); ConnectionMode connectionMode = maxConnectionsSizePerQuery < sqlUnits.size() ? ConnectionMode.CONNECTION_STRICTLY : ConnectionMode.MEMORY_STRICTLY; List<Connection> connections = callback.getConnections(connectionMode, dataSourceName, sqlUnitPartitions.size()); int count = 0; for (List<SQLUnit> each : sqlUnitPartitions) { result.add(getSQLExecuteGroup(connectionMode, connections.get(count++), dataSourceName, each, callback)); } return result; }
/** * Return a FlashMap contained in the given list that matches the request. * @return a matching FlashMap or {@code null} */ @Nullable private FlashMap getMatchingFlashMap(List<FlashMap> allMaps, HttpServletRequest request) { List<FlashMap> result = new LinkedList<>(); for (FlashMap flashMap : allMaps) { if (isFlashMapForRequest(flashMap, request)) { result.add(flashMap); } } if (!result.isEmpty()) { Collections.sort(result); if (logger.isTraceEnabled()) { logger.trace("Found " + result.get(0)); } return result.get(0); } return null; }
@Test public void testComplexGenericMap() { Map<List<String>, List<String>> inputMap = new HashMap<>(); List<String> inputKey = new LinkedList<>(); inputKey.add("1"); List<String> inputValue = new LinkedList<>(); inputValue.add("10"); inputMap.put(inputKey, inputValue); ComplexMapHolder holder = new ComplexMapHolder(); BeanWrapper bw = new BeanWrapperImpl(holder); bw.setPropertyValue("genericMap", inputMap); assertEquals(new Integer(1), holder.getGenericMap().keySet().iterator().next().get(0)); assertEquals(new Long(10), holder.getGenericMap().values().iterator().next().get(0)); }
public List<Integer> splitRedPacket(int money, int count) { List<Integer> moneys = new LinkedList<>(); //金额检查,如果最大红包 * 个数 < 总金额;则需要调大最小红包 MAX_MONEY if (MAX_MONEY * count <= money) { System.err.println("请调大最小红包金额 MAX_MONEY=[" + MAX_MONEY + "]"); return moneys ; } //计算出最大红包 int max = (int) ((money / count) * TIMES); max = max > MAX_MONEY ? MAX_MONEY : max; for (int i = 0; i < count; i++) { //随机获取红包 int redPacket = randomRedPacket(money, MIN_MONEY, max, count - i); moneys.add(redPacket); //总金额每次减少 money -= redPacket; } return moneys; }
int length = roleTagList.size() - 1; List<E> tagList = new LinkedList<E>(); Iterator<EnumItem<E>> iterator = roleTagList.iterator(); EnumItem<E> start = iterator.next(); E pre = start.labelMap.entrySet().iterator().next().getKey(); E perfect_tag = pre; tagList.add(pre); for (int i = 0; i < length; ++i) EnumItem<E> item = iterator.next(); for (E cur : item.labelMap.keySet()) tagList.add(pre);
// with two levels of indirection List<ArrayList> alist = new ArrayList<ArrayList>(); List<List> list = (List) alist; // gives you a warning. list.add(new LinkedList()); // adding a LinkedList into a list of ArrayList!! System.out.println(alist.get(0)); // runtime error
private CommandResponsePackets executeForMasterSlave() throws InterruptedException, ExecutionException, TimeoutException { String dataSourceName = new MasterSlaveRouter(((MasterSlaveSchema) logicSchema).getMasterSlaveRule(), GLOBAL_REGISTRY.getShardingProperties().<Boolean>getValue(ShardingPropertiesConstant.SQL_SHOW)).route(sql).iterator().next(); synchronizedFuture = new SynchronizedFuture(1); FutureRegistry.getInstance().put(connectionId, synchronizedFuture); executeSQL(dataSourceName, sql); List<QueryResult> queryResults = synchronizedFuture.get( GLOBAL_REGISTRY.getShardingProperties().<Long>getValue(ShardingPropertiesConstant.PROXY_BACKEND_CONNECTION_TIMEOUT_SECONDS), TimeUnit.SECONDS); FutureRegistry.getInstance().delete(connectionId); List<CommandResponsePackets> packets = new LinkedList<>(); for (QueryResult each : queryResults) { packets.add(((MySQLQueryResult) each).getCommandResponsePackets()); } return merge(new SQLJudgeEngine(sql).judge(), packets, queryResults); }
/** * Enumerate the simple paths. * * @return this object */ public SimplePathEnumerator enumerate() { Iterator<Edge> entryOut = cfg.outgoingEdgeIterator(cfg.getEntry()); if (!entryOut.hasNext()) { throw new IllegalStateException(); } Edge entryEdge = entryOut.next(); LinkedList<Edge> init = new LinkedList<>(); init.add(entryEdge); work(init); if (DEBUG && work == maxWork) { System.out.println("**** Reached max work! ****"); } return this; }
import java.util.LinkedList; class Test { public static void main(String args[]) { char arr[] = {3,1,4,1,5,9,2,6,5,3,5,8,9}; LinkedList<Integer> fifo = new LinkedList<Integer>(); for (int i = 0; i < arr.length; i++) fifo.add (new Integer (arr[i])); System.out.print (fifo.removeFirst() + "."); while (! fifo.isEmpty()) System.out.print (fifo.removeFirst()); System.out.println(); } }
private void doTestMultipartHttpServletRequest(MultipartHttpServletRequest request) throws IOException { Set<String> fileNames = new HashSet<>(); Iterator<String> fileIter = request.getFileNames(); while (fileIter.hasNext()) { fileNames.add(fileIter.next()); } assertEquals(2, fileNames.size()); assertTrue(fileNames.contains("file1")); assertTrue(fileNames.contains("file2")); MultipartFile file1 = request.getFile("file1"); MultipartFile file2 = request.getFile("file2"); Map<String, MultipartFile> fileMap = request.getFileMap(); List<String> fileMapKeys = new LinkedList<>(fileMap.keySet()); assertEquals(2, fileMapKeys.size()); assertEquals(file1, fileMap.get("file1")); assertEquals(file2, fileMap.get("file2")); assertEquals("file1", file1.getName()); assertEquals("", file1.getOriginalFilename()); assertNull(file1.getContentType()); assertTrue(ObjectUtils.nullSafeEquals("myContent1".getBytes(), file1.getBytes())); assertTrue(ObjectUtils.nullSafeEquals("myContent1".getBytes(), FileCopyUtils.copyToByteArray(file1.getInputStream()))); assertEquals("file2", file2.getName()); assertEquals("myOrigFilename", file2.getOriginalFilename()); assertEquals("text/plain", file2.getContentType()); assertTrue(ObjectUtils.nullSafeEquals("myContent2".getBytes(), file2.getBytes())); assertTrue(ObjectUtils.nullSafeEquals("myContent2".getBytes(), FileCopyUtils.copyToByteArray(file2.getInputStream()))); }
public List<T> asSortedList() { LinkedList<T> list = new LinkedList<>(); for (Iterator<T> i = elements.iterator(); i.hasNext();) { list.addFirst(i.next()); } return list; }
public void removeDownloaderCallback(IMapDownloaderCallback callback) { LinkedList<WeakReference<IMapDownloaderCallback>> ncall = new LinkedList<WeakReference<IMapDownloaderCallback>>(callbacks); Iterator<WeakReference<IMapDownloaderCallback>> it = ncall.iterator(); while (it.hasNext()) { IMapDownloaderCallback c = it.next().get(); if (c == callback) { it.remove(); } } callbacks = ncall; }
private <I, O> List<O> serialExecute(final Collection<ShardingExecuteGroup<I>> inputGroups, final ShardingGroupExecuteCallback<I, O> firstCallback, final ShardingGroupExecuteCallback<I, O> callback) throws SQLException { List<O> result = new LinkedList<>(); Iterator<ShardingExecuteGroup<I>> inputGroupsIterator = inputGroups.iterator(); ShardingExecuteGroup<I> firstInputs = inputGroupsIterator.next(); result.addAll(syncGroupExecute(firstInputs, null == firstCallback ? callback : firstCallback)); for (ShardingExecuteGroup<I> each : Lists.newArrayList(inputGroupsIterator)) { result.addAll(syncGroupExecute(each, callback)); } return result; }