using set or List here in the code - java

I am newbie to java, I have a scenario, where i need to list the organisation types from the table:
Requirement : Just listing, no add or removing the elements,
As i understand the difference between set and list:
Set:
Set is Unique collection of Objects.
Set is Un-ordered collection of Objects.
List:
List is non-unique collection of Objects.
List is ordered collection of Objects.
In my table i am having columns like:
id name is_active
1 Lab 1
2 Pharmacy 2
3 Hospital 3
Maximum 10 rows
**Controller**:
List<OrgType> orgTypeList = organizationService.getAllOrgTypes(true);
OrgTypeResponse response = new OrgTypeResponse();
List<EntityDetail> orgTypeDetailList = new ArrayList<>();
EntityDetail orgTypeDetail;
for(OrgType orgType : orgTypeList) {
orgTypeDetail = new EntityDetail();
orgTypeDetail.setId(orgType.getId());
orgTypeDetail.setName(orgType.getName());
orgTypeDetailList.add(orgTypeDetail);
}
response.setStatus(ResponseStatusCode.SUCCESS);
response.setTotalOrgTypes((long)orgTypeDetailList.size());
response.setOrgTypes(orgTypeDetailList);
return response;
**Service** Implementaion:
List<OrgType> orgTypeList = orgTypeRepository.findByActive(active);
return orgTypeList;
This is my EntityDetail class:
public class EntityDetail {
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
My question here is, can i use the Set instead of List
If Set is used, can i use TreeSet, because i need to show in the asc order of id
Or Leave the code, as it is
i just want the clarification,
Thanks

You can use any of them but things to be kept in consideration:
Set although provides unique data, but that also has a cost.
In case, you are sure that table has unique names of organizations then you should opt for list.
It seems like you are using Spring with JPA, if that is the case, then you can use SORT interface(org.springframework.data.domain.Sort) to get sorted data.

My question here is, can i use the Set instead of List
Yes, without problem, just implement methods equals and hashCode.
If Set is used, can i use TreeSet, because i need to show in the asc order of id
You can if class EntityDetail implements interface Comparable<EntityDetail>. This is necessary because TreeSet must know what is the natural order of the various EntityDetail objects.
For more details please see Oracle docs on object ordering and Javadoc for Comparable

yes u can use SET instead of List in this scenario because SET will ensure that duplicate entries are eliminated. But making use of SET make sure that you have overridden "equals" and "hashcode" appropriately.

This is how you need to override equals and hashcode methods and for sorting purpose you need to implement Comparable and implement compareTo method as follows:
class EntityDetail implements Comparable<EntityDetail>{
#Override
public int hashcode(){
int result = 17;
result = 31 * result + name.hashCode();
result = 31 * result + id;
return result;
}
#Override
public boolean equals(Object o){
if (o == this) return true;
if (!(o instanceof EntityDetail)) {
return false;
}
EntityDetail ed = (EntityDetail) o;
return ed.name.equals(name) &&
ed.id == id ;
}
#Override
public int compareTo(EntityDetail ed) {
int compareId = ((EntityDetail) ed).getId();
//ascending order
return this.id - compareId;
//descending order
//return compareId - this.id;
}
}

You can use List if you can make sure in your code that the details are added in it in the order that you want. If you are not sure of the order in which you add then you can use the Collections.sort method. For this you will also want to make your OrgType implement the Comparable interface to provide a strategy to order the OrgType objects. In your case it is by id.
If you use TreeSet, the sorting is done automatically whenever you insert into the set thereby eliminating the use of Collections.sortbut you will still have to provide an ordering strategy.
Have a look at this
There are costs of using a Set because it maintains unique elements but because you have a maximum of 10 rows that won't be a problem.

Related

Descending sorting LDAP with Spring Boot

Is it possible to sort records from LDAP directory with SortControlDirContextProcessor descending, not ascending?
I based on this:
SpringLdap - LdapTemplateSortedSearchITest
public void testSearch_SortControl_ConvenienceMethod() {
SortControlDirContextProcessor requestControl;
// Prepare for first search
requestControl = new SortControlDirContextProcessor("cn");
tested.search(BASE, FILTER_STRING, searchControls, callbackHandler,
requestControl);
int resultCode = requestControl.getResultCode();
boolean sorted = requestControl.isSorted();
assertThat("Search result should have been sorted: " + resultCode, sorted).isTrue();
List list = callbackHandler.getList();
assertSortedList(list);
}
It works but ascending. How to set descending sort?
I believe it could be helpful:
public SortKey(String attrID,
boolean ascendingOrder,
String matchingRuleID)
Creates a sort key for an attribute. Entries will be sorted according to the specified attribute in the specified sort order and using the specified matching rule, if supplied.
Parameters:
attrID - The non-null ID of the attribute to be used as a sort key.
ascendingOrder - If true then entries are arranged in ascending order. Otherwise there are arranged in descending order.
matchingRuleID - The possibly null ID of the matching rule to use to order the attribute values. If not specified then the ordering matching rule defined for the sort key attribute is used.
It is from docs: Java doc
Regarding your question: Example code from java doc
And I found one more alternative called unboundid ldap sdk
link
The solution for me was to implement a custom DirContextProcessor that allowed me to sort on multiple attributes with the desired direction (ascending/descending) by making use of the overload method of the SortControl class that takes an array of SortKeys objects as a parameter.
The implementation must extend the AbstractFallbackRequestAndResponseControlDirContextProcessor and override the createRequestControl method.
The superclass AbstractFallbackRequestAndResponseControlDirContextProcessor will take care of the actual creation of the control. It only expects 2 pieces of information from the subclass.
The fully qualified class name of the control to instantiate
The types and values of the constructor parameters
The fully qualified class name is provided in the subclass property defaultRequestControl, and the types and values of the constructor parameters are provided in the subclass method createRequestControl.
The information of the sorting direction for any particular attribute is provided in the ascendingOrder property of the SortKey object.
public class SortMultipleControlDirContextProcessor extends AbstractFallbackRequestAndResponseControlDirContextProcessor{
private SortKey[] sortKeys;
private boolean sorted;
private int resultCode;
public SortMultipleControlDirContextProcessor(SortKey ... sortKeys){
if(ArrayUtils.isEmpty(sortKeys)){
throw new IllegalArgumentException("At least one key to sort on must be provided.");
}
this.sortKeys = sortKeys;
this.sorted = false;
this.resultCode = -1;
this.defaultRequestControl = "javax.naming.ldap.SortControl";
this.defaultResponseControl = "javax.naming.ldap.SortResponseControl";
this.fallbackRequestControl = "com.sun.jndi.ldap.ctl.SortControl";
this.fallbackResponseControl = "com.sun.jndi.ldap.ctl.SortResponseControl";
loadControlClasses();
}
#Override
public Control createRequestControl(){
return super.createRequestControl(new Class[]{SortKey[].class, boolean.class}, new Object[]{sortKeys, critical});
}
#Override
protected void handleResponse(Object control) {
Boolean result = (Boolean) invokeMethod("isSorted", responseControlClass, control);
this.sorted = result;
Integer code = (Integer) invokeMethod("getResultCode", responseControlClass, control);
this.resultCode = code;
}
public SortKey[] getSortKeys(){
return sortKeys;
}
public boolean isSorted(){
return sorted;
}
public int getResultCode(){
return resultCode;
}
}
After the implementation, you can use the class to sort the results on multiple attributes in any desired direction:
// SortKey for sorting results on the cn attribute in descending order
SortKey cnSortKey = new SortKey("cn", false, null);
// Instantiate the control
SortMultipleControlDirContextProcessor myCustomControl = new SortMultipleControlDirContextProcessor(cnSortKey);
// Perform the search with the control
List<User> users = ldapTemplate.search("", orFilter.encode(), searchControls, new UserAttributesMapper(), myCustomControl);

Find index of an object in a list

I have situation where I have a list(required items) that holds a table column result like:
NAME
ADDRESS
AGE
.
.
etc
In my method I get a User object that contains values for user.getName(), user.getAge() etc. I want to know the best way to ensure that every item in the list is present in the user object. The no of items in the list are variable.
public boolean isUserInfoComplete(User user, ArrayList list){
//so, if the list has AGE, the user.getAge() must have some value
}
One way I thought of is maintaining another list that holds values of every user info and checking that against my db list but that is not scalable.
It's not possible to dynamically match your method names with the list contents without reflection (which can be expensive and fragile). You may want to consider keeping your User values in a central Map cache. Here's one way to do that:
public class User {
private enum Field {
NAME,
AGE
//...
}
private Map<String, Object> values = new HashMap<>();
private void putValue(Field field, Object value) {
values.put(field.name(), value);
}
private Object getValue(Field field) {
return values.get(field.name());
}
public void setName(String name) {
putValue(Field.NAME, name);
}
public String getName() {
return (String)getValue(Field.NAME);
}
public void setAge(int age) {
putValue(Field.AGE, age);
}
public Integer getAge() {
return (Integer)getValue(Field.AGE);
}
//...
public boolean isUserInfoComplete(List<String> fields) {
return values.keySet().containsAll(fields);
}
}
You could use reflection to solve this problem if the items in the list match the getters in your User object.
For example, if AGE is in the list, you could use reflection to look for the getAge() method on the User class, call it on the object, and then check the result for null (or switch on the method return type to perform other types of checks).
Here's a starting point for you to experiment with (I haven't compiled or tested it):
public boolean isUserInfoComplete(User user, ArrayList list){
for(String attribute : list) {
String methodName = "get" + attribute.substring(0, 1).toUpperCase() + attribute.substring(1).toLowerCase();
Method method = User.class.getMethod(methodName, null);
if(method != null) {
Object result = method.invoke(user);
if(result == null) {
return false;
}
}
}
return true;
}
This seems like a case where you need reflection. This gives you the opportunity to inspect methods and field from your objects at runtime.
If you know your User-objects etc will follow a java bean standard then you will be able to use the getters for checking, though I see now problem in making your fields public final and checking directly on the fields themselves.
Take a look at https://docs.oracle.com/javase/tutorial/reflect/
You can check it using contains() while looping. This process will be very resource-consuming.
Maybe you can redesign something and simply compare two User objects? Will be faster. You can do it by providing your own implementation of equals and hashcode methods.

Java How To Keep Unique Values In A List of Objects

I currently am retrieving a list of objects List<NprDto> (The NprDto class contains accountId, theDate1, and theDate2) from a query that returns results where the NprDto has duplicate accountIds. I need to have a List<NproDto> of only unique accountIds but keep the object. It only needs to add the first accountId it comes across and ignores the rest.
I'm currently trying this:
private List<NprDto> getUniqueAccountList(List<NprDto> nonUniqueAccountList) throws Exception {
Map<Long,NprDto> uniqueAccountsMapList = new HashMap<Long,NprDto>();
List<NprDto> uniqueAccountsList = null;
if(nonUniqueAccountList != null && !nonUniqueAccountList.isEmpty()) {
for(NprDto nprDto : nonUniqueAccountList) {
uniqueAccountsMapList.put(Long.valueOf(nprDto.getAccountId()), nprDto);
}
}
uniqueAccountsList = new ArrayList<NprDto>(uniqueAccountsMapList.values());
return uniqueAccountsList;
}
But this doesn't seem to be working because when I iterate through the returned uniqueAccountsList later it only picks up the first object.
Any help would be greatly appreciated.
I need to have a List of only unique accountIds but keep the
object.
You should use Set<NprDto>. For that you need to override equals and hasCode at NproDto class.
class NprDto{
Long accountId;
.......
#Override
public boolean equals(Object obj) {
NproDto other=(NproDto) obj;
return this.accountId==other.accountId;
}
#Override
public int hashCode() {
return accountId.hashCode();
}
}
Change your getUniqueAccountList as follows:
private Set<NprDto> getUniqueAccountSet(){
Map<Long,NprDto> uniqueAccountsMapList = new HashMap<Long,NprDto>();
Set<NprDto> uniqueAccs = new HashSet<NprDto>(uniqueAccountsMapList.values());
return uniqueAccs;
}
What you need here is a LinkedHashSet. It removes duplicates and keeps insertion order.
You do not need TreeSet here because it sorts and changes the order of the original List.
If preserving insertion order is not important use a HashSet.
Actually you need to implements equals and hascode method, it will be good for you
Remove duplicates from a list
Java Set contains the Unique value but its unsorted collection. List is sorted collection but contains the duplicates objects.
What you need to do is implement the equals, hashCode and compareTo methods for NprDto to match two objects as equal when their ID is the same. Then you can filter all duplicates as easy as this:
private List<NprDto> getUniqueAccountList(List<NprDto> nonUniqueAccountList) {
return new ArrayList<NprDto>(new LinkedHashSet<NprDto>(nonUniqueAccountList));
}

Passing 2 generics in Priority Queue

I want to keep two things in my priority queue...one is a number and the other is cost. i.e. I want to do the following:
PriorityQueue<Integer, Cost> q=new PriorityQueue<Integer, Cost>();
Cost is another class that i hav:
class Cost implements Comparable<Cost>
{
String name;
double cost;
#Override
public int compareTo(Cost s)
{
return Double.compare(cost, s.cost);
}
}
Also I want to perform comparisons only based on cost...but I also want some integer identifier to be passed along with cost...is there some way to achieve this?
i need to retrieve Cost based on id..therefore I am using a hash map for it. When using an id field in cost...i want to retrieve the entire cost instance based on that id field...is it possible...is yes, then how?
I am a novice at Java programming. Can someone pls suggest some way out?
Change your Cost class
public class Cost implements Comparable<Cost> {
String name;
double cost;
int id;
public Cost(int id, String name, double cost) {
this.id = id;
this.name = name;
this.cost = cost;
}
#Override
public int compareTo(Cost s) {
return Double.compare(cost, s.cost);
}
public int getId() {
return this.id;
}
#Override
public String toString() {
return new StringBuilder().append("id : ").append(id).append(
" name: ").append(name).append(" cost :").append(cost)
.toString();
}
}
Then you can simply declare PriorityQueue of Const
PriorityQueue<Cost> q=new PriorityQueue<Cost>();
Now when you want to find Cost based on id you can do below
PriorityQueue<Cost> queue = new PriorityQueue<Cost>();
queue.add(new Cost(1, "one", 1));
queue.add(new Cost(2, "two", 2));
int id = 2;// Id to be found
for (Cost cost : queue) {
if (cost.getId() == 2) {
System.out.println(cost);
}
}
The Cost object is a good start. Make an object that contains both an integer and a Cost, and put those in the priority queue. Or, add an integer field to the Cost class itself.
You may want to wrap your integer and cost in a Map/HashMap as below:
PriorityQueue<Map<Integer, Cost>> q = new PriorityQueue<Map<Integer, Cost>>();
Now you would be able to create a HashMap object and put you two object in that before putting in the queue.
Also, you want to create a custom wrapper Class e.g. CostNumber which will have Integer and Cost as two member variables. Once done ,you can use that new object in the queue.
Since PriorityQueue stores a single object, you need to do one of the following:
create a class that contains both the integer and the cost object, iff integer and cost are unrelated.
push the integer attribute as another member of Cost class iff they are related.
Also I want to perform comparisons only based on cost...but I also want some integer identifier to be passed along with cost...is there some way to achieve this?
Why would you want to pass something to compareTo that you are not going to use during comparison? In any case, the signature of this method cannot be changed if you want to leverage the Comparator framework. You can add that integer identifier to your Cost class itself as another member and thereby make it available during compareTo method execution.

Benefits of factoring a new class vs string values for elements in a Set

This is more of a design question with implications for code simplicity vs. performance.
Lets say you want to make sure a set of values for a given user id are the same between two systems. The example here is to check that a student id has the same number of course enrollments in System A and System B.
For this we create:
List<String> studentList = new ArrayList<String>();
Set<String> sysAEnrollments = new HashSet<String>();
Set<String> sysBEnrollments = new HashSet<String>();
private Map<String, String> badEnrollList = new HashMap<String, String>();
And fill them appropriately, given a list of student ids(studentList):
studentList = getCurrentStudentList();
for (String id : studentList){
sysAEnrollments = getSysAEnrollments(id);
sysBEnrollments = getSysBEnrollments(id);
if (!sysAEnrollments.containsAll(sysBEnrollments)){
badEnrollList.put(id, getBadEnrollmentsById(id, sysAEnrollments, sysBEnrollments));
}
}
Question: What should the method 'getBadEnrollmentsById' return?
Either a concatenated string with enough meaning so it can just be printed out.
Or have a new object, for example another collection with the list of course ids that could be used for further processing but harder to use for printed output.
Is it worth designing thoroughly all expected objects or replace some of them with concatenated strings for clarity and performance?
NOTES:
System A is preferred as the authoritative source
Output from getBadEnrollmentsById should have all courses and flag those missing in system B.
PROPOSED SOLUTION: (2012-SEP-14)
EDIT (2012-SEP-17): Updated the Course class to include hashCode and equals
As suggested by user351721 I continued modelling the remaining objects that match the expected results/requirements.
Slight changes made a big difference and allowed me to go over this design flaw and finish with the implementation.
The revised collections are:
List<String> studentList = new ArrayList<String>();
Enrollment sysAEnrollments;
Enrollment sysBEnrollments;
Map<String, List<String>> badEnrollList = new HashMap<String, List<String>>();
And we populate the Enrollments:
for (String id : studentList){
sysAEnrollments = getSysAEnrollments(id);
sysBEnrollments = getSysBEnrollments(id);
if (!sysAEnrollments.getCourses().containsAll(sysBEnrollments.getCourses())){
List<String> missingCourses = getProblemEnrollmentListById(id, sysAEnrollments, sysBEnrollments);
badEnrollList.put(id, missingCourses);
}
}
So for now the output can be printed from badEnrollList by getting at each ArrayList and printing the course names. A course name with a * will mean that it's missing in sysB.
The Enrollment class looks like this:
public class Enrollment {
private Set<Course> courses = new HashSet<Course>();
public void setCourses(Set<Course> courses){
this.courses = courses;
}
public Set<Course> getCourses(){
return this.courses;
}
}
And the Course class ended up like this:
public class Course {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(final String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
// Must override hashCode() and equals()
#Override
public boolean equals(Object o){
if (o == this)
return true;
if (!(o instanceof Course))
return false;
Course c = (Course) o;
return c.id.equals(this.id) && c.name.equals(this.name);
}
#Override
public int hashCode(){
// Magic numbers as shown on Joshua Bloch's book "Effective Java" 2nd Edition, p.48
int result = 17;
result = 31 * this.id.hashCode();
result = 31 * this.name.hashCode();
return result;
}
}
The changes might look subtle but the important clue is that Enrollments are not a collection of strings, Enrollments are a collection of Courses AND each Course has a name and a availability property. They don't seem to do much but by using them I am defining the objects that I'm working with and documenting how these classes can be reused in the future.
"Growing Object-Oriented Software, Guided by Tests" addresses this question: chapter 7, "Value Types". Worth reading. An excerpt:
The more code we write, the more we’re convinced that we should define types to represent value concepts in the domain, even if they don’t do much. It helps to create a consistent domain model that is more self-explanatory. If we create, for example, an Item type in a system, instead of just using String, we can f ind all the code that’s relevant for a change without having to chase through the method calls
concatenated strings
would mean you have to define a pattern and corresponding set of valid strings and implement validation and translation to entity classes. Providing an interface or class would make it easier to update your code in a year or so, not to mention other programmers that might work with your application. Why not store student, enrollment or course objects in badEnrollList? How do these objects look like and what do you want to do with them?
In general: Yes, designing thoroughly all expected objects is worth it.
I feel that a collection, such as List<String> would be a desirable return value. This allows you to more efficiently capture multiple discrepancies between the two sets, and process the missing courses in your second object more intuitively. Printing the list wouldn't be that hard, either - depending on how you wished to convey the information.
It's also worth mentioning that the .equals() method for Set is a cleaner and more intuitive way to ensure equivalence between two sets.
Instead of using all these sets and maps, I'd use Plain Old Java Objects (POJOs) that reflect the actual business objects in question. From what you've indicated, you have Students who have an id of some sort, and who are enrolled in classes on System A and on System B. I would build up a set of Student objects defined like so:
public class Student {
private String id;
private List<String> enrollmentsA;
private List<String> enrollmentsB;
// appropriate getters and setters
}
Depending on if you want to do anything else with Classes, it may even be preferable to create some form of EnrolledClass object to represent that too.
Within the students class, I'd then have a method that would determine the "bad" enrollments. If all that you want to do with this data is generate an email message, it may even be as simple as a String:
public String getBadEnrollmentsMessage() {
List<String> enrolledBoth = getCommonEnrollments();
List<String> enrolledOnlyA = getAOnlyEnrollments();
List<String> enrolledOnlyB = getBOnlyEnrollments();
StringBuilder output;
// format the contents of the above lists into output
// format should be however you want it in the email.
return output.toString();
}
Then you could have a map of Students to email enrollments messages:
HashMap<Student, String> studentEmails;
for (Student s : allStudents) {
studentEmails.put(s, s.getBadEnrollmentsMessage());
}
Of course, if you have a method like getBadEnrollmentsMessage(), I'm not even sure you need the Map of students and strings in the first place. Frankly you could just create a sendEnrollmentEmail method, pass in a Student, and extract the message via getBadEnrollmentsMessage() right there.

Categories

Resources