collections

What is the time complexity of java.util.Collections.sort() method?

﹥>﹥吖頭↗ 提交于 2019-12-18 04:33:26
问题 I have written the following class: public class SortingObjectsWithAngleField implements Comparator<Point> { public int compare(Point p1, Point p2) { double delta = p1.getAngle() - p2.getAngle(); if(delta == 0.00001) return 0; return (delta > 0.00001) ? 1 : -1; } } Then, in my main() method, I have created a List to which I add some objects which has "X" and "angle" field. I then use: Collections.sort(list, new SortingObjectsWithAngleField()); What is the complexity of this sort method? 回答1:

Check if a record exists in a VB6 collection?

霸气de小男生 提交于 2019-12-18 04:33:14
问题 I've inherited a large VB6 app at my current workplace. I'm kinda learning VB6 on the job and there are a number of problems I'm having. The major issue at the moment is I can't figure out how to check if a key exists in a Collection object. Can anyone help? 回答1: My standard function is very simple. This will work regardless of the element type, since it doesn't bother doing any assignment, it merely executes the collection property get. Public Function Exists(ByVal oCol As Collection, ByVal

Clone a List, Map or Set in Dart

泄露秘密 提交于 2019-12-18 04:32:35
问题 Coming from a Java background: what is the recommended way to "clone" a Dart List , Map and Set ? 回答1: Use of clone() in Java is tricky and questionable 1,2 . Effectively, clone() is a copy constructor and for that, the Dart List , Map and Set types each have a named constructor named .from() that perform a shallow copy; e.g. given these declarations Map<String, int> numMoons, moreMoons; numMoons = const <String,int>{ 'Mars' : 2, 'Jupiter' : 27 }; List<String> planets, morePlanets; you can

How do I use HashSet<T> as a dictionary key?

微笑、不失礼 提交于 2019-12-18 04:29:10
问题 I wish to use HashSet<T> as the key to a Dictionary: Dictionary<HashSet<T>, TValue> myDictionary = new Dictionary<HashSet<T>, TValue>(); I want to look up values from the dictionary such that two different instances of HashSet<T> that contain the same items will return the same value. HashSet<T> 's implementations of Equals() and GetHashCode() don't seem to do this (I think they're just the defaults). I can override Equals() to use SetEquals() but what about GetHashCode() ? I feel like I am

How to safely remove other elements from a Collection while iterating through the Collection

天大地大妈咪最大 提交于 2019-12-18 04:16:12
问题 I'm iterating over a JRE Collection which enforces the fail-fast iterator concept, and thus will throw a ConcurrentModificationException if the Collection is modified while iterating, other than by using the Iterator.remove() method . However, I need to remove an object's "logical partner" if the object meets a condition. Thus preventing the partner from also being processed. How can I do that? Perhaps by using better collection type for this purpose? Example. myCollection<BusinessObject> for

What happens if we override only hashCode() in a class and use it in a Set?

泪湿孤枕 提交于 2019-12-18 04:09:37
问题 This may not be the real world scenario but just curious to know what happens, below is the code. I am creating a set of object of class UsingSet . According to hashing concept in Java, when I first add object which contains "a", it will create a bucket with hashcode 97 and put the object inside it. Again when it encounters an object with "a", it will call the overridden hashcode method in the class UsingSet and it will get hashcode 97 so what is next? As I have not overridden equals method,

What is the time complexity of LinkedList.getLast() in Java?

↘锁芯ラ 提交于 2019-12-18 03:58:18
问题 I have a private LinkedList in a Java class & will frequently need to retrieve the last element in the list. The lists need to scale, so I'm trying to decide whether I need to keep a reference to the last element when I make changes (to achieve O(1)) or if the LinkedList class does that already with the getLast() call. What is the big-O cost of LinkedList.getLast() and is it documented? (i.e. can I rely on this answer or should I make no assumptions & cache it even if it's O(1)?) 回答1: It is O

ArrayList Retrieve object by Id

橙三吉。 提交于 2019-12-18 03:56:43
问题 Suppose I have an ArrayList<Account> of my custom Objects which is very simple. For example: class Account { public String Name; public Integer Id; } I want to retrieve the particular Account object based on an Id parameter in many parts of my application. What would be best way of going about this ? I was thinking of extending ArrayList but I am sure there must be better way. 回答1: It sounds like what you really want to use is a Map , which allows you to retrieve values based on a key. If you

Convert a double array to Double ArrayList

若如初见. 提交于 2019-12-18 03:54:40
问题 When I try to convert a double array to a Double arrayList I got the following error: Exception in thread "main" java.lang.ClassCastException: [D cannot be cast to java.lang.Double Below is my code. double [] firstValueArray ; ArrayList <Double> firstValueList = new ArrayList (Arrays.asList(firstValueArray)); I am comparing this list with another list and assign the result to another double variable. Please let me know the reason for this error. 回答1: Alas, Arrays.asList(..) doesn't work with

Which Java Collections are synchronized(thread safe), which are not?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-18 03:53:26
问题 Which Java Collections are synchronized, which are not? Example: HashSet is not synchronized 回答1: There are three groups of Collections. Java 1.0 collections which mostly legacy classes. This includes Hashtable, Vector, Stack. These are synchronized but I don't recommend you use them. Properties is perhaps one exception, but I wouldn't use it in a multi-threaded context. Java 1.2 collections added in 1998 which largely replaced these collection are not synchronized, but can be synchronized