comparator 相关问题

用于创建对象的Common接口,用于指定比较其他对象的方式。在实现繁重的问题上使用此标记时 - 标记实现编写的代码语言。

java中多个属性的自定义排序

我有以下自定义代码排序方法,要求是我们必须根据 BoardPoint、OffPoint、LastName 和 FirstName 进行自定义排序。 首先根据 BoardPoint 排序 如果

回答 1 投票 0

java - 从语义上比较不同类型的数字

假设我有三个数字: 可比较 n1 = new Integer(432); 可比较 n2 = new Long(40); 可比较 n3 = new Double(500.12); 我想通过 Comparable 接口来比较这些数字。

回答 4 投票 0

为什么我的比较器在插入一组时会被调用两次?

我试图了解比较器在 cpp 中是如何工作的。因此,当我插入 s1 时,不会调用比较器运算符,这是有道理的。但是当插入 s2 时,比较器运算符被校准...

回答 2 投票 0

如何使用java通过比较子列表元素来对父列表进行排序和排名? [已关闭]

我需要根据给出的科目优先顺序找到学生排名。学生列表包含带有分数的科目列表。主题优先顺序列表中有我们应该有的优先顺序...

回答 1 投票 0

在priority_queue中使用更大的比较器存储自定义类的指针

我有一个像这样的priority_queue,而不是使用less比较(priority_queue的默认值)。我想使用更大的<>。所以我像这样调整代码 优先队列 我有一个像这样的priority_queue,而不是使用less比较(priority_queue的默认值)。我想使用更大的<>。所以我像这样调整代码 priority_queue<EncodingNode*, vector<EncodingNode*>, greater<Comparator>> pq; 这是我的EncodingNode的定义 class EncodingNode { public: char c; int freq; int priority; }; 这是我的比较器类: struct Comparator { bool operator()(const EncodingNode* a, const EncodingNode* b); }; bool Comparator::operator()(const EncodingNode* a, const EncodingNode* b) { // logic for comparing two EncodingNode* if (a->freq > b->freq) return true; else if (a->freq == b->freq) { if (a->priority > b->priority) return true; } return false; } 它一直给我这个错误:无法将参数 1 从 'EncodingNode *' 转换为 'const_Ty &' 无论如何,有没有办法解决这个问题,如果我删除代码的大部分,它会运行,但不使用更大的<>,我必须以某种方式否定比较器内的条件并给出错误的结果。在这种情况下是否可以使用更大的<>。非常感谢。 std::greater 将 (x, y) 转换为 x > y,因为 Comparator 没有实现 > 运算符 greater<Comparator> 无法编译。在这种情况下你不需要std::greater,你只需直接传递比较器即可: priority_queue<EncodingNode*, vector<EncodingNode*>, Comparator> pq; 请注意,您的比较器可以简化为: bool Comparator::operator()(const EncodingNode* a, const EncodingNode* b) { return std::tie(a->freq, a->priority) > std::tie(b->freq, b->priority); }

回答 1 投票 0

比较然后比较给出编译错误

我正在尝试使用 Java8 比较器按姓名和年龄对员工列表进行排序,我在下面创建了比较器,但它给了我一个编译器错误 类型不匹配:无法从比较器转换<...

回答 1 投票 0

Delphi 字符串变量比较是否使用 loInvariantLocale?如何在本地启用 loUserLocale?

我的问题源于DevExpress TcxGrid默认比较(用于数据排序),归结为代码(在cxVariants.pas中实现): 如果 VarIsEmpty(V1) 那么 如果 VarIsEmpty...

回答 1 投票 0

一个列表充当对另一个列表进行排序的模板

假设有两个列表: 列表 listA = Arrays.asList(1,2,3,4,5,6,7,6,5,4,3,2,1,0); 该列表是用于对另一个列表B进行排序的模板。 列表 listB = Arrays.asList(0,106,107,101,105,102,102,103,10...

回答 1 投票 0

参考:集合中比较器的不变性

考虑以下 Java 示例: int[] a = new int[]{1, 2}; PriorityQueue pq = new PriorityQueue(Comparator.comparingInt(i -> a[i])); pq.add(0); pq.add(1); 一个[1]...

回答 1 投票 0

Java 比较问题 - 比较方法违反了其一般契约

我正在尝试对一些数字进行排序。我收到“java.lang.IllegalArgumentException:比较方法违反了其一般契约!”当我执行以下命令时出现异常...

回答 1 投票 0

Java 比较器,比较两个双精度数:

我使用Java 17。这是我的代码: 公共静态类 SortByPercentLike 实现 VideoSorterInterface { // 定义Lambda函数 比较器 videoPercentLikeCompara...

回答 1 投票 0

ExoPlayer TrackSelectionDialog 比较器不起作用

我正在以“height”+“p”格式向用户打印视频轨道。 默认情况下,TrackSelectionDialog 以随机顺序打印它们。我想将它们从最小到最大重新排序。 所以...

回答 1 投票 0

Java 获取 String CompareTo 作为比较器对象

我想通过 String.CompareTo 比较器对静态字符串数组进行排序和二分搜索。 问题是排序和二分查找都需要 Comparator 对象...

回答 13 投票 0

如何在Java中使用自定义比较器来定义嵌套映射的最内层映射?

我在Java中有一个Range类和一个比较器: 类范围{ 开始; 打算; } 类 RangeComparator 实现 Comparator { @覆盖 公共 int 比较(范围 range1,...

回答 1 投票 0

如何比较Java中类的两个不同泛型实例化?

MRE 因为好吧,作业……叹息 这是我陷入困境的问题: 考虑下面的类: 类 MyClass> 实现 Comparable>...

回答 1 投票 0

如何使用同时使用键和值的比较器对树形图进行排序

所以我在这个类中尝试使用我在另一个类(单独的文件)中创建的比较器来初始化 SortedMap 为什么这部分不起作用? 比较器 所以我在这个类中尝试使用我在另一个类(单独的文件)中创建的比较器来初始化 SortedMap 为什么这部分不起作用? Comparator<Map.Entry<Country, Pair<Integer, Integer>>> comparator = new SortCountryChargers(); countryChargers = new TreeMap<Country, Pair<Integer, Integer>>(comparator); 文件1: public class Ex4 { private static SortedMap<Country, Pair<Integer, Integer>> countryChargers; public static void setCountryChargers(Set<ChargingStation> chargingStationSet, int Kw){ Comparator<Map.Entry<Country, Pair<Integer, Integer>>> comparator = new SortCountryChargers(); countryChargers = new TreeMap<Country, Pair<Integer, Integer>>(comparator); for(ChargingStation chargingStation : chargingStationSet){ // get the charging station's country Country country = chargingStation.getCountry(); // check if the country is already part of the hashmap // if not, add it if(!countryChargers.containsKey(country)){ Pair<Integer, Integer> newPair = new Pair<>(0,0); countryChargers.put(country, newPair); } // update the hashmap // the first member of the pair is the charging stations > kw // the second member is the charging stations <= kw // first + second = total if(chargingStation.getkW() > Kw){ int increment = countryChargers.get(country).getFirst() + 1 ; countryChargers.get(country).setFirst(increment); } else { int increment = countryChargers.get(country).getSecond() + 1 ; countryChargers.get(country).setSecond(increment); } } } } 文件2: public class SortCountryChargers implements Comparator<Map.Entry<Country, Pair<Integer, Integer>>> { public int compare(Map.Entry<Country,Pair<Integer, Integer>> object1, Map.Entry<Country,Pair<Integer, Integer>> object2){ //get the total charging station num for objects 1 and 2 int pair1 = sumPairs(object1.getValue()); int pair2 = sumPairs(object2.getValue()); //compare total chargig station num if(pair1 > pair2) return 1; else if (pair1 < pair2) return -1; //if the total charging station num is equal, compare country names String country1 = object1.getKey().getName(); String country2 = object2.getKey().getName(); return country1.compareTo(country2); } //get the sum of the two members of an <integer, interger> pair public int sumPairs(Pair<Integer, Integer> p){ return p.getFirst() + p.getSecond(); } } 我尝试阅读多篇文章,但到目前为止我还没有找到答案。 A TreeMap 只能 根据其键进行排序,而不是根据其条目进行排序。您正在尝试使用 Comparator<Map.Entry<County, Pair<Integer, Integer>>>,但这不是 TreeMap 的工作原理。 (另一方面,它会接受 Comparator<Country>。) 如果您想根据键和值对条目进行排序,则无法在地图中内置这样做;您必须在地图的 entrySet 上单独进行操作。

回答 1 投票 0

无法解析构造函数“TreeSet(ComparatorByPrice)”

我在使用比较器的代码中遇到了这个问题,我在另一个类中实现了工具,一切都很好......但是在我调用比较器的这个类中,消息“不能

回答 1 投票 0

如何使用同时使用键和值的比较器对树形图进行排序(Java)

所以我在这个类中尝试使用我在另一个类(单独的文件)中创建的比较器来初始化 SortedMap 为什么这部分不起作用? 比较器 所以我在这个类中尝试使用我在另一个类(单独的文件)中创建的比较器来初始化 SortedMap 为什么这部分不起作用? Comparator<Map.Entry<Country, Pair<Integer, Integer>>> comparator = new SortCountryChargers(); countryChargers = new TreeMap<Country, Pair<Integer, Integer>>(comparator); 文件1: public class Ex4 { private static SortedMap<Country, Pair<Integer, Integer>> countryChargers; public static void setCountryChargers(Set<ChargingStation> chargingStationSet, int Kw){ Comparator<Map.Entry<Country, Pair<Integer, Integer>>> comparator = new SortCountryChargers(); countryChargers = new TreeMap<Country, Pair<Integer, Integer>>(comparator); for(ChargingStation chargingStation : chargingStationSet){ // get the charging station's country Country country = chargingStation.getCountry(); // check if the country is already part of the hashmap // if not, add it if(!countryChargers.containsKey(country)){ Pair<Integer, Integer> newPair = new Pair<>(0,0); countryChargers.put(country, newPair); } // update the hashmap // the first member of the pair is the charging stations > kw // the second member is the charging stations <= kw // first + second = total if(chargingStation.getkW() > Kw){ int increment = countryChargers.get(country).getFirst() + 1 ; countryChargers.get(country).setFirst(increment); } else { int increment = countryChargers.get(country).getSecond() + 1 ; countryChargers.get(country).setSecond(increment); } } } } 文件2: public class SortCountryChargers implements Comparator<Map.Entry<Country, Pair<Integer, Integer>>> { public int compare(Map.Entry<Country,Pair<Integer, Integer>> object1, Map.Entry<Country,Pair<Integer, Integer>> object2){ //get the total charging station num for objects 1 and 2 int pair1 = sumPairs(object1.getValue()); int pair2 = sumPairs(object2.getValue()); //compare total chargig station num if(pair1 > pair2) return 1; else if (pair1 < pair2) return -1; //if the total charging station num is equal, compare country names String country1 = object1.getKey().getName(); String country2 = object2.getKey().getName(); return country1.compareTo(country2); } //get the sum of the two members of an <integer, interger> pair public int sumPairs(Pair<Integer, Integer> p){ return p.getFirst() + p.getSecond(); } } 我尝试阅读多篇文章,但到目前为止我还没有找到答案。如果可以的话请帮帮我! A TreeMap 只能 根据其键进行排序,而不是根据其条目进行排序。您正在尝试使用 Comparator<Map.Entry<County, Pair<Integer, Integer>>>,但这不是 TreeMap 的工作原理。 如果您想根据键和值对条目进行排序,则无法在地图中内置这样做;您必须在地图的 entrySet 上单独进行操作。

回答 1 投票 0

SQL 中“LIKE”和“=”有什么区别?

以下之间有什么区别吗? 从用户中选择*,其中用户名=“davyjones” 和 从用户名中选择*,其中用户名类似于“davyjones”

回答 14 投票 0

使用Comparator.nullsFirst不与reverse()一起使用

我试图按三个值对汽车进行排序:首先按类型,然后在“类型”存储桶中我想按价格(从最低到最高)对它们进行排序,然后如果有任何价格相同的,我。 ..

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.