Is there a way to get the value of a HashMap randomly in Java?

This works: Random generator = new Random(); Object[] values = myHashMap.values().toArray(); Object randomValue = values[generator.nextInt(values.length)]; If you want the random value to be a type other than an Object simply add a cast to the last line. So if myHashMap was declared as: Map<Integer,String> myHashMap = new HashMap<Integer,String>(); The last line can be: String randomValue … Read more

Hibernate query.list() method is returning empty list instead of null value

The reason is not to force null checks in client code, in consistency with Effective Java 2nd Edition, Item 43: Return empty arrays or collections, not nulls. This makes the client code simpler and less error-prone (and most likely the method implementation as well). The null-return idiom is likely a holdover from the C programming … Read more

Notify ObservableCollection when Item changes

The spot you have commented as // Code to trig on item change… will only trigger when the collection object gets changed, such as when it gets set to a new object, or set to null. With your current implementation of TrulyObservableCollection, to handle the property changed events of your collection, register something to the … Read more

linq question: querying nested collections

To find an answer. questions.SelectMany(q => q.Answers).Where(a => a.Name == “SomeName”) To find the question of an answer. questions.Where(q => q.Answers.Any(a => a.Name == “SomeName”)) In fact you will get collections of answers or questions and you will have to use First(), FirstOrDefault(), Single(), or SingleOrDefault() depending on your needs to get one specific answer … Read more

ArrayList filter [duplicate]

In java-8, they introduced the method removeIf which takes a Predicate as parameter. So it will be easy as: List<String> list = new ArrayList<>(Arrays.asList(“How are you”, “How you doing”, “Joe”, “Mike”)); list.removeIf(s -> !s.contains(“How”));

How to check if collection contains items in given order using Hamcrest

You can use contains matcher instead, but you probably need to use latest version of Hamcrest. That method checks the order. assertThat(list, contains(“foo”, “boo”)); You can also try using containsInAnyOrder if order does not matter to you. That’s the code for contains matcher: public static <E> Matcher<Iterable<? extends E>> contains(List<Matcher<? super E>> itemMatchers) { return … Read more

Difference between Hashtable and Collections.synchronizedMap(HashMap)

One more difference that I can find at the implementation of both the classes is as follows: • The Hashtable class has all its methods synchronized i.e. the locking is done at the method level and hence one can say that the mutex is always at the Hashtable object (this) level. • The method Collections.synchronizedMap(Map) … Read more

Initial capacity of collection types, e.g. Dictionary, List

If the default values are not documented, the reason is likely that the optimal initial capacity is an implementation detail and subject to change between framework versions. That is, you shouldn’t write code that assumes a certain default value. The constructor overloads with a capacity are for cases in which you know better than the … Read more

VBA collection: list of keys

If you intend to use the default VB6 Collection, then the easiest you can do is: col1.add array(“first key”, “first string”), “first key” col1.add array(“second key”, “second string”), “second key” col1.add array(“third key”, “third string”), “third key” Then you can list all values: Dim i As Variant For Each i In col1 Debug.Print i(1) Next … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)