collections

What is the need of collection framework in java?

南楼画角 提交于 2019-12-21 04:42:26
问题 What is the need of Collection framework in Java since all the data operations(sorting/adding/deleting) are possible with Arrays and moreover array is suitable for memory consumption and performance is also better compared with Collections. Can anyone point me a real time data oriented example which shows the difference in both(array/Collections) of these implementations. 回答1: Arrays are not resizable. Java Collections Framework provides lots of different useful data types, such as linked

C# .First() vs [0]

一个人想着一个人 提交于 2019-12-21 04:33:09
问题 Interested, does approaches has any differences. So, I created two snippets. Snippet A List<int> a = new List<int>(); a.Add(4); a.Add(6); int b = a.First(); and Snippet B List<int> a = new List<int>(); a.Add(4); a.Add(6); int b = a[0]; In IL we trust, so Snippet A IL IL_0000: nop IL_0001: newobj System.Collections.Generic.List<System.Int32>..ctor IL_0006: stloc.0 // a IL_0007: ldloc.0 // a IL_0008: ldc.i4.4 IL_0009: callvirt System.Collections.Generic.List<System.Int32>.Add IL_000E: nop IL

Why use heap instead of binary tree when implementing priority queue?

夙愿已清 提交于 2019-12-21 04:28:21
问题 It seems to me that the only advantage of heap over binary tree is to find the smallest item in the heap in complexity of O(1) instead of O(log(2)n) in binary tree. When implementing priority queue you need to delete the smallest item each from the data structre. deleting the smallest item from a tree and both heap done in complexity of O(log(2)n). Althogh deleting item from a tree may be more complex. Deleting item with no childrens acctually very simple. My question is why use heap instead

C# Object reference not set to an instance of an object. Instantiating Class within a List?

。_饼干妹妹 提交于 2019-12-21 04:25:21
问题 public class OrderItem { public string ProductName { get; private set; } public decimal LatestPrice { get; private set; } public int Quantity { get; private set; } public decimal TotalOrder { get {return LatestPrice * Quantity;}} public OrderItem(string name, decimal price, int quantity) { } public OrderItem(string name, decimal price) : this(name, price, 1) { } } Above is the class, just for some background. public void AddProduct(string name, decimal price, int quantity) { lstOrderitem.Add

What is the logic of a fail safe iterator?

寵の児 提交于 2019-12-21 04:18:24
问题 If fail-safe iterator creates a separate copy and works on that, how come it is aware of any changes made to the original? public class concurrentHashMap { public static void main(String[] args) throws InterruptedException { MapCheck obj1 = new MapCheck(); Thread t1 = new Thread(new Runnable() { @Override public void run() { obj1.put(); } }); Thread t2 = new Thread(new Runnable() { @Override public void run() { obj1.iterte(); } }); t1.start(); t2.start(); t1.join(); t2.join(); } } class

Updating of BindingSource in WinForms does not update Datasource Collection

自作多情 提交于 2019-12-21 03:54:25
问题 I want to display a custom collection in a DataGridView in a Windows Forms app. This custom collection implements ICollection , and IEnumerable . I have set up a BindingSource , using the collection as the .DataSource property. The DataGridView is set to use my BindingSource as it's DataSource. When I add a new item to the collection using the BindingSource.Add() method, the DataGridView updates correctly with the new item. The BindingSource DataSource, on the other hand, does not:

Java 8: HashMap initialization with lambda expressions

自作多情 提交于 2019-12-21 03:35:09
问题 I'm trying to declare and define larger hash map at once. This is how I do it: public HashMap<Integer, Callable<String>> opcode_only = new HashMap<Integer, Callable<String>>() {{ put(x, y); put(x, y); }}; But, when I try to use lambda expressions in body of put , I'm hitting on eclipse warrning/error. This is how I use lambda in HashMap: public HashMap<Integer, Callable<String>> opcode_only = new HashMap<Integer, Callable<String>>() {{ put(0, () -> { return "nop"; }); put(1, () -> { return

How to convert String into Hashmap in java

a 夏天 提交于 2019-12-21 03:32:42
问题 How can I convert a String into a HashMap ? String value = "{first_name = naresh, last_name = kumar, gender = male}" into Map<Object, Object> = { first_name = naresh, last_name = kumar, gender = male } Where the keys are first_name , last_name and gender and the values are naresh , kumar , male . Note: Keys can be any thing like city = hyderabad . I am looking for a generic approach. 回答1: This is one solution. If you want to make it more generic, you can us StringUtils library. String value =

Collections.sort() throws Comparison method violates its general contract! exception

耗尽温柔 提交于 2019-12-21 03:32:27
问题 I'm trying to sort a List<> object and I get this exception thrown (for large lists only though) sorting code: List<FinalSentence> sentenceList = finalRepresentation.getSentences(); Collections.sort(sentenceList); // <=== EXCEPTION THROWN HERE!!! FinalSentence class header: public class FinalSentence implements Comparable<FinalSentence>{...} compareTo() implementation: @Override public int compareTo(FinalSentence o) { if (this == o) { return 0; } if (this.score > o.score) { return 1; } if

How to get a random element from a Set in Scala

大兔子大兔子 提交于 2019-12-21 03:18:16
问题 For any given set, for instance, val fruits = Set("apple", "grape", "pear", "banana") how to get a random element from fruits ? Many Thanks. 回答1: convert into Vector and get random element from it scala> val fruits = Set("apple", "grape", "pear", "banana") fruits: scala.collection.immutable.Set[String] = Set(apple, grape, pear, banana) scala> import scala.util.Random import scala.util.Random scala> val rnd=new Random rnd: scala.util.Random = scala.util.Random@31a9253 scala> fruits.toVector