Example usage for java.util SortedSet iterator

List of usage examples for java.util SortedSet iterator

Introduction

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

Prototype

Iterator<E> iterator();

Source Link

Document

Returns an iterator over the elements in this set.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    SortedSet<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();
    }/*w ww  .j  a  va  2s .  c o  m*/
    String[] array = (String[]) set.toArray(new String[set.size()]);
}

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]);//from   ww  w  .j a  v  a2s . c o  m

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

public static void main(String[] args) {
    SortedSet sortedSet = new TreeSet(Arrays.asList("one two three four five six seven eight".split(" ")));
    System.out.println(sortedSet);
    Object low = sortedSet.first(), high = sortedSet.last();
    System.out.println(low);//from   ww w  . j  a  va2 s  . c  om
    System.out.println(high);
    Iterator it = sortedSet.iterator();
    for (int i = 0; i <= 6; i++) {
        if (i == 3)
            low = it.next();
        if (i == 6)
            high = it.next();
        else
            it.next();
    }
    System.out.println(low);
    System.out.println(high);
    System.out.println(sortedSet.subSet(low, high));
    System.out.println(sortedSet.headSet(high));
    System.out.println(sortedSet.tailSet(low));
}

From source file:org.soaplab.admin.ExploreConfig.java

/*************************************************************************
 *
 *  An entry point//w w  w .  j  a  v  a2  s .co  m
 *
 *************************************************************************/
@SuppressWarnings("unchecked")
public static void main(String[] args) {

    try {
        BaseCmdLine cmd = getCmdLine(args, ExploreConfig.class);
        String param = null;

        // remember System properties before we initialize
        // Soaplab's Config
        Properties sysProps = System.getProperties();

        // for testing: adding property files
        if ((param = cmd.getParam("-f")) != null) {
            title("Adding config property files:");
            String[] files = param.split(",");
            for (String filename : files) {
                if (Config.addConfigPropertyFile(filename))
                    msgln("Added:  " + filename);
                else
                    msgln("Failed: " + filename);
            }
            msgln("");
        }
        if ((param = cmd.getParam("-xml")) != null) {
            title("Adding config XML files:");
            String[] files = param.split(",");
            for (String filename : files) {
                if (Config.addConfigXMLFile(filename))
                    msgln("Added:  " + filename);
                else
                    msgln("Failed: " + filename);
            }
            msgln("");
        }

        // list of configurations
        if (cmd.hasOption("-lf")) {
            title("Using property resources (in this order):");
            int i = 0;
            while (true) {
                Configuration cfg = null;
                int count = 0;
                try {
                    cfg = Config.get().getConfiguration(i++);
                    for (Iterator<String> it = cfg.getKeys(); it.hasNext(); count++)
                        it.next();
                } catch (IndexOutOfBoundsException e) {
                    break;
                }
                if (count == 0)
                    msg(i + ": (empty) ");
                else
                    msg(i + ": (" + String.format("%1$5d", count) + ") ");
                if (cfg instanceof FileConfiguration) {
                    msgln(((FileConfiguration) cfg).getFile().getAbsolutePath());
                } else if (cfg instanceof SystemConfiguration) {
                    msgln("Java System Properties");
                } else if (cfg instanceof BaseConfiguration) {
                    msgln("Directly added properties (no config file)");
                } else {
                    msgln(cfg.getClass().getName());
                }
            }
            msgln("");
        }

        // list of properties
        boolean listProps = cmd.hasOption("-l");
        boolean listAllProps = cmd.hasOption("-la");
        if (listProps || listAllProps) {
            title("Available properties" + (listProps ? " (except System properties):" : ":"));
            SortedMap<String, String> allProps = new TreeMap<String, String>();
            int maxLen = 0;
            for (Iterator<String> it = Config.get().getKeys(); it.hasNext();) {
                String key = it.next();
                if (listAllProps || !sysProps.containsKey(key)) {
                    if (key.length() > maxLen)
                        maxLen = key.length();
                    String[] values = Config.get().getStringArray(key);
                    if (values.length != 0)
                        allProps.put(key, StringUtils.join(values, ","));
                }
            }
            for (Iterator<Map.Entry<String, String>> it = allProps.entrySet().iterator(); it.hasNext();) {
                Map.Entry<String, String> entry = it.next();
                msgln(String.format("%1$-" + maxLen + "s", entry.getKey()) + " => " + entry.getValue());
            }
            msgln("");
        }

        // get properties by prefix
        if ((param = cmd.getParam("-prefix")) != null) {
            String serviceName = cmd.getParam("-service");
            if (serviceName == null)
                title("Properties matching prefix:");
            else
                title("Properties matching service name and prefix:");
            SortedSet<String> selectedKeys = new TreeSet<String>();
            int maxLen = 0;
            Properties props = Config.getMatchingProperties(param, serviceName, null);
            for (Enumeration en = props.propertyNames(); en.hasMoreElements();) {
                String key = (String) en.nextElement();
                if (key.length() > maxLen)
                    maxLen = key.length();
                selectedKeys.add(key);
            }
            for (Iterator<String> it = selectedKeys.iterator(); it.hasNext();) {
                String key = it.next();
                msgln(String.format("%1$-" + maxLen + "s", key) + " => " + props.getProperty(key));
            }
            msgln("");
        }

        // show individual properties value
        if (cmd.params.length > 0) {
            int maxLen = 0;
            for (int i = 0; i < cmd.params.length; i++) {
                int len = cmd.params[i].length();
                if (len > maxLen)
                    maxLen = len;
            }
            title("Selected properties:");
            for (int i = 0; i < cmd.params.length; i++) {
                String value = Config.get().getString(cmd.params[i]);
                msgln(String.format("%1$-" + maxLen + "s", cmd.params[i]) + " => "
                        + (value == null ? "<null>" : value));
            }
        }

    } catch (Throwable e) {
        processErrorAndExit(e);
    }
}

From source file:Main.java

public static List sortedUnion(List args1, List args2) {
    SortedSet set = new TreeSet();
    set.addAll(args1);// w  w w.j a v  a2s . co m
    set.addAll(args2);

    List lst = new ArrayList(set.size());
    for (Iterator it = set.iterator(); it.hasNext();) {
        Object o = it.next();
        lst.add(o);
    }

    return lst;
}

From source file:Main.java

public static <T> int compareAsSet(@Nonnull Comparator<? super T> elementComparator,
        @Nonnull Collection<? extends T> list1, @Nonnull Collection<? extends T> list2) {
    int res = Ints.compare(list1.size(), list2.size());
    if (res != 0)
        return res;

    SortedSet<? extends T> set1 = toSortedSet(elementComparator, list1);
    SortedSet<? extends T> set2 = toSortedSet(elementComparator, list2);

    Iterator<? extends T> elements2 = set2.iterator();
    for (T element1 : set1) {
        res = elementComparator.compare(element1, elements2.next());
        if (res != 0)
            return res;
    }//from w ww .ja v  a  2s  .co m
    return 0;
}

From source file:nl.nn.adapterframework.util.GlobalListItem.java

/**
 * Get the system names as an Iterator, alphabetically sorted
 * @return Iterator with the realm names, alphabetically sorted
 *///from   w  w w  .j av a  2s . c  o m
public static Iterator getRegisteredNames() {
    SortedSet sortedKeys = new TreeSet(items.keySet());
    return sortedKeys.iterator();
}

From source file:Main.java

public static <T> int compareAsSet(Comparator<? super T> elementComparator, Collection<? extends T> list1,
        Collection<? extends T> list2) {
    int res = Ints.compare(list1.size(), list2.size());
    if (res != 0)
        return res;

    SortedSet<? extends T> set1 = toSortedSet(elementComparator, list1);
    SortedSet<? extends T> set2 = toSortedSet(elementComparator, list2);

    Iterator<? extends T> elements2 = set2.iterator();
    for (T element1 : set1) {
        res = elementComparator.compare(element1, elements2.next());
        if (res != 0)
            return res;
    }//from  w ww.  ja  v a2  s . c  om
    return 0;
}

From source file:edu.harvard.med.screensaver.util.CollectionUtils.java

public static List<IntRange> splitIntoSequentialRanges(SortedSet<Integer> integers) {
    List<IntRange> ranges = Lists.newArrayList();
    Iterator<Integer> iter = integers.iterator();
    if (iter.hasNext()) {
        IntRange range = new IntRange(iter.next());
        while (iter.hasNext()) {
            int next = iter.next();
            if (next - 1 == range.getMaximumInteger()) {
                range = new IntRange(range.getMinimumInteger(), next);
            } else {
                ranges.add(range);//from   w w  w  . j  a va 2 s . com
                range = new IntRange(next);
            }
        }
        ranges.add(range);
    }
    return ranges;
}

From source file:Main.java

public static <T> T findLastItemAtPosition(SortedSet<T> queryResult, int itemPosition) {
    if (queryResult == null || queryResult.isEmpty())
        throw new IllegalArgumentException("Provided result set is 'null' or empty.");

    Iterator<T> resultSetIterator = queryResult.iterator();
    int nbItemInList = 0;
    T lastItem = null;// w  ww.j av  a 2s.  c  om
    while (resultSetIterator.hasNext() && nbItemInList++ < itemPosition)
        lastItem = resultSetIterator.next();
    return lastItem;
}