I've created an Arraylist for Employee class and initialized values for the data members like Empid,Empname,DOJ,Salary...after initializing I want to sort with respect to any one particular field. Is this possible in collections or in any concept?
Thanks in advance
If you are using c# and Linq you can try with:
var List = TheList.OrderBy(p => p.Empid);
I believe that linq(in c#) has this orderby extension method
List<MyObj> ol = myList.OrderBy(myOb => myOb.AProperty).ToList();
Java:
If you always want to sort on the same field, have your class Employee implement Comparable<Employee>. Than you can use Collections.sort(List)
If you want to sort on different Field, implement different Comparator classes and use Collections.sort(List, Comparator)
... In C#, this is very easily doable via Linq - Also look here for more examples.
Related
I am adding an element into a MutableList and I want to know its index.
mutableList.add(foo)
Will the index of the most recently added element always be the last index mutableList.size - 1 like it is with ArrayList?
I don't want to use mutableList.indexOf(foo) because I believe it takes O(n). I'm not finding lot of documentation on these.
Yes, MutableList.add() always adds to the end of the list (this is a requirement for any class implementing the interface). I've filed an issue to say this explicitly in the documentation.
MutableList is merely an interface. How are you initializing the list?
Using Kotlin methods like mutableListOf() creates ArrayList objects by default.
You raise a question that has never occurred to me since from testing and experience I've found that all new elements added with the add method are placed at the end.
I believe that this is the case and won't change.
Although not mentioned in the documentation, this case holds. If it didn't then for sure it would be in the documentation. Just like Set where it says
A generic unordered collection
On JVM, MutableList is equivalent to java.util.List whose add documentation does specify
Appends the specified element to the end of this list
Is it possible to configure NHibernate (specifically Fluent NHibernate) to serialize an array of simple types to a single database column? I seem to remember that this was possible but its been a while since I've used NHibernate.
Essentially I need to store the days of week that a person works (int[]) and would rather not have a separate table just for this purpose.
It is possible.
You need to implement a IUserType that takes care of mapping between your array and a data column (google that first; it's possible that somebody already implemented it)
Alternatively, you can do the conversion in your entity class, and map the single-field representation instead of the property. For example:
string numbers;
public int[] Numbers
{
get { return numbers.Split(','); }
set { numbers = string.Join(",", value.Select(x => x.ToString())); }
}
Yes. There's UserType for scenarios like that. You could also use enum and bit flag.
I have a datatable which provides objects from a list. Within this data table I would like to use a tag like p:columns(primefaces) which provides strings from a list that represent the name of a field.
I will now need a subexpression to be able to use the dynamic field name like:
#{entry.#[column.fieldName}}
Is there any possibility to do this in JSF2?
If entry has get/set accessors for columns values, you could use this syntax :
#{entry[column.fieldName]}
In EL you can use two syntaxes to access to the value of the "lastname" property of an object :
#{myObject.lastname}
#{myObject["lastname"]}
You can take a look to JSPIntro at oracle.com
No, EL doesn't work like that.
What you could do to achieve the desired functionality would be this:
#{entry.getField(column.fieldName)}
where getField() is a method that uses reflection (perhaps via PropertyDescriptor) to access the field with the given name. However, this is an EL 2.2 feature, so you'll need a pretty recent EL implemetation, such as provided by Tomcat 7.
I have a class called PriceStep. I keep a list of PriceStep objects in a class called PriceStepSearchSpace. Now I am required to have different PriceStepSearchSpace objects for different products and I need to keep them in some sort of a dictionary. I called this new class PriceStepSearchSpaceRepository.
Can you think of a simpler/shorter name?
You could call it Repository and put it in a namespace called PriceSteps.Searchspaces.
I might call it PriceStepSearchSpaces if it was unlikely that I would have any other type of collection of those objects. Otherwise, I like Timwi's idea of putting related classes into a namespace to prevent duplication of prefixes.
I would go with SearchSpace for your first and SearchSpaceDictionary for the second.
There's no need to preface a parent class with it's child class name!
However, you may want to re-think your object model, it's hard to give advice about that based on the info you provided.
PriceStep. PriceSteps. PriceStepsByProduct.
In NHibernate, Is it possible to do a look up based on an entity passed?
I would like to pass an object from the ui and do a look up based on its values instead of having multiple methods for each possible variation.
For example if I pass a user with the firstname 'John', I'd like to return all users with that firstname.
Any hints much appreciated.
You can use Find by example method in Nhibernate.
Here :
var user=new User();
user.Firstname="John";
var criteria=session.CreateCriteria(typeof(User)).Add(Example.Create(user));
Example is a special kind of expression that builds criterion based on provided entity.