collections

Immutable collections in Doctrine 2?

梦想的初衷 提交于 2019-12-22 06:02:19
问题 I'm looking for a way to return an immutable collection from a domain object in Doctrine 2. Let's start with this example from the doc: class User { // ... public function getGroups() { return $this->groups; } } // ... $user = new User(); $user->getGroups()->add($group); From a DDD point of view, if User is the aggregate root, then we'd prefer: $user = new User(); $user->addGroup($group); But still, if we do need the getGroups() method as well, then we ideally don't want to return the

Immutable collections in Doctrine 2?

旧时模样 提交于 2019-12-22 06:02:08
问题 I'm looking for a way to return an immutable collection from a domain object in Doctrine 2. Let's start with this example from the doc: class User { // ... public function getGroups() { return $this->groups; } } // ... $user = new User(); $user->getGroups()->add($group); From a DDD point of view, if User is the aggregate root, then we'd prefer: $user = new User(); $user->addGroup($group); But still, if we do need the getGroups() method as well, then we ideally don't want to return the

Why doesn't Collections.Generic.Queue have Synchronized method but Collections.Queue has?

纵饮孤独 提交于 2019-12-22 05:53:18
问题 System.Collections.Queue class has Queue.Synchronized method which returns a thread-safe Queue implementation. But the generic one, System.Collections.Generic.Queue does not have a Synchronized method. At this point I have two questions in mind: Why doesn't generic one have this method? Is it a framework API design decision? How is the queue returned from Queue.Synchronized is different than ConcurrentQueue<T> class? Thanks. 回答1: The Synchronized() method returns a wrapper queue that slaps a

Given a list of IP address, how do you find min, max?

懵懂的女人 提交于 2019-12-22 05:36:19
问题 In Java, i have an arrayList of ip address. how do i find the min and max ? I have used the Collection.min() but it doesnt work given a case like : 192.168.0.1 <--min 192.168.0.250 192.168.0.9 <--max how do i return 192.168.0.1 <--min 192.168.0.250 <--max instead? ArrayList is retrieve from the database. I would need to do this operation per tick (each tick is at 5 sec interval). The number of IP address would hit a max of probably 300. 回答1: Convert the IP address into a long integer, then

While sorting the map based on value, some values are missing. What causes this weird behaviour?

自作多情 提交于 2019-12-22 05:33:29
问题 I am trying to sort a map based on word frequency (i.e., based on value). For that I have overridden comparator and passed to TreeMap , but I am getting this weird output. public class WordFrequency { public static String sentence = "one three two two three three four four four"; public static Map<String, Integer> map; public static void main(String[] args) { map = new HashMap<>(); String[] words = sentence.split("\\s"); for (String word : words) { Integer count = map.get(word); if (count ==

Kotlin: Modifying (immutable) List through cast, is it legitimate?

隐身守侯 提交于 2019-12-22 05:29:30
问题 As we know the List in Kotlin is immutable i.e. you can't do add and remove as below. class TempClass { var myList: List<Int>? = null fun doSomething() { myList = ArrayList<Int>() myList!!.add(10) myList!!.remove(10) } } But if we cast it to ArrayList as below, the add and remove works. class TempClass { var myList: List<Int>? = null fun doSomething() { myList = ArrayList<Int>() (myList!! as ArrayList).add(10) (myList!! as ArrayList).remove(10) } } I just thought this is odd, as myList is

HashSet contains() method

让人想犯罪 __ 提交于 2019-12-22 05:18:37
问题 I executed below code and found the output was false . import java.util.Set; import java.util.HashSet; public class Name { private String first, last; public Name(String first, String last) { this.first = first; this.last = last; } public boolean equals(Object o) { if (!(o instanceof Name)) return false; Name n = (Name) o; return n.first.equals(first) && n.last.equals(last); } public static void main(String[] args) { Set<Name> s = new HashSet<Name>(); s.add(new Name("Donald", "Duck")); System

Unit Test IQueryable

别等时光非礼了梦想. 提交于 2019-12-22 05:16:29
问题 I am trying to write a unit test for a method which takes an IQueryable collection. How should I instantiate the collection in my unit test before I pass it to the method for test? This is the default code in my test IQueryable<Item> Items = null; // TODO: Initialize to an appropriate value And here is what I tried writing IQueryable<Item> Items = new IQueryable<Item>; // TODO: Initialize to an appropriate value Am I making a school boy error? 回答1: Well, you can use .AsQueryable() on any

Using 'map' with different sized collections in clojure

落花浮王杯 提交于 2019-12-22 05:01:01
问题 I'd like to understand the idiomatic way with which to operate over collections of different sizes in clojure. Is there a way I can tell the function 'map' to pad the rest of a collection with some default value? As an example, suppose I have 3 vectors: (def x [1 2 3 4]) (def y [1 2 3 4 5]) (def z [1 2 3 4 5 6 7]) (map + x y z) ; yields (3 6 9 12) In this case, how can I pad x and y with zeroes and have this yield: (3 6 9 12 10 6 7) 回答1: map doesn't do it itself, but you can use a combination

Order List by Date and Time (in string format)

你说的曾经没有我的故事 提交于 2019-12-22 04:58:23
问题 Probably what I'm asking is quite simple but I don't seem to be getting this one out. I have a list of elements containing a Date field and a Time field. The Date field is a regular DateTime and the Time field is a string. Time is formatted like HH:mm and ranges in 24h. Ordering my list by Date is simple by doing List.OrderBy(e => e.Date), but I don't seem to be able to later order it by Time so that the order of the records is according the date and the time. I tried this out but it's