collections

Multi-valued hashtable in Java

南楼画角 提交于 2019-12-28 06:27:34
问题 Is it possible to have multiple values for the same key in a hash table? If not, can you suggest any such class or interface which could be used? 回答1: No. That's kind of the idea of hash tables. However, you could either roll your own with a Map<YourKeyObject, List<YourValueObject>> and some utility methods for creating the list if it's not present, or use something like the Multimap from Google Collections. Example: String key = "hello"; Multimap<String, Integer> myMap = HashMultimap.create(

Concurrent Set Queue

Deadly 提交于 2019-12-28 05:56:21
问题 Maybe this is a silly question, but I cannot seem to find an obvious answer. I need a concurrent FIFO queue that contains only unique values. Attempting to add a value that already exists in the queue simply ignores that value. Which, if not for the thread safety would be trivial. Is there a data structure in Java or maybe a code snipit on the interwebs that exhibits this behavior? 回答1: If you want better concurrency than full synchronization, there is one way I know of to do it, using a

Do Collections.unmodifiableXXX methods violate LSP? [closed]

夙愿已清 提交于 2019-12-28 05:37:07
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed last year . Liskov Substitution principle is one of the principles of SOLID. I have read this principle some number of times now and have tried to understand it. Here is what I make out of it, This principle is related to strong behavioral contract among the hierarchy of classes. The

What is the fastest Java collection with the basic functionality of a Queue?

限于喜欢 提交于 2019-12-28 03:18:14
问题 What is the fastest collection in Java? I only need the operations to add and remove, order is not important, equals elements is not a issue, nothing more than add and remove is imporant. Without limit size is important too. These collection will have Objects inside him. Currently I'm using ArrayDeque because I see this is the faster Queue implementation. 回答1: ArrayDeque is best. See this benchmark, which comes from this blog post about the results of benchmarking this. ArrayDeque doesn't

What's Wrong with an ArrayList?

霸气de小男生 提交于 2019-12-28 03:05:09
问题 Recently I asked a question on SO that had mentioned the possible use of an c# ArrayList for a solution. A comment was made that using an arraylist is bad. I would like to more about this. I have never heard this statement before about arraylists. could sombody bring me up to speed on the possible performance problems with using arraylists c#. .net-2 回答1: The main problem with ArrayList is that is uses object - it means you have to cast to and from whatever you are encapsulating. It is a

What's Wrong with an ArrayList?

那年仲夏 提交于 2019-12-28 03:05:09
问题 Recently I asked a question on SO that had mentioned the possible use of an c# ArrayList for a solution. A comment was made that using an arraylist is bad. I would like to more about this. I have never heard this statement before about arraylists. could sombody bring me up to speed on the possible performance problems with using arraylists c#. .net-2 回答1: The main problem with ArrayList is that is uses object - it means you have to cast to and from whatever you are encapsulating. It is a

IEnumerable<T> as return type

爱⌒轻易说出口 提交于 2019-12-28 02:49:07
问题 Is there a problem with using IEnumerable<T> as a return type? FxCop complains about returning List<T> (it advises returning Collection<T> instead). Well, I've always been guided by a rule "accept the least you can, but return the maximum." From this point of view, returning IEnumerable<T> is a bad thing, but what should I do when I want to use "lazy retrieval"? Also, the yield keyword is such a goodie. 回答1: This is really a two part question. 1) Is there inherently anything wrong with

How can I loop through a List<T> and grab each item?

主宰稳场 提交于 2019-12-28 02:29:59
问题 How can I loop through a List and grab each item? I want the output to look like this: Console.WriteLine("amount is {0}, and type is {1}", myMoney.amount, myMoney.type); Here is my code: static void Main(string[] args) { List<Money> myMoney = new List<Money> { new Money{amount = 10, type = "US"}, new Money{amount = 20, type = "US"} }; } class Money { public int amount { get; set; } public string type { get; set; } } 回答1: foreach : foreach (var money in myMoney) { Console.WriteLine("Amount is

How to Quickly Remove Items From a List

寵の児 提交于 2019-12-28 01:52:52
问题 I am looking for a way to quickly remove items from a C# List<T> . The documentation states that the List.Remove() and List.RemoveAt() operations are both O(n) List.Remove List.RemoveAt This is severely affecting my application. I wrote a few different remove methods and tested them all on a List<String> with 500,000 items. The test cases are shown below... Overview I wrote a method that would generate a list of strings that simply contains string representations of each number ("1", "2", "3"

Best way to create a hashmap of arraylist

谁说我不能喝 提交于 2019-12-27 17:38:26
问题 I have one million rows of data in .txt format. the format is very simple. For each row: user1,value1 user2,value2 user3,value3 user1,value4 ... You know what I mean. For each user, it could appear many times, or appear only once (you never know). I need to find out all the values for each user. Because user may appear randomly, I used Hashmap to do it. That is: HashMap(key: String, value: ArrayList). But to add data to the arrayList, I have to constantly use HashMap get(key) to get the