Example usage for java.util Iterator hasNext

List of usage examples for java.util Iterator hasNext

Introduction

In this page you can find the example usage for java.util Iterator hasNext.

Prototype

boolean hasNext();

Source Link

Document

Returns true if the iteration has more elements.

Usage

From source file:DiameterMap.java

public static void main(String args[]) {
    String names[] = { "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto" };
    float diameters[] = { 4800f, 12103.6f, 12756.3f, 6794f, 142984f, 120536f, 51118f, 49532f, 2274f };
    Map map = new TreeMap();
    for (int i = 0, n = names.length; i < n; i++) {
        map.put(names[i], new Float(diameters[i]));
    }//from  w w w. j av  a 2 s  .  com
    Iterator it = map.keySet().iterator();
    Object obj;
    while (it.hasNext()) {
        obj = it.next();
        System.out.println(obj + ": " + map.get(obj));
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Object keyObject = "";
    Object valueObject = "";
    Map<Object, Object> weakMap = new WeakHashMap<Object, Object>();

    weakMap.put(keyObject, valueObject);
    WeakReference weakValue = new WeakReference<Object>(valueObject);

    weakMap.put(keyObject, weakValue);/*  w ww  .  j a v  a  2 s  .  c om*/

    Iterator it = weakMap.keySet().iterator();
    while (it.hasNext()) {
        Object key = it.next();
        weakValue = (WeakReference) weakMap.get(key);
        if (weakValue == null) {
            System.out.println("Value has been garbage-collected");
        } else {
            System.out.println("Get value");
            valueObject = weakValue.get();
        }
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Map<String, Integer> map = new HashMap<String, Integer>();
    map = new TreeMap();

    map.put("a", new Integer(1));
    map.put("b", new Integer(2));
    map.put("c", new Integer(3));

    int size = map.size(); // 2

    Object oldValue = map.put("a", new Integer(9)); // 1

    oldValue = map.remove("c"); // 3

    Iterator it = map.keySet().iterator();
    while (it.hasNext()) {
        Object key = it.next();/*  w w w  . j a  v  a 2 s . c o  m*/
    }

    it = map.values().iterator();
    while (it.hasNext()) {
        Object value = it.next();
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Object keyObject = "";
    Object valueObject = "";
    Map weakMap = new WeakHashMap();

    weakMap.put(keyObject, valueObject);
    WeakReference weakValue = new WeakReference(valueObject);

    weakMap.put(keyObject, weakValue);/*w  w  w  .j  av a  2  s.c om*/

    Iterator it = weakMap.keySet().iterator();
    while (it.hasNext()) {
        Object key = it.next();
        weakValue = (WeakReference) weakMap.get(key);
        if (weakValue == null) {
            System.out.println("Value has been garbage-collected");
        } else {
            System.out.println("Get value");
            valueObject = weakValue.get();
        }
    }
}

From source file:Main.java

public static void main(String[] a) {
    Map<String, String> yourMap = new HashMap<String, String>();
    yourMap.put("1", "one");
    yourMap.put("2", "two");
    yourMap.put("3", "three");

    Map<String, Object> map = new LinkedHashMap<String, Object>();

    List<String> keyList = new ArrayList<String>(yourMap.keySet());
    List<String> valueList = new ArrayList<String>(yourMap.values());
    Set<String> sortedSet = new TreeSet<String>(valueList);

    Object[] sortedArray = sortedSet.toArray();
    int size = sortedArray.length;

    for (int i = 0; i < size; i++) {
        map.put(keyList.get(valueList.indexOf(sortedArray[i])), sortedArray[i]);
    }//from ww  w  . j  av a 2  s. c om

    Set ref = map.keySet();
    Iterator it = ref.iterator();

    while (it.hasNext()) {
        String i = (String) it.next();
        System.out.println(i);
    }
}

From source file:SetDemo.java

public static void main(String[] args) {
    String[] letters = { "A", "B", "C", "D", "E" };

    Set s = new HashSet();

    for (int i = 0; i < letters.length; i++)
        s.add(letters[i]);/* ww w . j  ava  2  s  . com*/

    if (!s.add(letters[0]))
        System.out.println("Cannot add a duplicate.\n");

    Iterator iter = s.iterator();
    while (iter.hasNext())
        System.out.println(iter.next());

    System.out.println("");

    SortedSet ss = new TreeSet();

    for (int i = 0; i < letters.length; i++)
        ss.add(letters[i]);

    if (!ss.add(letters[0]))
        System.out.println("Cannot add a duplicate.\n");

    iter = ss.iterator();
    while (iter.hasNext())
        System.out.println(iter.next());
}

From source file:HTDemo2.java

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

    String str;// w ww  .j a va2  s.  co  m
    double bal;

    balance.put("A", 3434.34);
    balance.put("B", 123.22);
    balance.put("C", 1378.00);
    balance.put("D", 99.22);
    balance.put("E", -19.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:Main.java

public static void main(String[] args) {
    Map<String, int[]> map = new TreeMap<String, int[]>();

    int[] array = new int[3];
    array[0] = 0;/*from  w  w  w.java  2  s. c om*/
    array[1] = 1;
    array[2] = 2;
    map.put("array", array);

    Iterator<String> iter = map.keySet().iterator();

    while (iter.hasNext()) {
        String arrayName = iter.next();
        array = map.get(arrayName);
        System.out.print(arrayName + ":");
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]);
        }
    }
}

From source file:Main.java

public static void main(String[] args) {

    TreeSet<Integer> treeadd = new TreeSet<Integer>();

    treeadd.add(1);/*from   w w w .  j  ava2  s.  co  m*/
    treeadd.add(13);
    treeadd.add(17);
    treeadd.add(2);

    // create descending iterator
    Iterator<Integer> iterator = treeadd.descendingIterator();

    while (iterator.hasNext()) {
        System.out.println(iterator.next());
    }
}

From source file:Main.java

public static void main(String[] args) {
    Map charsets = Charset.availableCharsets();
    Iterator iterator = charsets.values().iterator();
    while (iterator.hasNext()) {
        Charset cs = (Charset) iterator.next();
        System.out.print(cs.displayName());
        if (cs.isRegistered()) {
            System.out.print(" (registered): ");
        } else {//from  ww w .ja v  a2  s.  c om
            System.out.print(" (unregistered): ");
        }
        System.out.print(cs.name());
        Iterator names = cs.aliases().iterator();
        while (names.hasNext()) {
            System.out.print(", ");
            System.out.print(names.next());
        }
        System.out.println();
    }
}