collections

How to Split odd and even numbers and sum of both in collection using Stream

那年仲夏 提交于 2019-12-19 04:54:09
问题 how can I Split odd and even numbers and sum both in collection using Stream method of java-8 ?? public class SplitAndSumOddEven { public static void main(String[] args) { // Read the input try (Scanner scanner = new Scanner(System.in)) { // Read the number of inputs needs to read. int length = scanner.nextInt(); // Fillup the list of inputs List<Integer> inputList = new ArrayList<>(); for (int i = 0; i < length; i++) { inputList.add(scanner.nextInt()); } // TODO:: operate on inputs and

WPF PropertyGrid - adding support for collections

痞子三分冷 提交于 2019-12-19 04:43:10
问题 I am working on wpf PropertyGrid (PG) control and I want the PG to support collection type( IList , ObservableCollection etc.) properties. I am bit confused on how to keep track of selected item(of that collection) and pass that to client. Any ideas? If the solution makes use of the Open Source WPF PropertyGrid (http://www.codeplex.com/wpg) I will implement the changes /additions back into the control. 回答1: No answers proves that there is no straight forward way of doing this. So I

Logic and its application to Collections.Generic and inheritance

爱⌒轻易说出口 提交于 2019-12-19 04:02:59
问题 Everything inherits from object. It's the basis of inheritance. Everything can be implicitly cast up the inheritance tree, ie. object me = new Person(); Therefore, following this through to its logical conclusion, a group of People would also be a group of objects: List<Person> people = new List<Person>(); people.Add(me); people.Add(you); List<object> things = people; // Ooops. Except, that won't work, the people who designed .NET either overlooked this, or there's a reason, and I'm not sure

java: create a lookup table using hashmap or some other java collection?

醉酒当歌 提交于 2019-12-19 03:56:57
问题 Extending this question, I have accepted an answer which says use lookup table or hashmap for this case as it would be a better construct to handle multiple conditions . Current construct. Class to store messages. public class ProgressMessages { public static String msg1="Welcome here ....."; . . . //around 100 more similar static variables. } Condition and Display the proper message from the above class. int x=calculatedVal1(m,n); int y=calculatedVal2(o,q); SimpleDateFormat formater=new

Get all possible partitions of a set

耗尽温柔 提交于 2019-12-19 03:18:04
问题 In Java I have a set where I want to obtain all possible combinations of subsets which their union make the main set. (partitioning a set) for example, given: set={1,2,3} the result should be: { {{1,2,3}} , {{1},{2,3}} , {{1,2},{3}} , {{1,3},{2}}, {{1},{2},{3}}} the number of possible partition of a set of n elements is B(n) known as Bell number. The code so far: public static <T> Set<Set<T>> powerSet(Set<T> myset) { Set<Set<T>> pset = new HashSet<Set<T>>(); if (myset.isEmpty()) { pset.add

How can I modify a collection while also iterating over it?

限于喜欢 提交于 2019-12-19 02:54:24
问题 I have a Board (a.k.a. &mut Vec<Vec<Cell>> ) which I would like to update while iterating over it. The new value I want to update with is derived from a function which requires a &Vec<Vec<Cell>> to the collection I'm updating. I have tried several things: Use board.iter_mut().enumerate() and row.iter_mut().enumerate() so that I could update the cell in the innermost loop. Rust does not allow calling the next_gen function because it requires a &Vec<Vec<Cell>> and you cannot have a immutable

What to pass to the Arrays instance method toArray(T[] a) method? [duplicate]

我是研究僧i 提交于 2019-12-19 02:34:26
问题 This question already has answers here : .toArray(new MyClass[0]) or .toArray(new MyClass[myList.size()])? (9 answers) Closed last year . If you have an instance of a Collection, say something like: Collection<String> addresses = new ArrayList<String>(); Which were to then be populated with a bunch of values, which is the "best" way, if any, to make use of the toArray() method without requiring a type cast? String[] addressesArray = addresses.toArray(new String[] {}); String[] addressesArray

sort concurrent map entries by value

五迷三道 提交于 2019-12-19 02:27:23
问题 Is there any way to create a thread-safe implementation of Map that maintains it's entries sorted by value? I know I can create a thread-safe Map like this ConcurrentMap<String, Double> rankings = new ConcurrentHashMap<String, Double>(); And I can then get the entries sorted by value by passing it to a utility method like this: public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) { List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>(map.entrySet());

Can't convert ListBox.ObjectCollection to a (typed) array

房东的猫 提交于 2019-12-18 23:38:22
问题 I want to convert the items to a String array or the type that I used to fill the ListBox.DataSource. The type has overridden ToString() but I can't seems to get it converted, not even to String[]. String[] a = (String[])ListBox1.Items; Contacts[] b = (Contacts[])ListBox1.Items; 回答1: string[] a = ListBox1.Items.Cast<string>().ToArray(); Of course, if all you plan to do with a is iterate over it, you don't have to call ToArray(). You can directly use the IEnumerable<string> returned from Cast

What's design pattern does Collections.sort use?

别等时光非礼了梦想. 提交于 2019-12-18 21:32:29
问题 When applying a comparator to a list in the following manner, what is the design pattern being used or what is the technique being used here? Collections.sort(myCollection, new Comparator<MyItem>() { @Override public int compare(MyItem item1, MyItem item2) { return item1.getId().compareTo(item2.getId()); } }); 回答1: TL;DR : Collections.sort is an example of a simple polymorphic substitution regardless of whether you use Functional Programming or Object Oriented Programming to make this