collections

List vs ArrayList vs Dictionary vs Hashtable vs Stack vs Queue? [closed]

一笑奈何 提交于 2019-12-29 02:33:11
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 5 years ago . We can use any of these (includes List, ArrayList, Dictionary, Hashtable, Stack, Queue) to hold value or hold reference to other objects as a collection. But, my question is which one is used when? 回答1: Lists Lists allow duplicate items, can be accessed by index, and support

Returning a Collection<ChildType> from a method that specifies that it returns Collection<ParentType>

為{幸葍}努か 提交于 2019-12-29 02:07:19
问题 I'm trying to write a method that will return a collection (eg Arraylist) that is declared using generics as containing either a parent class or a class that extends the parent class . For this method the objects in the collection will always be used as instances of the parent class but in other contexts they are used differently hence them being held separately. For example: import java.util.ArrayList; import java.util.Collection; public class TestClass { public static int PARENTTYPE=0;

Comparison method violates its general contract Exception

半城伤御伤魂 提交于 2019-12-29 01:43:12
问题 Below is a block of code that results in exception as indicated, Code : Collections.sort( arrayList, new Comparator() { public int compare( Object o1, Object o2 ) { TypeAdapterSort tas1 = ( TypeAdapterSort ) o1; TypeAdapterSort tas2 = ( TypeAdapterSort ) o2; if ( tas1.order < tas2.order ) return -1; else return 1; } } ); Exception : java.lang.IllegalArgumentException: Comparison method violates its general contract! at java.util.TimSort.mergeLo(TimSort.java:747) at java.util.TimSort.mergeAt

How to change value of an item of a collection

China☆狼群 提交于 2019-12-29 01:34:20
问题 With this code (in excel-vba) I add to a collection a number of items depending on an array. I use the value of the array as key and the string "NULL" as value for each item added. Dim Coll As New collection Dim myArr() Set Coll = New collection myArr() = Array("String1", "String2", "String3") For i = LBound(myArr) To UBound(myArr) Coll.Add "NULL", myArr(i) Next i Now, if I want to change the value of an item, identifying it by the key, I must remove the item and then add an item with same

What's the quickest way to remove an element from a Map by value in Java?

我是研究僧i 提交于 2019-12-28 12:51:28
问题 What's the quickest way to remove an element from a Map by value in Java? Currently I'm using: DomainObj valueToRemove = new DomainObj(); String removalKey = null; for (Map.Entry<String, DomainObj> entry : map.entrySet()) { if (valueToRemove.equals(entry.getValue())) { removalKey = entry.getKey(); break; } } if (removalKey != null) { map.remove(removalKey); } 回答1: Without using a Bi-directional map (commons-collections and google collections have them), you're stuck with iterating the Map 回答2

How can we maintain Immutability of a class with a mutable reference

99封情书 提交于 2019-12-28 12:05:11
问题 I know all the basic rules to make our class immutable but I am a little confused when there is another class reference. I know if there is collection instead of Address then we can make use of Collections.unmodifiableList(new ArrayList<>(modifiable)); and then we can make our class immutable. But in below case I am still unable to get the concept. public final class Employee{ private final int id; private Address address; public Employee(int id, Address address) { this.id = id; this.address

why polymorphism doesn't treat generic collections and plain arrays the same way?

好久不见. 提交于 2019-12-28 10:06:52
问题 assume that class Dog extends class Animal: why this polymorphic statement is not allowed: List<Animal> myList = new ArrayList<Dog>(); However, it's allowed with plain arrays: Animal[] x=new Dog[3]; 回答1: The reasons for this are based on how Java implements generics. An Arrays Example With arrays you can do this (arrays are covariant as others have explained) Integer[] myInts = {1,2,3,4}; Number[] myNumber = myInts; But, what would happen if you try to do this? Number[0] = 3.14; //attempt of

Difference between IQueryable, ICollection, IList & IDictionary interface

那年仲夏 提交于 2019-12-28 07:39:19
问题 I am trying to understand difference between IQueryable, ICollection, IList & IDictionary interface which is more faster for basic operations like iterating, Indexing, Querying and more. which class like Collection, List, Dictionary etc would be good to initiating with these interfaces and when should we be using these class. Basic advantages of using these classes over others. I tried reading other posts with similar questions but nothing answered my full questions. Thanks for the help. 回答1:

Window.InputBindings with a bound collection

我是研究僧i 提交于 2019-12-28 06:50:08
问题 I can't find anything that looks like this online. I am looking for a way to create a collection of Keybindings in the code (with a Keybinding ViewModel), and then bind the collection to the view, instead of listing out every binding manually in Xaml. I would expect it to look something like this <Window.InputBindings ItemsSource="{Binding Path=KeybindingList}" /> and then in the code, have a List. Is such an approach possible? Where would I start? 回答1: You can create an attached property,

Storing different types of elements in a List in Java

↘锁芯ラ 提交于 2019-12-28 06:32:10
问题 I'm trying to develop a general table loader which schema is known at runtime. This requires having a class which contains a list of different types of elements and supports various get and set method such as getInt(int index) , asString(int index) , asStringList(int index) . The types of elements I consider are Integer , Double , String , and List<Integer> , List<Double> and List<String> . The actual type of each element is known in run time, and I will store them in a List describing its