collections

Merging arrays based on a value of the key

别来无恙 提交于 2020-01-20 06:01:11
问题 I have two arrays of arrays that have an id key, and I'd like to merge the data together based on that array's key and key value. The data would look something like: $color = [ ['id' => 1, 'color' => 'red'], ['id' => 2, 'color' => 'green'], ['id' => 3, 'color' => 'blue'], ]; $size = [ ['id' => 1, 'size' => 'SM'], ['id' => 2, 'size' => 'XL'], ['id' => 3, 'size' => 'MD'], ['id' => 4, 'size' => 'LG'], ]; $combined = [ ['id' => 1, 'color' => 'red', 'size' => 'SM'], ['id' => 2, 'color' => 'green',

how to check if all elements of java collection match some condition?

荒凉一梦 提交于 2020-01-20 03:52:06
问题 I have an ArrayList<Integer> . I want to check if all elements of the list are greater then or less then certain condition. I can do it by iterating on each element. But I want to know if there is any method in Collection class to get the answer like we can do to find maximum or minimum with Collections.max() and Collections.min() respectively. 回答1: If you have java 8, use stream's allMatch function (reference): ArrayList<Integer> col = ...; col.stream().allMatch(i -> i>0); //for example all

How to create List of open generic type of class<T>?

梦想的初衷 提交于 2020-01-19 06:24:18
问题 i am working on someone else code i need to add few things i have a class public abstract class Data<T> { } public class StringData : Data<string> { } public class DecimalData : Data<decimal> { } in my program i want to maintain list of different type of data List<Data> dataCollection=new List<Data>(); dataCollection.Add(new DecimalData()); dataCollection.Add(new stringData()); List<Data> dataCollection=new List<Data>(); at above line i am getting compiler error Using the generic type 'Data'

How to sort a HashSet?

混江龙づ霸主 提交于 2020-01-19 04:42:06
问题 For lists, we use the Collections.sort(List) method. What if we want to sort a HashSet ? 回答1: A HashSet does not guarantee any order of its elements. If you need this guarantee, consider using a TreeSet to hold your elements. However if you just need your elements sorted for this one occurrence, then just temporarily create a List and sort that: Set<?> yourHashSet = new HashSet<>(); ... List<?> sortedList = new ArrayList<>(yourHashSet); Collections.sort(sortedList); 回答2: Add all your objects

How to Maintain order of insertion [duplicate]

时光总嘲笑我的痴心妄想 提交于 2020-01-19 00:51:12
问题 This question already has answers here : Java Class that implements Map and keeps insertion order? (9 answers) Closed 20 days ago . I want to add a key, value pair into a hashtable (or any other collection) but have to maintain insertion order. How can I do this? Like I'll add 1 as key "one" as value, 2 as key and "two" as value. The output should be: 1:one 2:two 回答1: Here are the characteristic differences of some important Map implementations: LinkedHashMap: "with predictable iteration

How to Maintain order of insertion [duplicate]

不问归期 提交于 2020-01-19 00:47:12
问题 This question already has answers here : Java Class that implements Map and keeps insertion order? (9 answers) Closed 20 days ago . I want to add a key, value pair into a hashtable (or any other collection) but have to maintain insertion order. How can I do this? Like I'll add 1 as key "one" as value, 2 as key and "two" as value. The output should be: 1:one 2:two 回答1: Here are the characteristic differences of some important Map implementations: LinkedHashMap: "with predictable iteration

Databind based on items in a collection matching some criteria

耗尽温柔 提交于 2020-01-17 12:37:06
问题 Suppose I have a basic control with a listbox and a text box, where the listbox is bound to a collection of objects and has a basic data template <DockPanel LastChildFill="True"> <TextBlock DockPanel.Dock="Top">Book name</TextBlock> <TextBox x:Name="bookNameTextBox" DockPanel.Dock="Top" /> <TextBlock DockPanel.Dock="Top">Authors</TextBlock> <ListBox ItemsSource="{Binding Authors}"> <ListBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Name}" /> </DataTemplate> </ListBox.ItemTemplate

MONGO get only the name of documents but not the whole documents

独自空忆成欢 提交于 2020-01-17 01:48:06
问题 query mongo to find only the names : in such a way that it should be fast, ie i don't want to get each document and then get the name from each of them. I am new to mongo, db.company.find() --> this will give whole document Which is VERY BIG NOTE ; i have used indexing to the company name & also the unique condition too. soo --> i think it should help me finding the company name fast and easily , BUT i don't know how this is the collection collection company : --> list of companies in the

arraylist sorting in order

删除回忆录丶 提交于 2020-01-16 18:18:23
问题 Attempting to sort arraylist in alphabetical order but the Collection.sort is giving me an error which i do not what to do with it import java.util.ArrayList; import java.util.Collection; /** * * @author user */ public class BookShelf { ArrayList<Book> listOfBooks = new ArrayList<Book>(); public void addBook(Book book) { listOfBooks.add(book); } public ArrayList<Book> returnListOfBooks() { ArrayList<Book> myBook = new ArrayList<Book>(listOfBooks); Collection.sort(myBook); return myBook; } any

fetch the entire nested model in a single call EF6 MySQL

余生长醉 提交于 2020-01-16 08:21:07
问题 How we can fetch the entire model in a single fetch. Say model is Model.MInner1.MInner2.MInner3 All the inner objects are List. I have only DbSet of Models and need to fetch entire model by loading all inner collection objects. I am getting this correctly with the statement below, also Object1 is loaded correctly. List<Model> models = dbContext.Models.Include("MInner1.Object1") .Include("MInner1.MInner2.MInner3").ToList(); MInner3 collection contains 2 object that need to be loaded. Like