List of usage examples for java.util List subList
List<E> subList(int fromIndex, int toIndex);
From source file:com.naver.template.common.ChunkUtils.java
public static <T> List<List<T>> chunk(List<T> list, int chunkSize) { List<List<T>> chunkedList = new ArrayList<List<T>>(); int start = 0; while (start < list.size()) { int end = Math.min(chunkSize + start, list.size()); chunkedList.add(list.subList(start, end)); start = end;/*from w w w . j av a 2 s .c om*/ } return chunkedList; }
From source file:com.cdancy.artifactory.rest.util.ArtifactoryUtils.java
public static GAVCoordinates gavFromURL(URL url, URL endpoint) { GAVCoordinates gavCoordinates = new GAVCoordinates(); String sanitizedURL = getBasePath(url, endpoint.toString()); String[] sanitizedURLParts = sanitizedURL.split("/"); List<String> gavArray = Arrays.asList(Arrays.copyOfRange(sanitizedURLParts, 2, sanitizedURLParts.length)); List<String> groupArray = gavArray.subList(0, gavArray.size() - 3); gavCoordinates.group = collectionToString(groupArray, "."); gavCoordinates.artifact = gavArray.get(groupArray.size()); gavCoordinates.version = gavArray.get(groupArray.size() + 1); return gavCoordinates; }
From source file:com.asual.summer.core.util.StringUtils.java
public static String lines(String str, int length) { List<String> values = Arrays.asList(str.split("\n\r|\r\n|\n|\r")); if (length < values.size()) { return join(values.subList(0, length).toArray(new String[] {}), "\n") + "..."; }/*from ww w .java 2 s . co m*/ return str; }
From source file:Main.java
public static <T> List<List<T>> partition(List<T> list, int partitionSize) { List<List<T>> results = new LinkedList<>(); for (int index = 0; index < list.size(); index += partitionSize) { int subListSize = Math.min(partitionSize, list.size() - index); results.add(list.subList(index, index + subListSize)); }// w w w. java 2s .c o m return results; }
From source file:com.addthis.hydra.kafka.KafkaUtils.java
public static Map<Integer, Node> getSeedKafkaBrokers(CuratorFramework zkClient, int brokerCount) { Map<Integer, Node> brokers = new HashMap<>(); List<Integer> ids = getKafkaBrokerIds(zkClient); Collections.shuffle(ids);//www . j a v a 2 s .co m brokerCount = (brokerCount == -1) ? ids.size() : Math.min(brokerCount, ids.size()); for (int id : ids.subList(0, brokerCount)) { try { byte[] brokerInfo = zkClient.getData().forPath(BROKERS_PATH + '/' + id); brokers.put(id, parseBrokerInfo(id, brokerInfo)); } catch (Exception e) { log.warn("failed to get/parse info for (ignored) broker: {}", id, e); } } return brokers; }
From source file:Main.java
/** * Get a range from a list based on start and count parameters in a safe way. * * @param start The start index/*from www. j a v a 2 s. co m*/ * @param count The number of elements to add * * @return The sublist consisting at most of {@code count} elements (less if the parameters * exceed the size of the list) */ public static <T> List<T> getRange(List<T> list, int start, int count) { if (start >= list.size() || count <= 0) { return new ArrayList<>(); } else if (start < 0) { start = 0; } int end = Math.min(list.size(), start + count); return list.subList(start, end); }
From source file:Main.java
/** * Shift objects in the list, objects will try to move to the furthest distance. * <pre>/*from w w w .j av a2 s.c o m*/ * e.g. ABCED -> Shift AC with offset=5 -> BEDAC * </pre> * * @param list list * @param indexArray object index array * @param offset offset value can be positive or negative */ public static void shiftItemsAsMuchAsPossible(List<?> list, int[] indexArray, int offset) { Arrays.sort(indexArray); if (offset > 0) { int last = list.size(); for (int i = indexArray.length - 1; i >= 0; i--) { last = shiftItem(list.subList(0, last), indexArray[i], offset); } } else { int last = -1; for (int i = 0; i < indexArray.length; i++) { int index = indexArray[i] - (last + 1); last = shiftItem(list.subList(last + 1, list.size()), index, offset); } } }
From source file:eu.ggnet.dwoss.rights.assist.gen.RightsGeneratorOperation.java
private static List<AtomicRight> getRandomRights() { List<AtomicRight> rights = Arrays.asList(AtomicRight.values()); Collections.shuffle(rights);/*w ww . j a va 2 s.c o m*/ int till = (int) (Math.random() * rights.size() - 1) + 1; rights = rights.subList(0, till); return rights; }
From source file:Main.java
public static <T> List<T> safeSubList(List<T> list, int fromIndex, int toIndex) { int size = list.size(); if (fromIndex >= size || toIndex <= 0 || fromIndex >= toIndex) { return Collections.emptyList(); }/*from w w w .j a va 2 s. co m*/ fromIndex = Math.max(0, fromIndex); toIndex = Math.min(size, toIndex); return list.subList(fromIndex, toIndex); }
From source file:de.tudarmstadt.ukp.experiments.argumentation.sequence.feature.discourse.PDTBDiscourseFeatures.java
private static String glueAttributionTokens(DiscourseAttribution attribution) { final int maxTokens = 3; List<String> tokens = new ArrayList<>(); for (Token token : JCasUtil.selectCovered(Token.class, attribution)) { tokens.add(token.getLemma().getCoveredText().toLowerCase()); }//from w w w . ja v a 2 s . co m tokens = tokens.subList(0, tokens.size() > maxTokens ? maxTokens : tokens.size() - 1); return StringUtils.join(tokens, "_"); }