collections

compare and sort different type of objects using java Collections

倖福魔咒の 提交于 2019-12-18 08:23:16
问题 How to compare and sort different type of objects using java Collections .Below is the use case: For example DOG,MAN,TREE, COMPUTER,MACHINE - all these different objects has a common property say "int lifeTime". Now I want to order these obects based on the lifeTime property Thx 回答1: All of these objects should have a common abstract class/interface such as Alive with a method getLifeTime() , and you could have either Alive extends Comparable<Alive> or create your own Comparator<Alive> .

Java generic collection, cannot add list to list

三世轮回 提交于 2019-12-18 07:53:24
问题 Why does the following public class ListBox { private Random random = new Random(); private List<? extends Collection<Object>> box; public ListBox() { box = new ArrayList<>(); } public void addTwoForks() { int sizeOne = random.nextInt(1000); int sizeTwo = random.nextInt(1000); ArrayList<Object> one = new ArrayList<>(sizeOne); ArrayList<Object> two = new ArrayList<>(sizeTwo); box.add(one); box.add(two); } public static void main(String[] args) { new ListBox().addTwoForks(); } } Not work? Just

What is the main difference between Hashset, Treeset and LinkedHashset, Hashmap and how does it work in Java?

我们两清 提交于 2019-12-18 07:08:27
问题 I just understand that LinkedHashSet does not allows duplicate elements when it is inserting. But, I dont understand how does Hashset works in Hava? I know a bit that Hashtable is used in Hashset so the hashtable used to store the elements and here also does not allow the duplicate elements. Then, Treeset is also similar to Hashset it also does not allows duplicate entries so unique elements will be seen and it follows ascending order. I have one more doubt regarding HashMap - Hashmap does

Shuffle/Random comparator

家住魔仙堡 提交于 2019-12-18 07:08:06
问题 IS there any way to emulate the behavior of Collections.shuffle without a comparator being vulnerable to the sorting algorithm implementation for the result to be safe? I mean not breaking the comparable contract etc.. 回答1: It is impossible to implement a real “shuffling comparator” without breaking the contract. One fundamental aspect of the Comparator contract is that the results are reproducible so the ordering of a particular Comparator instance must be fixed. Of course, you could pre

How do I filter a magento collection by a select drop-down attribute?

帅比萌擦擦* 提交于 2019-12-18 06:51:48
问题 In magento, I have an attribute called cl_designer, which is a select drop-down option. I want to filter the products collection on it, like this: $collection = Mage::getModel('catalog/product')->getCollection(); $collection->addAttributeToFilter('cl_designer', array('like' => $filter)); But it doesn't work! When I print out the query with $collection->getselect(), I see that it is comparing $filter to catalog_product_entity_int.value. But this is wrong, because for select options, catalog

What is the time complexity of constructing a PriorityQueue from a collection?

情到浓时终转凉″ 提交于 2019-12-18 06:01:26
问题 What is the complexity of Java's PriorityQueue constructor with a Collection ? I used the constructor: PriorityQueue(Collection<? extends E> c) Is the complexity O(n) or O(n*log(n))? 回答1: The time complexity to initialize a PriorityQueue from a collection, even an unsorted one, is O(n). Internally this uses a procedure called siftDown() to "heapify" an array in-place. (This is also called pushdown in the literature.) This is counterintuitive. It seems like inserting an element into a heap is

Thread safety with Dictionary<int,int> in .Net

你。 提交于 2019-12-18 05:47:19
问题 I have this function: static Dictionary<int, int> KeyValueDictionary = new Dictionary<int, int>(); static void IncreaseValue(int keyId, int adjustment) { if (!KeyValueDictionary.ContainsKey(keyId)) { KeyValueDictionary.Add(keyId, 0); } KeyValueDictionary[keyId] += adjustment; } Which I would have thought would not be thread safe. However, so far in testing it I have not seen any exceptions when calling it from multiple threads at the same time. My questions: Is it thread safe or have I just

HashSet vs. ArrayList

[亡魂溺海] 提交于 2019-12-18 04:44:17
问题 So I have a custom class Class that will have a set of another custom class Students. So it will look something like this: public class Class { private Set<Student> students; // other methods } Now I will be adding and removing many students to the set students and i will also be changing many of the private fields of a student already in the set of students. QUESTION: What data structure should I use to best implement this? Since I will be changing the property of the Student objects in set

Finding duplicate values in dictionary and print Key of the duplicate element

与世无争的帅哥 提交于 2019-12-18 04:44:11
问题 What can be the fastest way to to check the duplicate values in the dictionary and print its key? Dictionary MyDict which is having following values, Key Value 22 100 24 200 25 100 26 300 29 200 39 400 41 500 Example: key 22 and 25 have same values and i need to print that 22 and 25 have duplicate values. 回答1: It depends. If you have an ever changing dictionary and need to get that information only once, use this: MyDict.GroupBy(x => x.Value).Where(x => x.Count() > 1) However, if you have a

Why not allow an external interface to provide hashCode/equals for a HashMap?

家住魔仙堡 提交于 2019-12-18 04:36:06
问题 With a TreeMap it's trivial to provide a custom Comparator , thus overriding the semantics provided by Comparable objects added to the map. HashMap s however cannot be controlled in this manner; the functions providing hash values and equality checks cannot be 'side-loaded'. I suspect it would be both easy and useful to design an interface and to retrofit this into HashMap (or a new class)? Something like this, except with better names: interface Hasharator<T> { int alternativeHashCode(T t);