Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
I am learning GoF Java Design Patterns and I want to see some real life examples of them. Can you guys point to some good usage of these Design Patterns.(preferably in Java's core libraries). Thank you
In Java, is it legal to call remove on a collection when iterating through the collection using a foreach loop? For instance: List<String> names = .... for (String name : names) { // Do something names.remove(name). } As an addendum, is it legal to remove items that have not been iterated over yet? For instance, //Assume that the names list as duplicate entries List<String&...
Is there a foreach structure in MATLAB? If so, what happens if the underlying data changes (i.e. if objects are added to the set)?
I have a big doubt and I hope someone can help me out. I need to delete some objects from an arraylist if they meet a condition and I'm wondering which way could be more efficient. Here's the situation: I have a class that contains an arraylist containing some other objects. I have to iterate over this arraylist and delete all elements meeting a certain condition. As far as I know, those would...
Assume, I have a constant number of collections (e.g. 3 ArrayLists) as members of a class. Now, I want to expose all the elements to other classes so they can simply iterate over all elements (ideally, read only). I'm using guava collections and I wonder how I could use guava iterables/iterators to generate a logical view on the internal collections without making temporary copies.
I've been trying lately to implement some clean coding practices in AS3. One of these has been to not give away references to Arrays from a containing object. The point being that I control addition and removal from one Class and all other users of the Array receive read only version. At the moment that read only version is a ArrayIterator class I wrote, which implements a typical Iterator int...
I need a Iterator<Character> from a String object. Is there any available function in Java that provides me this or do I have to code my own?
what is the exact different of both.. is using enumeration more benefit than using iterator..? can anyone elaborate.. any reference article would be appeciated
How to get an iterator for an array in java? int[] arr={1,2,3}; for(int i:arr) System.out.println(i); How does the above for-each loop work ? Is the array converted to a list to get the iterator ?
I want to be able to remove multiple elements from a set while I am iterating over it. Initially I hoped that iterators were smart enough for the naive solution below to work. Set<SomeClass> set = new HashSet<SomeClass>(); fillSet(set); Iterator<SomeClass> it = set.iterator(); while (it.hasNext()) { set.removeAll(setOfElementsToRemove(it.next())); } But this throws a Co...
I want to write a method that removes all elements from a collection that follow a certain pattern. In functional languages, I would use filter() with a lambda expression. However, in Java, it seems I'm stuck with this: public void removeAllBlueCars() { LinkedList<Car> carsToRemove = new LinkedList<Car>(); for (Car c : cars) { if (c.getCarColor() == Color.BLUE) { ...
Being new to this I really am trying to learn how to keep code as simple as possible, whilst doing the job it's supposed to. The question I have done is from Project Euler, it says Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... Find the s...
The java.lang.Iterator interface has 3 methods: hasNext, next and remove. In order to implement a read-only iterator, you have to provide an implementation for 2 of those: hasNext and next. My problem is that these methods does not declare any exceptions. So if my code inside the iteration process declares exceptions, I must enclose my iteration code inside a try/catch block. My current polic...
If I have a list containing [alice,bob, abigail, charlie] and I want to write an iterator such that it iterates over elements that begin with 'a', can I write my own ? How can I do that ?
I have an implementation of java.util.Iterator which requires that the call to next() should always be proceeded by a call to hasNext(). (This is because results are returned asynchronosly in a multi threaded environment and it is never clear how many more results there might be). Would it be 'correct' to properly document this in the JavaDoc and then throw a RuntimeException if this was viola...
What is the most efficient way to do this?
I'm wrapping a java.sql.RecordSet inside a java.util.Iterator. My question is, what should I do in case any recordset method throws an SQLException? The java.util.Iterator javadoc explains which exceptions to throw in various situations (i.e. NoSuchElementException in case you call next() beyond the last element) However, it doesn't mention what to do when there is an entirely unrelated prob...
Insertion or deletion of an element at a specific point of a list, assuming that we have a pointer to the node already, is a constant-time operation. - from the Wikipedia Article on Linked list Linked list traversal in a single linked list always starts from the head. We have to keep going till we satisfy a given condition. So that will make any operation worst case O(n) unless we are d...
I have set up a HashMap like so: Map<String, ArrayList<String>> theAccused = new HashMap<String, ArrayList<String>>(); ... and I populate this by storing for every name (key), a list of names (value). So: ArrayList<String> saAccused = new ArrayList<String>(); // populate 'saAccused' ArrayList ... // done populating theAccused.put(sAccuser, saAccused); S...
Suppose I have a base class B, and a derived class D. I wish to have a method foo() within my base class that returns a new object of whatever type the instance is. So, for example, if I call B.foo() it returns an object of type B, while if I call D.foo() it returns an object of type D; meanwhile, the implementation resides solely in the base class B. Is this possible?
Exception in thread "main" java.util.ConcurrentModificationException Squash the PC dirties the room Violet. The room's state is now dirty Lily the animal growls The Animal Lily left the room and goes to Green through the west door. at java.util.HashMap$HashIterator.nextEntry(HashMap.java:793) at java.util.HashMap$KeyIterator.next(HashMap.java:828) at homework5.Room.critR...
 /*
  * Copyright 1997-2006 Sun Microsystems, Inc.  All Rights Reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Sun designates this
  * particular file as subject to the "Classpath" exception as provided
  * by Sun in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */
package java.util;

An iterator over a collection. Iterator takes the place of Enumeration in the Java Collections Framework. Iterators differ from enumerations in two ways:
  • Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics.
  • Method names have been improved.

This interface is a member of the Java Collections Framework.

Author(s):
Josh Bloch
Since:
1.2
See also:
Collection
ListIterator
java.lang.Iterable
public interface Iterator<E> {
    
Returns true if the iteration has more elements. (In other words, returns true if next() would return an element rather than throwing an exception.)

Returns:
true if the iteration has more elements
    boolean hasNext();

    
Returns the next element in the iteration.

Returns:
the next element in the iteration
Throws:
NoSuchElementException if the iteration has no more elements
    E next();

    
Removes from the underlying collection the last element returned by this iterator (optional operation). This method can be called only once per call to next(). The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method.

Throws:
java.lang.UnsupportedOperationException if the remove operation is not supported by this iterator
java.lang.IllegalStateException if the next method has not yet been called, or the remove method has already been called after the last call to the next method
    void remove();
New to GrepCode? Check out our FAQ X