List of usage examples for java.util Iterator next
E next();
From source file:Main.java
@SuppressWarnings("rawtypes") public static Map<String, Integer> swapIntKeysAndStrValues(Map<Integer, String> hm) { HashMap<String, Integer> nhm = new HashMap<String, Integer>(); Set<?> s = hm.entrySet(); Iterator<?> it = s.iterator(); while (it.hasNext()) { Map.Entry m = (Map.Entry) it.next(); nhm.put((String) m.getValue(), (Integer) m.getKey()); }//from w w w . j a v a2 s . c o m return nhm; }
From source file:Main.java
public static <T> List<T> search(final String str, Collection<? extends T> coll, Comparable<? super T> comp) { if (comp == null) return null; List<T> list = new ArrayList<T>(); Iterator<? extends T> i = coll.iterator(); while (i.hasNext()) { T next = i.next(); if (comp.compareTo(next) > 0) { list.add(next);/*from w w w.j ava 2 s . c o m*/ } } return list; }
From source file:Main.java
@SuppressWarnings("rawtypes") public static Map<String, String> swapStrKeysAndStrValues(Map<String, ?> hm) { HashMap<String, String> nhm = new HashMap<String, String>(); Set<?> s = hm.entrySet(); Iterator<?> it = s.iterator(); while (it.hasNext()) { Map.Entry m = (Map.Entry) it.next(); nhm.put((String) m.getValue(), (String) m.getKey()); }// w w w .j a va2s . com return nhm; }
From source file:Main.java
public static JSONObject shallowDiffJSONObjects(JSONObject jo1, JSONObject jo2) throws JSONException { JSONObject result = new JSONObject(); Iterator<String> it = jo2.keys(); while (it.hasNext()) { String key = it.next(); // See if the key is missing or the value is different in the other object Object val1 = jo1.opt(key); Object val2 = jo2.get(key); if (val1 == null || !val2.equals(val1)) result.put(key, val2); }//from ww w .j a v a2 s. co m return result; }
From source file:Main.java
/** * Adds the contents of an iterator to an existing Collection. * @param <T> The type of elements returned by the iterator. * @param collection The receptacle to put the elements into. * @param elementsToAdd The elements to add to the Collection. */// w w w . jav a 2 s . c o m public static <T> void addToCollection(Collection<T> collection, Iterator<T> elementsToAdd) { while (elementsToAdd.hasNext()) { T next = elementsToAdd.next(); collection.add(next); } }
From source file:Main.java
public static <T> List<T> listFromIterator(Iterator<T> i) { List<T> retval = new ArrayList<T>(); while (i.hasNext()) retval.add(i.next()); return retval; }
From source file:Main.java
public static List list(Iterator ite) { ArrayList ret = new ArrayList(); while (ite.hasNext()) { Object o = ite.next(); ret.add(o);//ww w .j a v a 2 s . c o m } return ret; }
From source file:Main.java
public static Object keyString(HashMap<String, String> map, String o) { Iterator<String> it = map.keySet().iterator(); while (it.hasNext()) { Object keyString = it.next(); if (map.get(keyString).equals(o)) return keyString; }// ww w . j a va 2s. c o m return null; }
From source file:Main.java
public static <K, V> Map<K, V> convertMap(final Map<K, V> map, final Collection<K> keys, final Collection<V> values) { if (keys.size() != values.size()) { throw new IllegalArgumentException("Invalid Collection. Collection size is not equals."); }/* w w w .j av a 2s. c o m*/ Iterator<V> valueIt = values.iterator(); for (K key : keys) { map.put(key, valueIt.next()); } return map; }
From source file:Main.java
public static <T> T getByIndex(Iterable<T> iterable, int index) { T el = null;//from www . j av a2 s . c o m Iterator<T> it = iterable.iterator(); for (int i = 0; it.hasNext(); i++) { T cur = it.next(); if (i == index) { el = cur; break; } } return el; }