如何使用比较器进行重复?
How to use Comparator for duplicates?
我是 Java 的新手,我尝试制作一个比较器,将对象从 Min 排序到 Max,如果有重复项,会将它们发送到列表末尾以获得结果类似于 [1, 2, 3, 1, 2, 3, 1, 2, 3, 等等]。但即使是这个初始变体也不起作用!?我得到一个 ClassCastException,表明无法将 Integer 转换为 TestArr。哪里出错了?这是我的代码:
public class IntraArrayValuesComparison {
public static void main(String[] args) {
ArrayList<Integer> num1 = new ArrayList<Integer>();
num1.add(40);
num1.add(50);
num1.add(50);
num1.add(40);
num1.add(20);
num1.add(10);
num1.add(10);
num1.add(30);
num1.add(20);
num1.add(30);
Collections.sort(num1, new ArraysValuesCompare());
Iterator itr = num1.iterator();
while (itr.hasNext()) {
TestArr st = (TestArr)itr.next();
System.out.println(st.var);
}
System.out.println();
}
}
class TestArr {
int var;
TestArr (int var) {
this.var = var;
}
}
class ArraysValuesCompare implements Comparator{
public int compare (Object o1, Object o2){
TestArr a = (TestArr)o1;
TestArr b = (TestArr)o2;
if (a.var >= b.var)
return 0;
else
return 1;
}
}
你不能用Comparator
。
但是您可以对值进行排序,然后重新排列值,即重复将重复项移动到列表的末尾,就像您说的那样。
public static List<Integer> sortAscendingRepeated(Integer... values) {
List<Integer> list = new ArrayList<>(Arrays.asList(values));
Collections.sort(list);
for (int i = 1; i < list.size(); i++)
if (list.get(i).equals(list.get(i - 1)))
list.add(list.remove(i--));
return list;
}
测试
System.out.println(sortAscendingRepeated(40, 50, 50, 40, 20, 10, 10, 30, 20, 30));
System.out.println(sortAscendingRepeated(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 5, 4));
输出
[10, 20, 30, 40, 50, 10, 20, 30, 40, 50]
[1, 2, 3, 4, 5, 1, 2, 3, 4, 1, 2, 3]
直方图可以用来合并统计。
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class IntraArrayValuesComparison {
private static void sort(List<TestArr> num1) {
Map<TestArr, Integer> histogram = new TreeMap<TestArr, Integer>();
for(TestArr test : num1) {
Integer count = histogram.get(test);
if(count == null)
count = 0;
histogram.put(test, ++count);
}
num1.clear();
while(!histogram.isEmpty()) {
Iterator<TestArr> itr = histogram.keySet().iterator();
while (itr.hasNext()) {
TestArr st = itr.next();
Integer count = histogram.get(st);
if(count > 0) {
num1.add(st);
histogram.put(st, --count);
continue;
}
itr.remove();
}
}
}
public static void main(String[] args) {
List<TestArr> num1 = new ArrayList<>();
num1.add(new TestArr(40));
num1.add(new TestArr(50));
num1.add(new TestArr(50));
num1.add(new TestArr(40));
num1.add(new TestArr(20));
num1.add(new TestArr(10));
num1.add(new TestArr(10));
num1.add(new TestArr(30));
num1.add(new TestArr(20));
num1.add(new TestArr(30));
sort(num1);
Iterator<TestArr> itr = num1.iterator();
while (itr.hasNext()) {
TestArr st = itr.next();
System.out.println(st.var);
}
System.out.println();
}
}
class TestArr implements Comparable<TestArr> {
Integer var;
TestArr (int var) {
this.var = var;
}
@Override
public int compareTo(TestArr o) {
if(o == null)
return -1;
return var.compareTo(o.var);
}
}
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
public class IntraArrayValuesComparison {
private static boolean isInRange(Integer st, Integer val) {
final int DELTA = 5;
return val.intValue() >= (st.intValue() - DELTA) && val.intValue() <= (st.intValue() + DELTA);
}
private static List<Integer> getDeckWithLessCards(List<List<Integer> > resultDecks) {
if(resultDecks.isEmpty())
return new ArrayList<>();
List<Integer> cards = resultDecks.get(0);
for(int i= 1; i < resultDecks.size(); ++i) {
if(resultDecks.get(i).size() < cards.size())
cards = resultDecks.get(i);
}
return cards;
}
private static Collection<List<Integer> > sort(List<Integer> deck, Integer splitSize) {
Map<Integer, List<Integer>> decks = new TreeMap<>();
outerLoop:
for(Integer card : deck) {
List<Integer> cards = new ArrayList<>();
Iterator<Integer> itr = decks.keySet().iterator();
while (itr.hasNext()) {
Integer masterCard = itr.next();
if(isInRange(card, masterCard)) {
decks.get(masterCard).add(card);
continue outerLoop;
}
}
decks.put(card, cards);
decks.get(card).add(card);
}
System.out.println("Arranged decks:\t" + decks.values());
//do transpose
List<List<Integer> > resultDecks = new ArrayList<>();
if(splitSize == null || splitSize < 2)
splitSize = decks.size();
for(int i= 0; i < splitSize; ++i)
resultDecks.add(new ArrayList<>());
boolean lastCard = false;
while(!decks.isEmpty()) {
Iterator<Integer> itr = decks.keySet().iterator();
if (itr.hasNext()) {
Integer masterCard = itr.next();
List<Integer> cards = decks.get(masterCard);
Integer card = Collections.min(cards);
cards.remove(card);
boolean uno = cards.isEmpty();
if(uno) {
lastCard = true;
getDeckWithLessCards(resultDecks).add(card);
itr.remove();
}
else if(lastCard) {
resultDecks.get(0).add(card);
lastCard = false;
}
else
getDeckWithLessCards(resultDecks).add(card);
}
}
return resultDecks;
}
public static void main(String[] args) {
String[] data = null;
System.out.print("Please input data (1, 5, 10, 2, 15, 3, 20): ");
try (Scanner scanner = new Scanner(System.in)) {
data = scanner.nextLine().split(",");
} catch(Exception ex) {
System.out.println("\nIncorrect data format.");
return;
}
if(data == null || data.length < 2) {
System.out.println("Incorrect data format.");
return;
}
List<Integer> deck = new ArrayList<>();
for(String value : data)
deck.add(new Integer(value.trim()));
Collection<List<Integer> > resultDecks = sort(deck, 3);
System.out.println("Final decks:\t" + resultDecks);
System.out.println();
}
}
我是 Java 的新手,我尝试制作一个比较器,将对象从 Min 排序到 Max,如果有重复项,会将它们发送到列表末尾以获得结果类似于 [1, 2, 3, 1, 2, 3, 1, 2, 3, 等等]。但即使是这个初始变体也不起作用!?我得到一个 ClassCastException,表明无法将 Integer 转换为 TestArr。哪里出错了?这是我的代码:
public class IntraArrayValuesComparison {
public static void main(String[] args) {
ArrayList<Integer> num1 = new ArrayList<Integer>();
num1.add(40);
num1.add(50);
num1.add(50);
num1.add(40);
num1.add(20);
num1.add(10);
num1.add(10);
num1.add(30);
num1.add(20);
num1.add(30);
Collections.sort(num1, new ArraysValuesCompare());
Iterator itr = num1.iterator();
while (itr.hasNext()) {
TestArr st = (TestArr)itr.next();
System.out.println(st.var);
}
System.out.println();
}
}
class TestArr {
int var;
TestArr (int var) {
this.var = var;
}
}
class ArraysValuesCompare implements Comparator{
public int compare (Object o1, Object o2){
TestArr a = (TestArr)o1;
TestArr b = (TestArr)o2;
if (a.var >= b.var)
return 0;
else
return 1;
}
}
你不能用Comparator
。
但是您可以对值进行排序,然后重新排列值,即重复将重复项移动到列表的末尾,就像您说的那样。
public static List<Integer> sortAscendingRepeated(Integer... values) {
List<Integer> list = new ArrayList<>(Arrays.asList(values));
Collections.sort(list);
for (int i = 1; i < list.size(); i++)
if (list.get(i).equals(list.get(i - 1)))
list.add(list.remove(i--));
return list;
}
测试
System.out.println(sortAscendingRepeated(40, 50, 50, 40, 20, 10, 10, 30, 20, 30));
System.out.println(sortAscendingRepeated(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 5, 4));
输出
[10, 20, 30, 40, 50, 10, 20, 30, 40, 50]
[1, 2, 3, 4, 5, 1, 2, 3, 4, 1, 2, 3]
直方图可以用来合并统计。
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class IntraArrayValuesComparison {
private static void sort(List<TestArr> num1) {
Map<TestArr, Integer> histogram = new TreeMap<TestArr, Integer>();
for(TestArr test : num1) {
Integer count = histogram.get(test);
if(count == null)
count = 0;
histogram.put(test, ++count);
}
num1.clear();
while(!histogram.isEmpty()) {
Iterator<TestArr> itr = histogram.keySet().iterator();
while (itr.hasNext()) {
TestArr st = itr.next();
Integer count = histogram.get(st);
if(count > 0) {
num1.add(st);
histogram.put(st, --count);
continue;
}
itr.remove();
}
}
}
public static void main(String[] args) {
List<TestArr> num1 = new ArrayList<>();
num1.add(new TestArr(40));
num1.add(new TestArr(50));
num1.add(new TestArr(50));
num1.add(new TestArr(40));
num1.add(new TestArr(20));
num1.add(new TestArr(10));
num1.add(new TestArr(10));
num1.add(new TestArr(30));
num1.add(new TestArr(20));
num1.add(new TestArr(30));
sort(num1);
Iterator<TestArr> itr = num1.iterator();
while (itr.hasNext()) {
TestArr st = itr.next();
System.out.println(st.var);
}
System.out.println();
}
}
class TestArr implements Comparable<TestArr> {
Integer var;
TestArr (int var) {
this.var = var;
}
@Override
public int compareTo(TestArr o) {
if(o == null)
return -1;
return var.compareTo(o.var);
}
}
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
public class IntraArrayValuesComparison {
private static boolean isInRange(Integer st, Integer val) {
final int DELTA = 5;
return val.intValue() >= (st.intValue() - DELTA) && val.intValue() <= (st.intValue() + DELTA);
}
private static List<Integer> getDeckWithLessCards(List<List<Integer> > resultDecks) {
if(resultDecks.isEmpty())
return new ArrayList<>();
List<Integer> cards = resultDecks.get(0);
for(int i= 1; i < resultDecks.size(); ++i) {
if(resultDecks.get(i).size() < cards.size())
cards = resultDecks.get(i);
}
return cards;
}
private static Collection<List<Integer> > sort(List<Integer> deck, Integer splitSize) {
Map<Integer, List<Integer>> decks = new TreeMap<>();
outerLoop:
for(Integer card : deck) {
List<Integer> cards = new ArrayList<>();
Iterator<Integer> itr = decks.keySet().iterator();
while (itr.hasNext()) {
Integer masterCard = itr.next();
if(isInRange(card, masterCard)) {
decks.get(masterCard).add(card);
continue outerLoop;
}
}
decks.put(card, cards);
decks.get(card).add(card);
}
System.out.println("Arranged decks:\t" + decks.values());
//do transpose
List<List<Integer> > resultDecks = new ArrayList<>();
if(splitSize == null || splitSize < 2)
splitSize = decks.size();
for(int i= 0; i < splitSize; ++i)
resultDecks.add(new ArrayList<>());
boolean lastCard = false;
while(!decks.isEmpty()) {
Iterator<Integer> itr = decks.keySet().iterator();
if (itr.hasNext()) {
Integer masterCard = itr.next();
List<Integer> cards = decks.get(masterCard);
Integer card = Collections.min(cards);
cards.remove(card);
boolean uno = cards.isEmpty();
if(uno) {
lastCard = true;
getDeckWithLessCards(resultDecks).add(card);
itr.remove();
}
else if(lastCard) {
resultDecks.get(0).add(card);
lastCard = false;
}
else
getDeckWithLessCards(resultDecks).add(card);
}
}
return resultDecks;
}
public static void main(String[] args) {
String[] data = null;
System.out.print("Please input data (1, 5, 10, 2, 15, 3, 20): ");
try (Scanner scanner = new Scanner(System.in)) {
data = scanner.nextLine().split(",");
} catch(Exception ex) {
System.out.println("\nIncorrect data format.");
return;
}
if(data == null || data.length < 2) {
System.out.println("Incorrect data format.");
return;
}
List<Integer> deck = new ArrayList<>();
for(String value : data)
deck.add(new Integer(value.trim()));
Collection<List<Integer> > resultDecks = sort(deck, 3);
System.out.println("Final decks:\t" + resultDecks);
System.out.println();
}
}