collections

How to Check if an ArrayList Contain 2 values?

梦想与她 提交于 2019-12-20 02:27:30
问题 is there any way to check if a collection contain a value or more with better performance than looping twice with contains? in other meaning something that would look like this person.contains("Joe" || "Jasha"); 回答1: The implementation of ArrayList.contains() loops through every element and does an equals() test, so calling .contains() twice is inefficient . You could write your own loop that check both at once using a compiled regex Pattern that looks for either name at the same time:

Generic collection & wildcard in java

只愿长相守 提交于 2019-12-20 02:26:42
问题 I am having problems getting my head around generics in the following situation, see inline comments below for my questions: public void exampleMethod() { //Intuitively I would expect this to mean that test is set containing objects //that subclass AbstractGroup Set<? extends AbstractGroup> test; //Yet the compiler complains here and I do not understand why? test.add(new AnyAbstractGroupSubGroup()); //I would guess that a method call such as this at runtime test = new HashSet<SubGroupA>() /

Syncronized sorting between two ArrayLists

a 夏天 提交于 2019-12-20 02:13:25
问题 I have two ArrayLists. The first contains a group of words with capitalization and punctuation. The other contains this same group of words, but with the capitalization and punctuation removed. . ArrayList1 ..... ArrayList2 MURDER! ........ murder It's ........... its Hello .......... hello Yes-Man ........ yesman ON ............. on The second array has all the words alphabetized and all the letters in each word alphabetized. It looks something like this: aemnsy demrru ehllo ist no I want to

Syncronized sorting between two ArrayLists

纵饮孤独 提交于 2019-12-20 02:13:22
问题 I have two ArrayLists. The first contains a group of words with capitalization and punctuation. The other contains this same group of words, but with the capitalization and punctuation removed. . ArrayList1 ..... ArrayList2 MURDER! ........ murder It's ........... its Hello .......... hello Yes-Man ........ yesman ON ............. on The second array has all the words alphabetized and all the letters in each word alphabetized. It looks something like this: aemnsy demrru ehllo ist no I want to

Why Collection Initialization Throws NullReferenceException

僤鯓⒐⒋嵵緔 提交于 2019-12-20 02:13:21
问题 The following code throws a NullReferenceException : internal class Foo { public Collection<string> Items { get; set; } // or List<string> } class Program { static void Main(string[] args) { new Foo() { Items = { "foo" } // throws NullReferenceException }; } } Why don't collection initiliazers work in this case, although Collection<string> implements the Add() method, and why is NullReferenceException is thrown? Is it possible to get the collection initializer working, or is Items = new

How can I shorten List<List<KeyValuePair<string, string>>>?

我只是一个虾纸丫 提交于 2019-12-20 02:10:56
问题 I want to store an list of key value pair lists in a lightweight structure. This seems too cumbersome. What's better? Does List<Dictionary<string, string>> add much a overhead? What other options are available? 回答1: Consider using aliasing for shorthand: namespace Application { using MyList = List<List<KeyValuePair<string, string>>>; public class Sample { void Foo() { var list = new MyList(); } } } 回答2: Both List and Dictionary are pretty efficient, so I wouldn't think twice about using them.

collections.Counter: most_common INCLUDING equal counts

人盡茶涼 提交于 2019-12-20 01:47:05
问题 In collections.Counter , the method most_common(n) returns only the n most frequent items in a list. I need exactly that but I need to include the equal counts as well. from collections import Counter test = Counter(["A","A","A","B","B","C","C","D","D","E","F","G","H"]) -->Counter({'A': 3, 'C': 2, 'B': 2, 'D': 2, 'E': 1, 'G': 1, 'F': 1, 'H': 1}) test.most_common(2) -->[('A', 3), ('C', 2) I would need [('A', 3), ('B', 2), ('C', 2), ('D', 2)] since they have the same count as n=2 for this case.

collections.Counter: most_common INCLUDING equal counts

筅森魡賤 提交于 2019-12-20 01:46:38
问题 In collections.Counter , the method most_common(n) returns only the n most frequent items in a list. I need exactly that but I need to include the equal counts as well. from collections import Counter test = Counter(["A","A","A","B","B","C","C","D","D","E","F","G","H"]) -->Counter({'A': 3, 'C': 2, 'B': 2, 'D': 2, 'E': 1, 'G': 1, 'F': 1, 'H': 1}) test.most_common(2) -->[('A', 3), ('C', 2) I would need [('A', 3), ('B', 2), ('C', 2), ('D', 2)] since they have the same count as n=2 for this case.

Uncaught Error: Not permitted. Untrusted code may only update documents by ID. [403]

孤者浪人 提交于 2019-12-20 01:45:12
问题 I have this error but it does not specify which code line is faulty. Is there any way I can narrow down which codes I need to focus on? Not sure if its a related problem, but when I submit a doc and its supposed to recognize my user.username , but it comes out blank when html displays {{author}} . The code for the collection (shared folder for both public/ server) is as below: var post = _.extend(postAttributes, { userId: user._id, author: user.username }); Do appreciate any help! Update: New

Find some values in a mongodb collection?

谁都会走 提交于 2019-12-20 01:37:48
问题 Im trying to read a (mongo)userdatabase with java. On the tutorial page I saw how to read the whole collection. I can do something like that: DBCursor cursor = col.find(); while (cursor.hasNext()) { System.out.println(cursor.next()); } Now if I have a collection with users := name, age, password (...) and whatever. Now I would like to find a name with a password. For example for a login process. Lets say I have two strings: String n and p. If there is a user.equals(n) and a password.equals(p)