Java ListIterator Usage internStringsInList(List list)

Here you can find the source of internStringsInList(List list)

Description

This method interns all the strings in the given list in place.

License

Apache License

Declaration

public static List<String> internStringsInList(List<String> list) 

Method Source Code

//package com.java2s;
/*/*from www  .  ja  v a 2 s .c  o  m*/
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.List;
import java.util.ListIterator;

public class Main {
    /**
     * This method interns all the strings in the given list in place. That is,
     * it iterates over the list, replaces each element with the interned copy
     * and eventually returns the same list.
     *
     * Note that the provided List implementation should return an iterator
     * (via list.listIterator()) method, and that iterator should implement
     * the set(Object) method. That's what all List implementations in the JDK
     * provide. However, if some custom List implementation doesn't have this
     * functionality, this method will return without interning its elements.
     */
    public static List<String> internStringsInList(List<String> list) {
        if (list != null) {
            try {
                ListIterator<String> it = list.listIterator();
                while (it.hasNext()) {
                    it.set(it.next().intern());
                }
            } catch (UnsupportedOperationException e) {
            } // set() not implemented - ignore
        }
        return list;
    }
}

Related

  1. gotoIndex(ListIterator iterator, int index)
  2. incrementalComponentWiseAverageArbitraryDepth(Object average, int n, Object newItems)
  3. indexOf(List list, E object)
  4. insertBefore(List list, E... elements)
  5. intern(ListIterator in)
  6. isEqual(java.util.List list1, java.util.List list2)
  7. iteratorBinarySearchGet(ListIterator i, int index)
  8. lastIndexOf(List list, T element)
  9. listEquals(List left, List right)