Here you can find the source of changeComparator(TreeSet
Parameter | Description |
---|---|
T | type |
set | set which is cleared and whose elements should contain the newly create one (which is returned) |
cmp | comparator |
public static <T> TreeSet<T> changeComparator(TreeSet<T> set, Comparator<? super T> cmp)
//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; } }