下面列出了java.util.LinkedHashSet#clear ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void test_forEach() throws Exception {
LinkedHashMap<String, String> map = new LinkedHashMap<>();
map.put("three", "3");
map.put("two", "2");
map.put("one", "1");
LinkedHashMap<String, String> output = new LinkedHashMap<>();
map.forEach((k, v) -> output.put(k,v));
assertEquals(map, output);
LinkedHashSet<String> setOutput = new LinkedHashSet<>();
map.keySet().forEach((k) -> setOutput.add(k));
assertEquals(map.keySet(), setOutput);
setOutput.clear();
map.values().forEach((v) -> setOutput.add(v));
assertEquals(new LinkedHashSet<>(map.values()), setOutput);
LinkedHashSet<Map.Entry<String,String>> entrySetOutput = new LinkedHashSet<>();
map.entrySet().forEach((v) -> entrySetOutput.add(v));
assertEquals(map.entrySet(), entrySetOutput);
}
@Override
public void loadState(FindInProjectSettingsBase state) {
XmlSerializerUtil.copyBean(state, this);
//Avoid duplicates
LinkedHashSet<String> tmp = new LinkedHashSet<>(findStrings);
findStrings.clear();
findStrings.addAll(tmp);
tmp.clear();
tmp.addAll(replaceStrings);
replaceStrings.clear();
replaceStrings.addAll(tmp);
tmp.clear();
tmp.addAll(dirStrings);
dirStrings.clear();
dirStrings.addAll(tmp);
}
static void moveToStart(final LinkedHashSet<Cipher> ciphers, final Collection<Cipher> toBeMovedCiphers) {
List<Cipher> movedCiphers = new ArrayList<>(toBeMovedCiphers);
List<Cipher> originalCiphers = new ArrayList<>(ciphers);
movedCiphers.retainAll(ciphers);
ciphers.clear();
ciphers.addAll(movedCiphers);
ciphers.addAll(originalCiphers);
}
public static void main(String[] args) {
//create object of LinkedHashSet
LinkedHashSet lhashSet = new LinkedHashSet();
//add elements to LinkedHashSet object
lhashSet.add(new Integer("1"));
lhashSet.add(new Integer("2"));
lhashSet.add(new Integer("3"));
System.out.println("LinkedHashSet before removal : " + lhashSet);
/*
To remove all elements from Java LinkedHashSet or to clear LinkedHashSet
object use,
void clear() method.
This method removes all elements from LinkedHashSet.
*/
lhashSet.clear();
System.out.println("LinkedHashSet after removal : " + lhashSet);
/*
To check whether LinkedHashSet contains any elements or not
use
boolean isEmpty() method.
This method returns true if the LinkedHashSet does not contains any elements
otherwise false.
*/
System.out.println("Is LinkedHashSet empty ? " + lhashSet.isEmpty());
/*
Please note that removeAll method of Java LinkedHashSet class can
also be used to remove all elements from LinkedHashSet object.
*/
}
public static void main(String[] args) {
//create object of LinkedHashSet
LinkedHashSet lhashSet = new LinkedHashSet();
//add elements to LinkedHashSet object
lhashSet.add(new Integer("1"));
lhashSet.add(new Integer("2"));
lhashSet.add(new Integer("3"));
System.out.println("LinkedHashSet before removal : " + lhashSet);
/*
To remove all elements from Java LinkedHashSet or to clear LinkedHashSet
object use,
void clear() method.
This method removes all elements from LinkedHashSet.
*/
lhashSet.clear();
System.out.println("LinkedHashSet after removal : " + lhashSet);
/*
To check whether LinkedHashSet contains any elements or not
use
boolean isEmpty() method.
This method returns true if the LinkedHashSet does not contains any elements
otherwise false.
*/
System.out.println("Is LinkedHashSet empty ? " + lhashSet.isEmpty());
/*
Please note that removeAll method of Java LinkedHashSet class can
also be used to remove all elements from LinkedHashSet object.
*/
}
public final void checkGraphLoop(A entity, boolean parents, boolean children) throws ServiceException {
HashSet<Long> checked = new HashSet<Long>();
LinkedHashSet<A> path = new LinkedHashSet<A>();
if (parents) {
checkParents(entity, entity, checked, path);
}
checked.clear();
path.clear();
if (children) {
checkChildren(entity, entity, checked, path);
}
}
public List<CodeLens> getCodeLensSymbols(String uri, IProgressMonitor monitor) {
if (!preferenceManager.getPreferences().isCodeLensEnabled()) {
return Collections.emptyList();
}
final ICompilationUnit unit = JDTUtils.resolveCompilationUnit(uri);
IClassFile classFile = null;
if (unit == null) {
classFile = JDTUtils.resolveClassFile(uri);
if (classFile == null) {
return Collections.emptyList();
}
} else {
if (!unit.getResource().exists() || monitor.isCanceled()) {
return Collections.emptyList();
}
}
try {
ITypeRoot typeRoot = unit != null ? unit : classFile;
IJavaElement[] elements = typeRoot.getChildren();
LinkedHashSet<CodeLens> lenses = new LinkedHashSet<>(elements.length);
collectCodeLenses(typeRoot, elements, lenses, monitor);
if (monitor.isCanceled()) {
lenses.clear();
}
return new ArrayList<>(lenses);
} catch (JavaModelException e) {
JavaLanguageServerPlugin.logException("Problem getting code lenses for" + unit.getElementName(), e);
}
return Collections.emptyList();
}
public LinkedHashSet<Path> scan(FileSystem fs, Path filePath, Set<String> consumedFiles)
{
LinkedHashSet<Path> pathSet;
pathSet = super.scan(fs, filePath, consumedFiles);
TreeSet<Path> orderFiles = new TreeSet<>();
orderFiles.addAll(pathSet);
pathSet.clear();
Iterator<Path> fileIterator = orderFiles.iterator();
while (fileIterator.hasNext()) {
pathSet.add(fileIterator.next());
}
return pathSet;
}
@Override
public void process(LinkedHashSet<T> impl) {
impl.clear();
}
@Test
public void testMessageDeleiveredWhenNonBlockingEnabled() throws Exception {
final LinkedHashSet<Message> received = new LinkedHashSet<>();
final LinkedHashSet<Message> beforeRollback = new LinkedHashSet<>();
final LinkedHashSet<Message> afterRollback = new LinkedHashSet<>();
Connection connection = connectionFactory.createConnection();
Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue(destinationName);
MessageConsumer consumer = session.createConsumer(destination);
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
received.add(message);
}
});
sendMessages();
session.commit();
connection.start();
assertTrue("Pre-Rollback expects to receive: " + MSG_COUNT + " messages.", Wait.waitFor(new Wait.Condition() {
@Override
public boolean isSatisified() throws Exception {
LOG.info("Consumer has received " + received.size() + " messages.");
return received.size() == MSG_COUNT;
}
}));
beforeRollback.addAll(received);
received.clear();
session.rollback();
assertTrue("Post-Rollback expects to receive: " + MSG_COUNT + " messages.", Wait.waitFor(new Wait.Condition() {
@Override
public boolean isSatisified() throws Exception {
LOG.info("Consumer has received " + received.size() + " messages since rollback.");
return received.size() == MSG_COUNT;
}
}));
afterRollback.addAll(received);
received.clear();
assertEquals(beforeRollback.size(), afterRollback.size());
assertEquals(beforeRollback, afterRollback);
session.commit();
}
@Test
public void testMessageDeleiveredInCorrectOrder() throws Exception {
final LinkedHashSet<Message> received = new LinkedHashSet<>();
final LinkedHashSet<Message> beforeRollback = new LinkedHashSet<>();
final LinkedHashSet<Message> afterRollback = new LinkedHashSet<>();
Connection connection = connectionFactory.createConnection();
Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue(destinationName);
MessageConsumer consumer = session.createConsumer(destination);
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
received.add(message);
}
});
sendMessages();
session.commit();
connection.start();
assertTrue("Pre-Rollback expects to receive: " + MSG_COUNT + " messages.", Wait.waitFor(new Wait.Condition() {
@Override
public boolean isSatisified() throws Exception {
LOG.info("Consumer has received " + received.size() + " messages.");
return received.size() == MSG_COUNT;
}
}));
beforeRollback.addAll(received);
received.clear();
session.rollback();
assertTrue("Post-Rollback expects to receive: " + MSG_COUNT + " messages.", Wait.waitFor(new Wait.Condition() {
@Override
public boolean isSatisified() throws Exception {
LOG.info("Consumer has received " + received.size() + " messages since rollback.");
return received.size() == MSG_COUNT;
}
}));
afterRollback.addAll(received);
received.clear();
assertEquals(beforeRollback.size(), afterRollback.size());
assertEquals(beforeRollback, afterRollback);
Iterator<Message> after = afterRollback.iterator();
Iterator<Message> before = beforeRollback.iterator();
while (before.hasNext() && after.hasNext()) {
TextMessage original = (TextMessage) before.next();
TextMessage rolledBack = (TextMessage) after.next();
int originalInt = Integer.parseInt(original.getText());
int rolledbackInt = Integer.parseInt(rolledBack.getText());
assertEquals(originalInt, rolledbackInt);
}
session.commit();
}
@Test
public void testMessageDeleiveryDoesntStop() throws Exception {
final LinkedHashSet<Message> received = new LinkedHashSet<>();
final LinkedHashSet<Message> beforeRollback = new LinkedHashSet<>();
final LinkedHashSet<Message> afterRollback = new LinkedHashSet<>();
Connection connection = connectionFactory.createConnection();
Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue(destinationName);
MessageConsumer consumer = session.createConsumer(destination);
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
received.add(message);
}
});
sendMessages();
connection.start();
assertTrue("Pre-Rollback expects to receive: " + MSG_COUNT + " messages.", Wait.waitFor(new Wait.Condition() {
@Override
public boolean isSatisified() throws Exception {
LOG.info("Consumer has received " + received.size() + " messages.");
return received.size() == MSG_COUNT;
}
}));
beforeRollback.addAll(received);
received.clear();
session.rollback();
sendMessages();
assertTrue("Post-Rollback expects to receive: " + MSG_COUNT + " messages.", Wait.waitFor(new Wait.Condition() {
@Override
public boolean isSatisified() throws Exception {
LOG.info("Consumer has received " + received.size() + " messages since rollback.");
return received.size() == MSG_COUNT * 2;
}
}));
afterRollback.addAll(received);
received.clear();
assertEquals(beforeRollback.size() * 2, afterRollback.size());
session.commit();
}
@Test
public void testNonBlockingMessageDeleiveryIsDelayed() throws Exception {
final LinkedHashSet<Message> received = new LinkedHashSet<>();
ActiveMQConnection connection = (ActiveMQConnection) connectionFactory.createConnection();
connection.getRedeliveryPolicy().setInitialRedeliveryDelay(TimeUnit.SECONDS.toMillis(6));
Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue(destinationName);
MessageConsumer consumer = session.createConsumer(destination);
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
received.add(message);
}
});
sendMessages();
connection.start();
assertTrue("Pre-Rollback expects to receive: " + MSG_COUNT + " messages.", Wait.waitFor(new Wait.Condition() {
@Override
public boolean isSatisified() throws Exception {
LOG.info("Consumer has received " + received.size() + " messages.");
return received.size() == MSG_COUNT;
}
}));
received.clear();
session.rollback();
assertFalse("Delayed redelivery test not expecting any messages yet.", Wait.waitFor(new Wait.Condition() {
@Override
public boolean isSatisified() throws Exception {
return received.size() > 0;
}
}, TimeUnit.SECONDS.toMillis(4)));
session.commit();
session.close();
}