Java Code Examples for java.util.LinkedHashSet#clear()

The following examples show how to use java.util.LinkedHashSet#clear() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LinkedHashMapTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 2
Source File: FindInProjectSettingsBase.java    From consulo with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 3
Source File: OpenSSLCipherConfigurationParser.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
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);
}
 
Example 4
Source File: RemoveAllElementsFromLinkedHashSetExample.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
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.
    */
  }
 
Example 5
Source File: RemoveAllElementsFromLinkedHashSetExample.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
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.
    */
    }
 
Example 6
Source File: ReflexionCycleHelper.java    From ctsms with GNU Lesser General Public License v2.1 5 votes vote down vote up
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);
	}
}
 
Example 7
Source File: CodeLensHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
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();
}
 
Example 8
Source File: AbstractFileInputOperatorTest.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 9
Source File: SetSynchronizer.java    From pravega with Apache License 2.0 4 votes vote down vote up
@Override
public void process(LinkedHashSet<T> impl) {
    impl.clear();
}
 
Example 10
Source File: NonBlockingConsumerRedeliveryTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@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();
}
 
Example 11
Source File: NonBlockingConsumerRedeliveryTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@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();
}
 
Example 12
Source File: NonBlockingConsumerRedeliveryTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@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();
}
 
Example 13
Source File: NonBlockingConsumerRedeliveryTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@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();
}