List of usage examples for java.util Iterator hasNext
boolean hasNext();
From source file:SetTest.java
public static void main(String[] args) { Set<String> words = new HashSet<String>(); // HashSet implements Set long totalTime = 0; Scanner in = new Scanner(System.in); while (in.hasNext()) { String word = in.next();/* w w w . jav a 2s. co m*/ long callTime = System.currentTimeMillis(); words.add(word); callTime = System.currentTimeMillis() - callTime; totalTime += callTime; } Iterator<String> iter = words.iterator(); for (int i = 1; i <= 20 && iter.hasNext(); i++) System.out.println(iter.next()); System.out.println(". . ."); System.out.println(words.size() + " distinct words. " + totalTime + " milliseconds."); }
From source file:HelloPeopleJDOM.java
public static void main(String[] args) throws Exception { SAXBuilder builder = new SAXBuilder(); Document doc = builder.build("./src/data.xml"); StringBuffer output = new StringBuffer(); // create the basic HTML output output.append("<html>\n").append("<head>\n<title>\nPerson List</title>\n</head>\n").append("<body>\n") .append("<ul>\n"); Iterator itr = doc.getRootElement().getChildren().iterator(); while (itr.hasNext()) { Element elem = (Element) itr.next(); output.append("<li>"); output.append(elem.getAttribute("lastName").getValue()); output.append(", "); output.append(elem.getAttribute("firstName").getValue()); output.append("</li>\n"); }/* ww w. j a va 2s. c om*/ // create the end of the HTML output output.append("</ul>\n</body>\n</html>"); System.out.println(output.toString()); }
From source file:Employee.java
public static void main(String args[]) { LinkedList<Employee> phonelist = new LinkedList<Employee>(); phonelist.add(new Employee("A", "1")); phonelist.add(new Employee("B", "2")); phonelist.add(new Employee("C", "3")); Iterator<Employee> itr = phonelist.iterator(); Employee pe;//ww w . j a va2s. c o m while (itr.hasNext()) { pe = itr.next(); System.out.println(pe.name + ": " + pe.number); } ListIterator<Employee> litr = phonelist.listIterator(phonelist.size()); while (litr.hasPrevious()) { pe = litr.previous(); System.out.println(pe.name + ": " + pe.number); } }
From source file:Main.java
public static void main(String[] args) { // create a LinkedList LinkedList<String> list = new LinkedList<String>(); // add some elements list.add("Hello"); list.add("from java2s.com"); list.add("10"); // print the list System.out.println("LinkedList:" + list); // set Iterator as descending Iterator x = list.descendingIterator(); // print list with descending order while (x.hasNext()) { System.out.println(x.next()); }//from w w w . ja v a 2 s .c o m }
From source file:MapEntrySetDemo.java
public static void main(String[] argv) { // Construct and load the hash. This simulates loading a // database or reading from a file, or wherever the data is. Map map = new HashMap(); // The hash maps from company name to address. // In real life this might map to an Address object... map.put("Adobe", "Mountain View, CA"); map.put("IBM", "White Plains, NY"); map.put("Learning Tree", "Los Angeles, CA"); map.put("Microsoft", "Redmond, WA"); map.put("Netscape", "Mountain View, CA"); map.put("O'Reilly", "Sebastopol, CA"); map.put("Sun", "Mountain View, CA"); // List the entries using entrySet() Set entries = map.entrySet(); Iterator it = entries.iterator(); while (it.hasNext()) { Map.Entry entry = (Map.Entry) it.next(); System.out.println(entry.getKey() + "-->" + entry.getValue()); }// www. j av a 2 s .c o m }
From source file:MainClass.java
public static void main(String[] args) throws Exception { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); KeyPair pair = generateRSAKeyPair(); ByteArrayOutputStream bOut = new ByteArrayOutputStream(); bOut.write(generateV1Certificate(pair).getEncoded()); bOut.close();/* w ww. java 2 s . co m*/ InputStream in = new ByteArrayInputStream(bOut.toByteArray()); CertificateFactory fact = CertificateFactory.getInstance("X.509", "BC"); X509Certificate x509Cert; Collection collection = new ArrayList(); while ((x509Cert = (X509Certificate) fact.generateCertificate(in)) != null) { collection.add(x509Cert); } Iterator it = collection.iterator(); while (it.hasNext()) { System.out.println("version: " + ((X509Certificate) it.next()).getVersion()); } }
From source file:MainClass.java
public static void main(String[] args) throws IOException { for (int i = 0; i < data.length; i++) data[i] = (byte) i; ServerSocketChannel server = ServerSocketChannel.open(); server.configureBlocking(false);//from w w w . j av a 2 s.com server.socket().bind(new InetSocketAddress(9000)); Selector selector = Selector.open(); server.register(selector, SelectionKey.OP_ACCEPT); while (true) { selector.select(); Set readyKeys = selector.selectedKeys(); Iterator iterator = readyKeys.iterator(); while (iterator.hasNext()) { SelectionKey key = (SelectionKey) iterator.next(); iterator.remove(); if (key.isAcceptable()) { SocketChannel client = server.accept(); System.out.println("Accepted connection from " + client); client.configureBlocking(false); ByteBuffer source = ByteBuffer.wrap(data); SelectionKey key2 = client.register(selector, SelectionKey.OP_WRITE); key2.attach(source); } else if (key.isWritable()) { SocketChannel client = (SocketChannel) key.channel(); ByteBuffer output = (ByteBuffer) key.attachment(); if (!output.hasRemaining()) { output.rewind(); } client.write(output); } key.channel().close(); } } }
From source file:SetExampleV2.java
public static void main(String args[]) { // create two sets Set set1 = new HashSet(); set1.add("Red"); set1.add("Green"); Set set2 = new HashSet(); set2.add("Yellow"); set2.add("Red"); // create a composite set out of these two CompositeSet composite = new CompositeSet(); // set the class that handles additions, conflicts etc // composite.setMutator(new CompositeMutator()); // initialize the composite with the sets // Cannot be used if set1 and set2 intersect is not null and // a strategy to deal with it has not been set composite.addComposited(new Set[] { set1, set2 }); // do some addition/deletions // composite.add("Pink"); // composite.remove("Green"); // whats left in the composite? Iterator itr = composite.iterator(); while (itr.hasNext()) { System.err.println(itr.next()); }/*from w ww.j a va 2 s .c o m*/ }
From source file:Main.java
public static void main(String[] args) { // Create a list of strings List<String> names = new ArrayList<>(); names.add("A"); names.add("B"); names.add("C"); // Get an iterator for the list Iterator<String> nameIterator = names.iterator(); // Iterate over all elements in the list while (nameIterator.hasNext()) { // Get the next element from the list String name = nameIterator.next(); System.out.println(name); }/*from w ww . j a v a2 s.c o m*/ }
From source file:TreeSetDemo.java
public static void main(String[] argv) { //+/*from ww w . j ava 2 s. co m*/ /* * A TreeSet keeps objects in sorted order. We use a Comparator * published by String for case-insensitive sorting order. */ TreeSet tm = new TreeSet(String.CASE_INSENSITIVE_ORDER); tm.add("Gosling"); tm.add("da Vinci"); tm.add("van Gogh"); tm.add("Java To Go"); tm.add("Vanguard"); tm.add("Darwin"); tm.add("Darwin"); // TreeSet is Set, ignores duplicates. // Since it is sorted we can ask for various subsets System.out.println("Lowest (alphabetically) is " + tm.first()); // Print how many elements are greater than "k" System.out.println(tm.tailSet("k").toArray().length + " elements higher than \"k\""); // Print the whole list in sorted order System.out.println("Sorted list:"); java.util.Iterator t = tm.iterator(); while (t.hasNext()) System.out.println(t.next()); //- }