Example usage for java.util Set iterator

List of usage examples for java.util Set iterator

Introduction

In this page you can find the example usage for java.util Set iterator.

Prototype

Iterator<E> iterator();

Source Link

Document

Returns an iterator over the elements in this set.

Usage

From source file:MainClass.java

public static void main(String args[]) {
    Hashtable<String, Double> balance = new Hashtable<String, Double>();

    String str;/*  w  w  w.  j  av a  2  s. c  o m*/
    double bal;

    balance.put("A", 4.34);
    balance.put("B", 3.22);
    balance.put("C", 8.00);
    balance.put("D", 9.22);
    balance.put("E", -9.08);

    Set<String> set = balance.keySet();

    Iterator<String> itr = set.iterator();
    while (itr.hasNext()) {
        str = itr.next();
        System.out.println(str + ": " + balance.get(str));
    }

    System.out.println();

    bal = balance.get("A");
    balance.put("A", bal + 1000);
    System.out.println("A's new balance: " + balance.get("A"));
}

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());
    }/*from ww  w  .j  ava2s.com*/
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    FileInputStream in = new FileInputStream(args[0]);
    X509CRL crl = (X509CRL) cf.generateCRL(in);
    Set s = crl.getRevokedCertificates();
    if (s != null && s.isEmpty() == false) {
        Iterator t = s.iterator();
        while (t.hasNext()) {
            X509CRLEntry entry = (X509CRLEntry) t.next();
            System.out.println("serial number = " + entry.getSerialNumber().toString(16));
            System.out.println("revocation date = " + entry.getRevocationDate());
            System.out.println("extensions = " + entry.hasExtensions());
        }/*from ww  w . j  a va 2 s.  co  m*/
    }
    in.close();
}

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  .java2 s. c om*/

    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:ArraySet.java

public static void main(String args[]) {
    String elements[] = { "A", "B", "C", "D", "E" };
    Set set = new ArraySet(Arrays.asList(elements));
    Iterator iter = set.iterator();
    while (iter.hasNext()) {
        System.out.println(iter.next());
    }//  w  ww .  j  a v  a 2s  .  co m
}

From source file:ArraySet.java

public static void main(String args[]) {
    String elements[] = { "Java", "Source", "and", "Support", "." };
    Set set = new ArraySet(Arrays.asList(elements));
    Iterator iter = set.iterator();
    while (iter.hasNext()) {
        System.out.println(iter.next());
    }//from   w ww . j a  v a  2  s .com
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setValidating(false);//from w w  w . j av a2  s  .c  o m
    domFactory.setNamespaceAware(true);
    domFactory.setIgnoringComments(true);
    domFactory.setIgnoringElementContentWhitespace(true);

    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document dDoc = builder.parse("C:/data.xsd");

    Node rootNode = dDoc.getElementsByTagName("xs:schema").item(0);
    System.out.println(rootNode.getNodeName());

    XPath xPath1 = XPathFactory.newInstance().newXPath();
    NamespaceContext nsContext = new NamespaceContext() {
        @Override
        public String getNamespaceURI(String prefix) {
            return "http://www.w3.org/2001/XMLSchema";
        }

        @Override
        public String getPrefix(String namespaceURI) {
            return "xs";
        }

        @Override
        public Iterator getPrefixes(String namespaceURI) {
            Set s = new HashSet();
            s.add("xs");
            return s.iterator();
        }
    };
    xPath1.setNamespaceContext((NamespaceContext) nsContext);
    NodeList nList1 = (NodeList) xPath1.evaluate("//xs:schema", dDoc, XPathConstants.NODESET);
    System.out.println(nList1.item(0).getNodeName());

    NodeList nList2 = (NodeList) xPath1.evaluate("//xs:element", rootNode, XPathConstants.NODESET);
    System.out.println(nList2.item(0).getNodeName());
}

From source file:Main.java

public static void main(String[] argv) {
    // Create the sorted set
    Set<String> set = new TreeSet<String>();

    set.add("b");
    set.add("c");
    set.add("a");

    Iterator it = set.iterator();
    while (it.hasNext()) {

        Object element = it.next();
        System.out.println(element);
    }/*from www .j  a  v a 2s  . com*/

    // Create an array containing the elements in a set
    String[] array = (String[]) set.toArray(new String[set.size()]);
    Arrays.toString(array);
}

From source file:NewFingerServer.java

public static void main(String[] arguments) throws Exception {
    ServerSocketChannel sockChannel = ServerSocketChannel.open();
    sockChannel.configureBlocking(false);

    InetSocketAddress server = new InetSocketAddress("localhost", 79);
    ServerSocket socket = sockChannel.socket();
    socket.bind(server);/*from   w  w  w.  jav  a2s  .co  m*/

    Selector selector = Selector.open();
    sockChannel.register(selector, SelectionKey.OP_ACCEPT);

    while (true) {
        selector.select();
        Set keys = selector.selectedKeys();
        Iterator it = keys.iterator();
        while (it.hasNext()) {
            SelectionKey selKey = (SelectionKey) it.next();
            it.remove();
            if (selKey.isAcceptable()) {
                ServerSocketChannel selChannel = (ServerSocketChannel) selKey.channel();
                ServerSocket selSocket = selChannel.socket();
                Socket connection = selSocket.accept();

                InputStreamReader isr = new InputStreamReader(connection.getInputStream());
                BufferedReader is = new BufferedReader(isr);
                PrintWriter pw = new PrintWriter(new BufferedOutputStream(connection.getOutputStream()), false);
                pw.println("NIO Finger Server");
                pw.flush();
                String outLine = null;
                String inLine = is.readLine();
                if (inLine.length() > 0) {
                    outLine = inLine;
                }
                readPlan(outLine, pw);
                pw.flush();
                pw.close();
                is.close();

                connection.close();
            }
        }
    }
}

From source file:MainClass.java

public static void main(String[] args) throws IOException {
    Charset charset = Charset.forName("ISO-8859-1");
    CharsetEncoder encoder = charset.newEncoder();
    CharsetDecoder decoder = charset.newDecoder();

    ByteBuffer buffer = ByteBuffer.allocate(512);

    Selector selector = Selector.open();

    ServerSocketChannel server = ServerSocketChannel.open();
    server.socket().bind(new java.net.InetSocketAddress(8000));
    server.configureBlocking(false);/*from www  . ja va2s. co  m*/
    SelectionKey serverkey = server.register(selector, SelectionKey.OP_ACCEPT);

    for (;;) {
        selector.select();
        Set keys = selector.selectedKeys();

        for (Iterator i = keys.iterator(); i.hasNext();) {
            SelectionKey key = (SelectionKey) i.next();
            i.remove();

            if (key == serverkey) {
                if (key.isAcceptable()) {
                    SocketChannel client = server.accept();
                    client.configureBlocking(false);
                    SelectionKey clientkey = client.register(selector, SelectionKey.OP_READ);
                    clientkey.attach(new Integer(0));
                }
            } else {
                SocketChannel client = (SocketChannel) key.channel();
                if (!key.isReadable())
                    continue;
                int bytesread = client.read(buffer);
                if (bytesread == -1) {
                    key.cancel();
                    client.close();
                    continue;
                }
                buffer.flip();
                String request = decoder.decode(buffer).toString();
                buffer.clear();
                if (request.trim().equals("quit")) {
                    client.write(encoder.encode(CharBuffer.wrap("Bye.")));
                    key.cancel();
                    client.close();
                } else {
                    int num = ((Integer) key.attachment()).intValue();
                    String response = num + ": " + request.toUpperCase();
                    client.write(encoder.encode(CharBuffer.wrap(response)));
                    key.attach(new Integer(num + 1));
                }
            }
        }
    }
}