sortedset

How to find the index of an element in a TreeSet?

末鹿安然 提交于 2019-12-17 18:34:25
问题 I'm using a TreeSet<Integer> and I'd quite simply like to find the index of a number in the set. Is there a nice way to do this that actually makes use of the O(log(n)) complexity of binary trees? (If not, what should I do, and does anyone know why not? I'm curious why such a class would be included in Java without something like a search function.) 回答1: As @Yrlec points out set.headSet(element).size will returns 0 though there is no this element in the set. So we'd better check: return set

Returning a SortedSet [closed]

情到浓时终转凉″ 提交于 2019-12-12 03:25:33
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 years ago . When I run the following code: Student student1 = new Student("Billy", 13); Student student2 = new Student("Bob", 12); Student student3 = new Student("Belle", 11); Student student4 = new Student("Barry", 10); Student student5 = new Student("Brian", 10); Student student6 = new Student("Bane", 13); Collection

Sorting values with Comparator changes all the values with that object

纵饮孤独 提交于 2019-12-11 13:49:45
问题 I am working in an android application I want to sort a List of Objects with an Object Property . I have sorted it successfully but when I sort it all the List with that object changes the value to same as the sorted value Please look into ma code : SortedSet<Caseload> removeDuplicateClientName = new TreeSet<Caseload>( new Comparator<Caseload>() { @Override public int compare(Caseload caseload0, Caseload caseload1) { return caseload0.ClientName.compareTo(caseload1.ClientName); } }); //

SortedSet - custom order when storing a class object

偶尔善良 提交于 2019-12-11 03:15:42
问题 I'm looking at replacing a HashSet with a SortedSet because it is better suited to the data I'm storing. However, all the examples I've seen so far relate to storing simple objects - int's, strings etc. I want to implement this for a custom class with a number of properties, however the class also includes a date that I want to use as the 'indexer'. The question is how do I go about declaring a custom indexer for the set to use which will override the default behaviour? Thanks in advance. 回答1

GSON: Serialize object with java.util.TreeSet

安稳与你 提交于 2019-12-10 16:15:29
问题 How can I serialize a TreeSet properly? In order to give you an idea of what's not working I've set up this little demo project. The main goal is to print a JSON string of my QData object. App.java package de.company.gsonserializer; import java.util.ArrayList; import java.util.Comparator; import java.util.HashMap; import java.util.Map; import java.util.SortedSet; import java.util.TreeSet; public class App { public static void main( String[] args ) { QData qdata = new QData(); ArrayList<LData>

how can I pass infinity to redis from python?

佐手、 提交于 2019-12-10 15:29:36
问题 I'm using redis-py and want to use -inf and inf with ZRANGEBYSCORE. I tried to do this using string and float of inf but those return an empty set. How can I do this? EDIT I tried doing the following command: redis.StrictRedis.ZRANGEBYSCORE("SORTEDSET", "-inf", "inf") or redis.StrictRedis.ZRANGEBYSCORE("SORTEDSET", float("-inf"), float("inf")) UPDATE My error was that my abstraction for zrangebyscore was using zrange by mistake...inf works as stated below. 回答1: This is my code has been tested

C# fastest union of 2 sets of sorted values

谁说我不能喝 提交于 2019-12-09 16:41:14
问题 What is the fastest way to union 2 sets of sorted values? Speed (big-O) is important here; not clarity - assume this is being done millions of times. Assume you do not know the type or range of the values, but have an efficent IComparer<T> and/or IEqualityComparer<T> . Given the following set of numbers: var la = new int[] { 1, 2, 4, 5, 9 }; var ra = new int[] { 3, 4, 5, 6, 6, 7, 8 }; I am expecting 1, 2, 3, 4, 5, 6, 7, 8, 9. The following stub may be used to test the code: static void Main

What is the most efficient way to ge the top N items of the union of M sorted sets

こ雲淡風輕ζ 提交于 2019-12-08 12:39:02
问题 Say you have 4 sorted sets with thousands and thousands of keys and scores. Since they are sorted sets, getting the top items can ben done in logaritmic time complexity. The easy way would be to take the union of the sets, and then get the top items. But doing so is at least linear to the sum of all items in all sets. The best way I could think of is this: Take the top N items from every set Find the item with the lowest rank and the higest score for that rank. Devide that score by the number

How to store sorted set of objects in redis?

戏子无情 提交于 2019-12-07 04:59:02
问题 I would like to know how to store a list of objects in Redis. That is I have a key like this. users:pro { name: "Bruce", age: "20", score: 100, name: "Ed", age: "22", score: 80 } Where I will want to store a list of hashes as value of a particular key. I would like to use the score field as the score field in the sorted set. How could I accomplish this? I have seen writing a putting a single hash for a key, but what if I want multiple hashes and one of the hash fields must act as a score

Redis sorted set and solving ties

你。 提交于 2019-12-06 06:57:12
问题 I'm using a Redis sorted set to store a ranking for a project I'm working on. We hadn't anticipated (!) how we wanted to handle ties. Redis sorts lexicographically the entries that have the same score, but what we want to do is instead give the same rank to all the entries that have the same score, so for instance in the case of redis 127.0.0.1:6379> ZREVRANGE foo 0 -1 WITHSCORES 1) "first" 2) "3" 3) "second3" 4) "2" 5) "second2" 6) "2" 7) "second1" 8) "2" 9) "fifth" 10) "1" we want to