本文整理汇总了Java中java.util.Collections.nCopies方法的典型用法代码示例。如果您正苦于以下问题:Java Collections.nCopies方法的具体用法?Java Collections.nCopies怎么用?Java Collections.nCopies使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.Collections
的用法示例。
在下文中一共展示了Collections.nCopies方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setUp
import java.util.Collections; //导入方法依赖的package包/类
@Override
protected void setUp() throws Exception {
super.setUp();
clearWorkDir();
jar = new File(getWorkDir(), "x.jar");
JarOutputStream os = new JarOutputStream(
new FileOutputStream(jar)
);
os.putNextEntry(new ZipEntry("fldr/plain.txt"));
os.write("Ahoj\n".getBytes());
os.closeEntry();
os.close();
JarClassLoader registerJarSource = new JarClassLoader(
Collections.nCopies(1, jar),
new ClassLoader[] { getClass().getClassLoader() }
);
assertNotNull("Registered", registerJarSource);
}
示例2: testMetaInfServicesPackageNotListedByDefault
import java.util.Collections; //导入方法依赖的package包/类
public void testMetaInfServicesPackageNotListedByDefault() throws Exception {
File jar = new File(getWorkDir(), "non-empty.jar");
TestFileUtils.writeZipFile(jar,
"META-INF/services/java.lang.Runnable:1"
);
JarClassLoader l1 = new JarClassLoader(Collections.nCopies(1, jar), new ClassLoader[] {
ClassLoader.getSystemClassLoader()
});
Set<ProxyClassLoader> set = ProxyClassPackages.findCoveredPkg("META-INF.services");
assertNull("No JAR covers META-INF.services: " + set, set);
ProxyClassLoader l2 = new ProxyClassLoader(new ProxyClassLoader[] { l1 }, true);
Enumeration<URL> en = l2.getResources("META-INF/services/java.lang.Runnable");
assertTrue("Some are there", en.hasMoreElements());
URL one = en.nextElement();
assertFalse("And that is all", en.hasMoreElements());
URL alternative = l2.getResource("META-INF/services/java.lang.Runnable");
assertEquals("Same URL", one, alternative);
ProxyClassPackages.removeCoveredPakcages(l1);
}
示例3: testLazyVisDestroy
import java.util.Collections; //导入方法依赖的package包/类
public void testLazyVisDestroy() throws Exception {
LazyChildren lch = new LazyChildren();
AbstractNode a = new AbstractNode(lch);
List<String> arr = Collections.nCopies(100, "A");
lch.keys(arr.toArray(new String[0]));
TreeNode ta = Visualizer.findVisualizer(a);
final TreeNode snd = ta.getChildAt(2);
Reference<Node> sndNode = new WeakReference<Node>(Visualizer.findNode(snd));
assertEquals("Child check", "A", snd.toString());
assertEquals("Counter should be 1", 1, lch.cnt);
a.destroy();
assertGC("Whole subtree under a can be GCed now", sndNode);
}
示例4: sort
import java.util.Collections; //导入方法依赖的package包/类
@Override
@NonNull
public List<DataObject> sort(@NonNull final List<DataObject> original) {
if (!isLibrary()) {
return original;
}
final List<DataObject> result = new ArrayList<>(Collections.<DataObject>nCopies(CAT_MAP.size(), null));
for (DataObject dobj : original) {
final String name = dobj.getName();
final Integer index = CAT_MAP.get(name);
if (index == null) {
result.add(dobj);
} else {
result.set (index, dobj);
}
}
return filterNulls(result);
}
示例5: toStringWithSmallCollections
import java.util.Collections; //导入方法依赖的package包/类
@Test
public void toStringWithSmallCollections() {
List<String> strings = Collections.nCopies(1, "0");
List<Integer> ints = Collections.nCopies(1, 1);
List<Long> longs = Collections.nCopies(1, 2L);
List<Double> doubles = Collections.nCopies(1, 3.1D);
List<Float> floats = Collections.nCopies(1, 4.2F);
List<Byte> bytes = Collections.nCopies(1, (byte)5);
List<Character> chars = Collections.nCopies(1, '6');
assertThat(LogHelper.toString(strings), is("[0]"));
assertThat(LogHelper.toString(ints), is("[1]"));
assertThat(LogHelper.toString(longs), is("[2]"));
assertThat(LogHelper.toString(doubles), is("[3.1]"));
assertThat(LogHelper.toString(floats), is("[4.2]"));
assertThat(LogHelper.toString(bytes), is("[5]"));
assertThat(LogHelper.toString(chars), is("[6]"));
}
示例6: main
import java.util.Collections; //导入方法依赖的package包/类
public static void main(String[] args) {
/*
To create a List containing n copies of specified Object use,
static List nCopies(int n, Object obj) method of Java Collections class.
This method returns immutable List containing n copies of the
specified Object.
*/
List list = Collections.nCopies(5, "A");
//iterate through newly created list
System.out.println("List contains, ");
Iterator itr = list.iterator();
while (itr.hasNext()) System.out.println(itr.next());
}
示例7: testLarge
import java.util.Collections; //导入方法依赖的package包/类
@Test
public void testLarge() throws Exception {
Message m = new Message();
// list size 80 MB
m.l = new ArrayList<Integer>(Collections.nCopies(20 * 1024 * 1024, 1000000007));
Message m2 = marshaller.parse(marshaller.stream(m));
assertNotSame(m, m2);
assertEquals(m.l.size(), m2.l.size());
}
示例8: testConstructor_collectionTooLarge
import java.util.Collections; //导入方法依赖的package包/类
/**
* Initializing from too large collection throws IllegalArgumentException
*/
public void testConstructor_collectionTooLarge() {
// just barely fits - succeeds
new ArrayBlockingQueue(SIZE, false,
Collections.nCopies(SIZE, ""));
try {
new ArrayBlockingQueue(SIZE - 1, false,
Collections.nCopies(SIZE, ""));
shouldThrow();
} catch (IllegalArgumentException success) {}
}
示例9: parseParameters
import java.util.Collections; //导入方法依赖的package包/类
/**
* Attempt to parse the given object as a set of parameters for this handler.
*
* @param params the parameter block to parse
* @return true if the object made sense for this handler; false otherwise.
*/
@Override
public boolean parseParameters(Object params)
{
if (params == null || !(params instanceof BuildBattleDecorator))
return false;
this.params = (BuildBattleDecorator) params;
this.sourceBounds = this.params.getGoalStructureBounds();
this.destBounds = this.params.getPlayerStructureBounds();
this.delta = new Vec3i(destBounds.getMin().getX() - sourceBounds.getMin().getX(),
destBounds.getMin().getY() - sourceBounds.getMin().getY(),
destBounds.getMin().getZ() - sourceBounds.getMin().getZ());
this.structureVolume = volumeOfBounds(this.sourceBounds);
assert(this.structureVolume == volumeOfBounds(this.destBounds));
this.dest = new ArrayList<IBlockState>(Collections.nCopies(this.structureVolume, (IBlockState)null));
this.source = new ArrayList<IBlockState>(Collections.nCopies(this.structureVolume, (IBlockState)null));
DrawBlockBasedObjectType tickBlock = this.params.getBlockTypeOnCorrectPlacement();
DrawBlockBasedObjectType crossBlock = this.params.getBlockTypeOnIncorrectPlacement();
this.blockTypeOnCorrectPlacement = (tickBlock != null) ? new XMLBlockState(tickBlock.getType(), tickBlock.getColour(), tickBlock.getFace(), tickBlock.getVariant()) : null;
this.blockTypeOnIncorrectPlacement = (crossBlock != null) ? new XMLBlockState(crossBlock.getType(), crossBlock.getColour(), crossBlock.getFace(), crossBlock.getVariant()) : null;
return true;
}
示例10: MH_hashArguments
import java.util.Collections; //导入方法依赖的package包/类
static MethodHandle MH_hashArguments(Class<? extends Object[]> arrayClass, int arity) {
if (arrayClass == Object[].class)
return MH_hashArguments(arity);
ArrayList<Class<?>> ptypes = new ArrayList<>(Collections.<Class<?>>nCopies(arity, arrayClass.getComponentType()));
MethodType mt = MethodType.methodType(Object.class, ptypes);
return MH_hashArguments_VA.asType(mt);
}
示例11: reallyLongCashFlow
import java.util.Collections; //导入方法依赖的package包/类
@Test
public void reallyLongCashFlow() {
int size = 1000000;
TimeSeries sampleTimeSeries = new MockTimeSeries(Collections.nCopies(size, (Tick) new MockTick(10)));
TradingRecord tradingRecord = new BaseTradingRecord(Order.buyAt(0), Order.sellAt(size - 1));
CashFlow cashFlow = new CashFlow(sampleTimeSeries, tradingRecord);
assertDecimalEquals(cashFlow.getValue(size - 1), 1);
}
示例12: concurrentVerify
import java.util.Collections; //导入方法依赖的package包/类
@SuppressWarnings("Convert2Lambda")
private void concurrentVerify(final JWT jwt, final String token) throws TimeoutException, InterruptedException {
final Waiter waiter = new Waiter();
List<VerifyTask> tasks = Collections.nCopies(REPEAT_COUNT, new VerifyTask(waiter, jwt, token));
executor.invokeAll(tasks, TIMEOUT, TimeUnit.MILLISECONDS);
waiter.await(TIMEOUT, REPEAT_COUNT);
}
示例13: search
import java.util.Collections; //导入方法依赖的package包/类
/**
* Searches the tokens in the words given taking into account their relative positioning.
*
* @param words words to search from
*/
private void search(List<String> words) {
int minDistance = Integer.MAX_VALUE, startTokenIndex = 0;
while (startTokenIndex < words.size()) {
List<Integer> tokenIndexes = new ArrayList<>(Collections.nCopies(tokens.size(), -1));
int subListTokenIndex = words.subList(startTokenIndex, words.size()).indexOf(tokens.get(0));
int newTokenIndex = subListTokenIndex + startTokenIndex;
tokenIndexes.set(0, newTokenIndex);
if (subListTokenIndex == -1) {
break;
}
for (int i = 1; i < tokens.size(); ++i) {
subListTokenIndex = words.subList(newTokenIndex + 1, words.size()).indexOf(tokens.get(i));
if (subListTokenIndex == -1) {
break;
}
newTokenIndex = subListTokenIndex + newTokenIndex + 1;
tokenIndexes.set(i, newTokenIndex);
}
if (subListTokenIndex == -1) {
break;
}
int minDistanceCandidate = tokenIndexes.get(tokenIndexes.size() - 1) - tokenIndexes.get(0);
if (minDistanceCandidate < minDistance) {
minDistance = minDistanceCandidate;
result.tokenIndexes = tokenIndexes;
}
startTokenIndex = tokenIndexes.get(0) + 1;
}
}
示例14: Statistics
import java.util.Collections; //导入方法依赖的package包/类
public Statistics() {
mMonthlyData = new ArrayList<>(Collections.nCopies(12, 0));
mCategoryData = new SparseIntArray();
}
示例15: CircularArrayList
import java.util.Collections; //导入方法依赖的package包/类
public CircularArrayList(int capacity) {
n = capacity + 1;
buf = new ArrayList<E>(Collections.nCopies(n, (E) null));
}