Share

Search Java Code Snippets


  Help us in improving the repository. Add new snippets through 'Submit Code Snippet ' link.





#Java - Code Snippets for '#Iterator' - 6 code snippet(s) found

 Sample 1. Usage of Iterator

Iterator iterator = ReportingEmp.iterator();
while (iterator.hasNext()) {
   EmployeeBean eb1 = (EmployeeBean)iterator.next();
   EmployeeFactory empFactory = new EmployeeFactory(eb1.getType());
   svEmp = empFactory.getFactoryProduct();
   allocation += svEmp.getAllocation();
}

   Like      Feedback     collections  Iterator


 Sample 2. Get all Permutations of List Elements using Apache Commons PermutationIterator

List<String> list = new ArrayList();
list.add("Washington");
list.add("Nevada");
list.add("California");

PermutationIterator permIterator = new PermutationIterator((Collection) list);
      
permIterator.forEachRemaining(System.out::print); // prints [Washington, Nevada, California][Washington, California, Nevada][California, Washington, Nevada][California, Nevada, Washington][Nevada, California, Washington][Nevada, Washington, California]

   Like      Feedback     Permutations of collection elements  Permutations of List elements  Apache Commons  PermutationIterator  Iterator  Collections  .forEachRemaining  System.out::print  List  ArrayList


 Sample 3. Get Length of String having UTF8 Characters

public static int utf8Length(String string) {
   CharacterIterator iter = new StringCharacterIterator(string);
   char ch = iter.first();
   int size = 0;
   while (ch != CharacterIterator.DONE) {
      if ((ch >= 0xD800) && (ch < 0xDC00)) {
         char trail = iter.next();
         if ((trail > 0xDBFF) && (trail < 0xE000)) {
            size += 4;
         } else {
            size += 3;
            iter.previous(); // rewind one
         }
      } else if (ch < 0x80) {
         size++;
      } else if (ch < 0x800) {
         size += 2;
      } else {
         size += 3;
      }
      ch = iter.next();
   }
   return size;
}

   Like      Feedback     UTF8 characters  CharacterIterator.DONE  CharacterIterator  StringCharacterIterator


 Sample 4. Code Sample / Example / Snippet of java.util.Iterator

    public void validateRangeIterators() {

SortedRangeSet srs1 = new SortedRangeSet("1-10");

Iterator i1 = srs1.rangeIterator();

assert i1.hasNext() : "We should have one Range instance in our iterator.";

assert ((Range) i1.next()).toRepresentation().equals("1-10");

assert !i1.hasNext() : "There should be only one instance in our iterator.";

SortedRangeSet srs2 = new SortedRangeSet("1-5,8,10-15");

Iterator i2 = srs2.rangeIterator();

assert i2.hasNext() && i2.next() instanceof Range

&& i2.hasNext() && i2.next() instanceof Range

&& i2.hasNext() && i2.next() instanceof Range

&& !i2.hasNext() : "There should be exactly three Range instances in our iterator.";

SortedRangeSet srs3 = new SortedRangeSet("");

assert !srs3.iterator().hasNext() : "Iterator should be empty.";

}


   Like      Feedback      java.util.Iterator


Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner
 Sample 5. Code Sample / Example / Snippet of java.text.StringCharacterIterator

    public static String encode(String source) {

if (source == null) {

return "$e";

}

StringBuffer result = new StringBuffer();

StringCharacterIterator sci = new StringCharacterIterator(source);

for (char c = sci.current(); c != CharacterIterator.DONE; c = sci.next()) {

if (c == '$') {

result.append("$$");

}

else if (c == ',') {

result.append("$k");

}

else if (c == ' ') {

result.append("$n");

}

else if (c == ' ') {

result.append("$r");

}

else {

result.append(c);

}

}

return result.toString();

}


   Like      Feedback      java.text.StringCharacterIterator


 Sample 6. Code Sample / Example / Snippet of org.apache.ace.range.RangeIterator

    private void verifyStoreContents(final LogStoreImpl store, final int count, Writer... writers) throws IOException {

List<Descriptor> descriptors = store.getDescriptors();



long expectedID = 0;

for (Descriptor desc : descriptors) {

SortedRangeSet rangeSet = desc.getRangeSet();

RangeIterator rangeIter = rangeSet.iterator();



while (rangeIter.hasNext()) {

long id = rangeIter.next();



Event expectedEntry = null;

for (int i = 0; (expectedEntry == null) && i < writers.length; i++) {

expectedEntry = writers[i].m_written.remove(id);

}

assertNotNull(expectedEntry, "Event ID #" + id + " never written?!");

assertEquals(expectedEntry.getID(), expectedID++, "Entry ID mismatch?!");

}

}

}


   Like      Feedback      org.apache.ace.range.RangeIterator



Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner



comments powered by Disqus