Java TreeSet changeComparator(TreeSet set, Comparator cmp)

Here you can find the source of changeComparator(TreeSet set, Comparator cmp)

Description

Clears the initial set, and creates a new one with same contents and has the specified comparator.

License

Open Source License

Parameter

Parameter Description
T type
set set which is cleared and whose elements should contain the newly create one (which is returned)
cmp comparator

Return

copy of the specified set with the specified comparator

Declaration

public static <T> TreeSet<T> changeComparator(TreeSet<T> set, Comparator<? super T> cmp) 

Method Source Code

//package com.java2s;
/*/*from  w ww. j a  va2 s .  c  om*/
 * SetUtils.java
 * 
 * Copyright (C) 2009 Leo Osvald <leo.osvald@gmail.com>
 * 
 * This file is part of SGLJ.
 * 
 * SGLJ is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * SGLJ is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.Comparator;

import java.util.TreeSet;

public class Main {
    /**
     * Clears the initial set, and creates a new one with same contents
     * and has the specified comparator.
     * @param <T> type
     * @param set set which is cleared and whose elements should contain
     * the newly create one (which is returned)
     * @param cmp comparator
     * @return copy of the specified set with the specified comparator
     */
    public static <T> TreeSet<T> changeComparator(TreeSet<T> set, Comparator<? super T> cmp) {
        TreeSet<T> ret = new TreeSet<T>(cmp);
        ret.addAll(set);
        set.clear();
        return ret;
    }
}

Related

  1. addSplits(TreeSet splitKeys, byte[] prefix, int splitBits)
  2. buildTreeSet(Collection elements)
  3. concatenate(TreeSet in)
  4. convertSpanToSparseGrid(final int curIx, final int span, final TreeSet indexes)
  5. createTreeSet(Iterable c)
  6. cvtListToTreeSet(List list)