Example usage for java.util List listIterator

List of usage examples for java.util List listIterator

Introduction

In this page you can find the example usage for java.util List listIterator.

Prototype

ListIterator<E> listIterator();

Source Link

Document

Returns a list iterator over the elements in this list (in proper sequence).

Usage

From source file:org.deegree.tools.alkis.BackReferenceFixer.java

public static void main(String[] args) {
    Options opts = initOptions();/*from   ww w .j a v  a 2 s. c  o m*/
    FileInputStream fis = null;
    FileOutputStream fos = null;
    try {
        CommandLine line = new PosixParser().parse(opts, args);
        String input = line.getOptionValue('i');
        String output = line.getOptionValue('o');
        String schema = line.getOptionValue('s');
        fis = new FileInputStream(input);
        fos = new FileOutputStream(output);
        XMLInputFactory xifac = XMLInputFactory.newInstance();
        XMLOutputFactory xofac = XMLOutputFactory.newInstance();
        XMLStreamReader xreader = xifac.createXMLStreamReader(input, fis);
        IndentingXMLStreamWriter xwriter = new IndentingXMLStreamWriter(xofac.createXMLStreamWriter(fos));
        GMLStreamReader reader = GMLInputFactory.createGMLStreamReader(GMLVersion.GML_32, xreader);

        AppSchema appSchema = new GMLAppSchemaReader(null, null, schema).extractAppSchema();
        reader.setApplicationSchema(appSchema);

        GMLStreamWriter writer = GMLOutputFactory.createGMLStreamWriter(GMLVersion.GML_32, xwriter);
        XlinkedObjectsHandler handler = new XlinkedObjectsHandler(true, null, new GmlXlinkOptions());
        writer.setReferenceResolveStrategy(handler);

        QName prop = new QName(ns601, "dientZurDarstellungVon");

        Map<String, List<String>> refs = new HashMap<String, List<String>>();
        Map<String, List<String>> types = new HashMap<String, List<String>>();
        Map<String, String> bindings = null;

        for (Feature f : reader.readFeatureCollectionStream()) {
            if (bindings == null) {
                bindings = f.getType().getSchema().getNamespaceBindings();
            }
            for (Property p : f.getProperties(prop)) {
                FeatureReference ref = (FeatureReference) p.getValue();
                List<String> list = refs.get(ref.getId());
                if (list == null) {
                    list = new ArrayList<String>();
                    refs.put(ref.getId(), list);
                }
                list.add(f.getId());
                list = types.get(ref.getId());
                if (list == null) {
                    list = new ArrayList<String>();
                    types.put(ref.getId(), list);
                }
                list.add("inversZu_dientZurDarstellungVon_" + f.getType().getName().getLocalPart());
            }
        }

        QName[] inversePropNames = new QName[] {
                new QName(ns601, "inversZu_dientZurDarstellungVon_AP_Darstellung"),
                new QName(ns601, "inversZu_dientZurDarstellungVon_AP_LTO"),
                new QName(ns601, "inversZu_dientZurDarstellungVon_AP_PTO"),
                new QName(ns601, "inversZu_dientZurDarstellungVon_AP_FPO"),
                new QName(ns601, "inversZu_dientZurDarstellungVon_AP_KPO_3D"),
                new QName(ns601, "inversZu_dientZurDarstellungVon_AP_LPO"),
                new QName(ns601, "inversZu_dientZurDarstellungVon_AP_PPO") };

        reader.close();
        fis.close();
        writer.setNamespaceBindings(bindings);

        fis = new FileInputStream(input);
        xreader = xifac.createXMLStreamReader(input, fis);
        reader = GMLInputFactory.createGMLStreamReader(GMLVersion.GML_32, xreader);
        reader.setApplicationSchema(appSchema);

        if (bindings != null) {
            for (Map.Entry<String, String> e : bindings.entrySet()) {
                if (!e.getKey().isEmpty()) {
                    xwriter.setPrefix(e.getValue(), e.getKey());
                }
            }
        }
        xwriter.writeStartDocument();
        xwriter.setPrefix("gml", "http://www.opengis.net/gml/3.2");
        xwriter.writeStartElement("http://www.opengis.net/gml/3.2", "FeatureCollection");
        xwriter.writeNamespace("gml", "http://www.opengis.net/gml/3.2");

        GmlDocumentIdContext ctx = new GmlDocumentIdContext(GMLVersion.GML_32);

        for (Feature f : reader.readFeatureCollectionStream()) {
            if (refs.containsKey(f.getId())) {
                List<Property> props = new ArrayList<Property>(f.getProperties());
                ListIterator<Property> iter = props.listIterator();
                String name = iter.next().getName().getLocalPart();
                while (name.equals("lebenszeitintervall") || name.equals("modellart") || name.equals("anlass")
                        || name.equals("zeigtAufExternes") || name.equals("istTeilVon")
                        || name.equals("identifier")) {
                    if (iter.hasNext()) {
                        name = iter.next().getName().getLocalPart();
                    } else {
                        break;
                    }
                }
                if (iter.hasPrevious()) {
                    iter.previous();
                }
                for (QName propName : inversePropNames) {
                    Iterator<String> idIter = refs.get(f.getId()).iterator();
                    Iterator<String> typeIter = types.get(f.getId()).iterator();
                    while (idIter.hasNext()) {
                        String id = idIter.next();
                        if (typeIter.next().equals(propName.getLocalPart())) {
                            PropertyType pt = f.getType().getPropertyDeclaration(propName);
                            Property p = new GenericProperty(pt, new FeatureReference(ctx, "#" + id, null));
                            iter.add(p);
                        }
                    }
                }
                f.setProperties(props);

            }
            xwriter.writeStartElement("http://www.opengis.net/gml/3.2", "featureMember");
            writer.write(f);
            xwriter.writeEndElement();
        }

        xwriter.writeEndElement();
        xwriter.close();
    } catch (Throwable e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(fis);
        IOUtils.closeQuietly(fos);
    }
}

From source file:Main.java

public static void main(String[] args) {
    List<Integer> numbers = new ArrayList<Integer>();

    for (Integer i : Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7))
        numbers.add(i);//from   w  w  w  .ja va 2  s  .c om
    printList(numbers); // 0,1,2,3,4,5,6,7

    // replaces each element with twice its value
    for (int index = 0; index < numbers.size(); index++) {
        numbers.set(index, numbers.get(index) * 2);
    }
    printList(numbers);

    // does nothing because list is not being changed
    for (Integer number : numbers) {
        number++;
    }
    printList(numbers);

    // same as above -- just different syntax
    for (Iterator<Integer> iter = numbers.iterator(); iter.hasNext();) {
        Integer number = iter.next();
        number++;
    }
    printList(numbers);

    for (ListIterator<Integer> iter = numbers.listIterator(); iter.hasNext();) {
        Integer number = iter.next();
        iter.add(number + 1);
    }
    printList(numbers);

    for (Iterator<Integer> iter = numbers.iterator(); iter.hasNext();) {
        Integer number = iter.next();
        if (number % 2 == 0) // if number is even
            iter.remove(); // remove it from the collection
    }
    printList(numbers); // 1,3,5,7,9,11,13,15

    // ListIterator<?> has a "set" method to replace elements
    for (ListIterator<Integer> iter = numbers.listIterator(); iter.hasNext();) {
        Integer number = iter.next();
        iter.set(number / 2); // divide each element by 2
    }
    printList(numbers); // 0,1,2,3,4,5,6,7

    // Use Java 8 Lambda
    List<Integer> list = numbers;
    list.stream().forEach(elem -> System.out.println("element " + elem));

}

From source file:MainClass.java

static void listTrim(List<String> strings) {
    for (ListIterator<String> lit = strings.listIterator(); lit.hasNext();) {
        lit.set(lit.next().trim());//from   ww w .  j a v a 2s .  c  om
    }
}

From source file:Main.java

public static <V> void removeDuplicates(List<V> lst) {
    for (ListIterator<V> it = lst.listIterator(); it.hasNext();) {
        int idx = it.nextIndex();
        V elem = it.next();/*from   w  ww .  j av  a2 s  . c  o m*/
        if (idx < lst.size() - 1 && lst.subList(idx + 1, lst.size() - 1).contains(elem))
            it.remove();
    }
}

From source file:Main.java

public static long iterAdd(List list) {
    ListIterator iter = list.listIterator();
    long start, stop, result = 0;
    for (int i = 0; i < 100; i++) {
        start = System.nanoTime();
        iter.add(list.size() + 1 + i);//from w w  w .j  a v a  2  s  .  c o m
        stop = System.nanoTime();
        result += stop - start;
    }
    return result / 100;
}

From source file:Main.java

public static long iterRemove(List list) {
    ListIterator iter = list.listIterator();
    long start, stop, result = 0;
    for (int i = 0; i < 100; i++) {
        iter.next();// www.  j  a v  a2  s  . c om
        start = System.nanoTime();
        iter.remove();
        stop = System.nanoTime();
        result += stop - start;
    }
    return result / 100;
}

From source file:Main.java

public static <E> int indexOf(List<E> c, E item, BiPredicate<? super E, ? super E> equalTester) {
    ListIterator<E> iter = c.listIterator();
    while (iter.hasNext()) {
        int index = iter.nextIndex();
        if (equalTester.test(iter.next(), item)) {
            return index;
        }/*from  w  w w.  j  ava 2 s  . c  o m*/
    }
    return -1;
}

From source file:Main.java

/**
 * Removed null elements in-place.//from   w ww.  j  a va2 s. com
 * 
 * @param <T>
 * @param col
 */
public static <T> void clearNulls(List<T> col) {
    ListIterator<T> iter = col.listIterator();
    while (iter.hasNext()) {
        if (iter.next() == null) {
            iter.remove();
        }
    }
}

From source file:org.codice.ddf.configuration.PropertyResolver.java

/**
 * Returns a List with variables replaced by system property values if they exist
 *
 * @return//from  www .  java2 s .  co m
 */
public static List<String> resolveProperties(List<String> list) {
    for (final ListIterator<String> i = list.listIterator(); i.hasNext();) {
        final String item = StrSubstitutor.replaceSystemProperties(i.next());
        i.set(item);
    }
    return list;
}

From source file:Main.java

public static <T> boolean listEquals(List<T> left, List<T> right) {
    if (left == right)
        return true;

    ListIterator<T> leftIterator = left.listIterator();
    ListIterator<T> rightIterator = right.listIterator();

    while (leftIterator.hasNext() && rightIterator.hasNext()) {
        T leftElement = leftIterator.next();
        T rightElement = rightIterator.next();
        if (!(leftElement == null ? rightElement == null : leftElement.equals(rightElement)))
            return false;
    }/* w w w  .  j  a v a  2 s.c o  m*/
    return !(leftIterator.hasNext() || rightIterator.hasNext());
}