本文整理汇总了Java中java.util.ArrayList.trimToSize方法的典型用法代码示例。如果您正苦于以下问题:Java ArrayList.trimToSize方法的具体用法?Java ArrayList.trimToSize怎么用?Java ArrayList.trimToSize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.ArrayList
的用法示例。
在下文中一共展示了ArrayList.trimToSize方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: leastOf
import java.util.ArrayList; //导入方法依赖的package包/类
/**
* Returns the {@code k} least elements from the given iterator according to this ordering, in
* order from least to greatest. If there are fewer than {@code k} elements present, all will be
* included.
*
* <p>The implementation does not necessarily use a <i>stable</i> sorting algorithm; when multiple
* elements are equivalent, it is undefined which will come first.
*
* <p><b>Java 8 users:</b> Continue to use this method for now. After the next release of Guava,
* use {@code Streams.stream(iterator).collect(Comparators.least(k, thisComparator))} instead.
*
* @return an immutable {@code RandomAccess} list of the {@code k} least elements in ascending
* order
* @throws IllegalArgumentException if {@code k} is negative
* @since 14.0
*/
public <E extends T> List<E> leastOf(Iterator<E> iterator, int k) {
checkNotNull(iterator);
checkNonnegative(k, "k");
if (k == 0 || !iterator.hasNext()) {
return ImmutableList.of();
} else if (k >= Integer.MAX_VALUE / 2) {
// k is really large; just do a straightforward sorted-copy-and-sublist
ArrayList<E> list = Lists.newArrayList(iterator);
Collections.sort(list, this);
if (list.size() > k) {
list.subList(k, list.size()).clear();
}
list.trimToSize();
return Collections.unmodifiableList(list);
} else {
TopKSelector<E> selector = TopKSelector.least(k, this);
selector.offerAll(iterator);
return selector.topK();
}
}
示例2: filter
import java.util.ArrayList; //导入方法依赖的package包/类
@Override
public Entry filter(Entry entry) {
NavigableMap<byte[], Integer> scopes = entry.getKey().getScopes();
if (scopes == null || scopes.isEmpty()) {
return null;
}
ArrayList<Cell> cells = entry.getEdit().getCells();
int size = cells.size();
for (int i = size - 1; i >= 0; i--) {
Cell cell = cells.get(i);
// The scope will be null or empty if
// there's nothing to replicate in that WALEdit
if (!scopes.containsKey(cell.getFamily())
|| scopes.get(cell.getFamily()) == HConstants.REPLICATION_SCOPE_LOCAL) {
cells.remove(i);
}
}
if (cells.size() < size / 2) {
cells.trimToSize();
}
return entry;
}
示例3: trimToSize
import java.util.ArrayList; //导入方法依赖的package包/类
public void trimToSize() {
if (attributes == null) {
return;
}
if (attributes.isEmpty()) {
attributes = null;
return;
}
if (attributes instanceof ArrayList) {
ArrayList<Attribute> al = (ArrayList<Attribute>)attributes;
al.trimToSize();
boolean allCanon = true;
for (Attribute a : al) {
if (!a.isCanonical()) {
allCanon = false;
}
if (a.fixups != null) {
assert(!a.isCanonical());
a.fixups = Fixups.trimToSize(a.fixups);
}
}
if (allCanon) {
// Replace private writable attribute list
// with only trivial entries by public unique
// immutable attribute list with the same entries.
attributes = getCanonList(al);
}
}
}
示例4: concatPhases
import java.util.ArrayList; //导入方法依赖的package包/类
private static List<CompilationPhase> concatPhases(final CompilationPhases[] bases) {
final ArrayList<CompilationPhase> l = new ArrayList<>();
for(final CompilationPhases base: bases) {
l.addAll(base.phases);
}
l.trimToSize();
return l;
}
示例5: trimToSize
import java.util.ArrayList; //导入方法依赖的package包/类
/**
* Reduces the memory used by this {@code ArrayListMultimap}, if feasible.
*/
public void trimToSize() {
for (Collection<V> collection : backingMap().values()) {
ArrayList<V> arrayList = (ArrayList<V>) collection;
arrayList.trimToSize();
}
}
示例6: filter
import java.util.ArrayList; //导入方法依赖的package包/类
public static <T> List<T> filter(List<T> list, ListUtilsHook<T> hook) {
ArrayList<T> r = new ArrayList<T>();
for (T t : list) {
if (hook.Result(t)) {
r.add(t);
}
}
r.trimToSize();
return r;
}
示例7: getContextInjectionFields
import java.util.ArrayList; //导入方法依赖的package包/类
/**
* Get {@link ViewContext} annotated fields of class and its superclasses
* @param cls Class to inspect
* @return ViewContext annotated fields
* @throws ViewConfigurationException Invalid Context injection type
*/
public static Collection<ViewContextField> getContextInjectionFields(Class<?> cls)
throws ViewConfigurationException {
ArrayList<ViewContextField> fields = new ArrayList<>();
Class<?> currentClass = cls;
while (currentClass != null) {
final Field[] declaredFields = currentClass.getDeclaredFields();
for (final Field field : declaredFields) {
if (field.isAnnotationPresent(ViewContext.class)) {
// check not final
if (Modifier.isFinal(field.getModifiers())) {
throw new ViewConfigurationException("Context injection field " + field.getName()
+ " must not be declared as final in class " + cls.getName());
}
ViewContext vc = field.getAnnotation(ViewContext.class);
fields.add(
ViewContextField.build(AnnotationUtils.getStringValue(vc.value()), vc.required(), field));
}
}
currentClass = currentClass.getSuperclass();
}
fields.trimToSize();
return fields;
}
示例8: readColumnInfoViaReflection
import java.util.ArrayList; //导入方法依赖的package包/类
private ColumnMapping readColumnInfoViaReflection(Class<?> clazz) {
Field[] fieldArray = clazz.getDeclaredFields();
ArrayList<ColumnInfo> list = new ArrayList<>(fieldArray.length);
ColumnInfo rowNumberColumn = null;
for (Field field: fieldArray) {
RowNumber rowNumberAnnotation = field.getAnnotation(RowNumber.class);
if (! Objects.isNull(rowNumberAnnotation)) {
rowNumberColumn = new ColumnInfo("__id__", field.getName(), -1, null,Integer.class, NoopConverter.class);
continue;
}
Column annotation = field.getAnnotation(Column.class);
if (! Objects.isNull(annotation)) {
Class<?> converter = annotation.convertorClass();
list.add(new ColumnInfo(annotation.name().trim(),
field.getName(),
annotation.index(),
annotation.dataFormat(),
field.getType(),
converter));
}
}
if (list.isEmpty()) {
throw new ZeroCellException(String.format("Class %s does not have @Column annotations", clazz.getName()));
}
list.trimToSize();
return new ColumnMapping(rowNumberColumn, list);
}
示例9: addEquivalents
import java.util.ArrayList; //导入方法依赖的package包/类
/**
* Replaces all isomorphic clusters in the given list by a single cluster
* with weight given by their sum. The resulting list is also sorted by #
* of connections. Both the list and the the weights of the clusters it
* contains may be altered by this method.
*/
public static void addEquivalents(ArrayList<ClusterDiagram> list) {
if (list.size() < 2) {
return;
}
reduce(list);
ClusterDiagram cluster1 = null;
for (int i=0; i<list.size(); i++) {
cluster1 = list.get(i);
for (int j=i+1; j<list.size(); j++) {
ClusterDiagram cluster2 = list.get(j);
if (cluster1.getNumConnections() != cluster2.getNumConnections()) {
break;
}
if(cluster2.getWeight().numerator() == 0) {
continue;
}
if(cluster1.isIsomorphOf(cluster2)) {
int[] score1 = new int[cluster1.mNumBody/2+1];
cluster1.calcScore(score1);
if(cluster2.scoreGreaterThan(score1)) {
cluster2.setWeight(cluster1.getWeight().plus(cluster2.getWeight()));
cluster1.setWeight(Rational.ZERO);
break;
}
cluster1.setWeight(cluster1.getWeight().plus(cluster2.getWeight()));
cluster2.setWeight(Rational.ZERO);
}
}
}
//remove clusters having zero weight
for (int i=0; i<list.size(); i++) {
ClusterDiagram cluster = list.get(i);
if(cluster.getWeight().numerator() == 0) {
list.remove(i);
i--;
}
}
list.trimToSize();
}
示例10: optimizeList
import java.util.ArrayList; //导入方法依赖的package包/类
private static <T> List<T> optimizeList(final ArrayList<T> list) {
switch(list.size()) {
case 0: {
return Collections.emptyList();
}
case 1: {
return Collections.singletonList(list.get(0));
}
default: {
list.trimToSize();
return list;
}
}
}
示例11: filterDefinitions
import java.util.ArrayList; //导入方法依赖的package包/类
/**
* Utility function for returning the subset of the availableDefinitions in availableDefinitions
* order, with categories named in the allowedDefinitionNames. If availableDefinitions is
* empty, no filtering is performed.
* @param allowedDefinitionNames
* @param availableDefinitions
* @return
*/
protected static List<SuiteDefinition> filterDefinitions(
Set<String> allowedDefinitionNames, List<SuiteDefinition> availableDefinitions)
{
if (allowedDefinitionNames.isEmpty())
{
return availableDefinitions;
}
else
{
ArrayList<SuiteDefinition> definitions = new ArrayList<SuiteDefinition>();
for (String category : allowedDefinitionNames)
{
SuiteDefinition allowedDefinition = _getSuitedDefinitionByCategory(availableDefinitions, category);
if (allowedDefinition != null)
{
definitions.add(allowedDefinition);
}
else
{
_LOG.warning("Unabled to find test category named:" + category);
}
}
definitions.trimToSize();
return Collections.unmodifiableList(definitions);
}
}
示例12: getPath
import java.util.ArrayList; //导入方法依赖的package包/类
private List getPath (String pathName) {
String cp = saVM.getSystemProperty(pathName);
String pathSep = saVM.getSystemProperty("path.separator");
ArrayList al = new ArrayList();
StringTokenizer st = new StringTokenizer(cp, pathSep);
while (st.hasMoreTokens()) {
al.add(st.nextToken());
}
al.trimToSize();
return al;
}
示例13: create
import java.util.ArrayList; //导入方法依赖的package包/类
@Override
public Object create() {
ArrayList list = new ArrayList();
for (int i = 0; i < 1000; i++) {
Integer x = new Integer(i);
list.add(x);
}
list.trimToSize();
return list;
}
示例14: getHeaderFields
import java.util.ArrayList; //导入方法依赖的package包/类
/**
* Returns an unmodifiable Map of the header fields.
*/
@Override
public Map<String,List<String>> getHeaderFields() {
if (!connected) {
// Try to connect (silently)
try {
connect();
} catch (IOException e) {
//Ignore
}
}
if (attributes == null)
return (Collections.emptyMap());
HashMap<String,List<String>> headerFields =
new HashMap<String,List<String>>(attributes.size());
NamingEnumeration<String> attributeEnum = attributes.getIDs();
try {
while (attributeEnum.hasMore()) {
String attributeID = attributeEnum.next();
Attribute attribute = attributes.get(attributeID);
if (attribute == null) continue;
ArrayList<String> attributeValueList =
new ArrayList<String>(attribute.size());
NamingEnumeration<?> attributeValues = attribute.getAll();
while (attributeValues.hasMore()) {
Object attrValue = attributeValues.next();
attributeValueList.add(getHeaderValueAsString(attrValue));
}
attributeValueList.trimToSize(); // should be a no-op if attribute.size() didn't lie
headerFields.put(attributeID, Collections.unmodifiableList(attributeValueList));
}
} catch (NamingException ne) {
// Shouldn't happen
}
return Collections.unmodifiableMap(headerFields);
}
示例15: buildAndValidateAcl
import java.util.ArrayList; //导入方法依赖的package包/类
/**
* Builds the final list of ACL entries to return by trimming, sorting and
* validating the ACL entries that have been added.
*
* @param aclBuilder ArrayList<AclEntry> containing entries to build
* @return List<AclEntry> unmodifiable, sorted list of ACL entries
* @throws AclException if validation fails
*/
private static List<AclEntry> buildAndValidateAcl(
ArrayList<AclEntry> aclBuilder) throws AclException {
if (aclBuilder.size() > MAX_ENTRIES) {
throw new AclException("Invalid ACL: ACL has " + aclBuilder.size() +
" entries, which exceeds maximum of " + MAX_ENTRIES + ".");
}
aclBuilder.trimToSize();
Collections.sort(aclBuilder, ACL_ENTRY_COMPARATOR);
// Full iteration to check for duplicates and invalid named entries.
AclEntry prevEntry = null;
for (AclEntry entry: aclBuilder) {
if (prevEntry != null &&
ACL_ENTRY_COMPARATOR.compare(prevEntry, entry) == 0) {
throw new AclException(
"Invalid ACL: multiple entries with same scope, type and name.");
}
if (entry.getName() != null && (entry.getType() == MASK ||
entry.getType() == OTHER)) {
throw new AclException(
"Invalid ACL: this entry type must not have a name: " + entry + ".");
}
prevEntry = entry;
}
// Search for the required base access entries. If there is a default ACL,
// then do the same check on the default entries.
ScopedAclEntries scopedEntries = new ScopedAclEntries(aclBuilder);
for (AclEntryType type: EnumSet.of(USER, GROUP, OTHER)) {
AclEntry accessEntryKey = new AclEntry.Builder().setScope(ACCESS)
.setType(type).build();
if (Collections.binarySearch(scopedEntries.getAccessEntries(),
accessEntryKey, ACL_ENTRY_COMPARATOR) < 0) {
throw new AclException(
"Invalid ACL: the user, group and other entries are required.");
}
if (!scopedEntries.getDefaultEntries().isEmpty()) {
AclEntry defaultEntryKey = new AclEntry.Builder().setScope(DEFAULT)
.setType(type).build();
if (Collections.binarySearch(scopedEntries.getDefaultEntries(),
defaultEntryKey, ACL_ENTRY_COMPARATOR) < 0) {
throw new AclException(
"Invalid default ACL: the user, group and other entries are required.");
}
}
}
return Collections.unmodifiableList(aclBuilder);
}