Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I think it is better and faster to explain by one example, see the example below:
I have two constants: MY_PREFIX_CONSTANT1 and MY_PREFIX_CONSTANT2.
Then one method that puts those two constants in one array.
Then I add one third constant called: MY_PREFIX_CONSTANT3 but I forgot to add this third constant to the array in this method.
What I want to run is something like a check by which I can notify that the MY_PREFIX_CONSTANT3 constant was not added to the array.
Is this possible?
I would suggest using an enum:
public enum MyEnum {
VALUE1(1),
VALUE2(2),
VALUE3(3);
private final int id;
MyEnum(int id) {
this.id = id;
}
}
You can access all enum values as array using MyEnum.values()
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
In this example http://www.vogella.com/tutorials/JavaPersistenceAPI/article.html#jpaintro_entitymanager if an entity have a #OneToMany relationship. the List is initialized in the declaration like this:
#OneToMany(mappedBy = "family")
private final List<Person> members = new ArrayList<Person>();
I used to not initialize entities lists in my code, and always have to check if null. So My Question is: Is initializing lists (relationships) as an empty list in the declaration is what needs to be done normally or what ? and Why ?
What I'm currently doning
private final List<Person> members
In general "null" and "empty" have different meanings even if the behavior for jpa remains the same. Null means it was never initialized while empty means there are no entries. So if you need to handle those 2 cases different, you can do, if not it doesnt matter which way you go. For example null can mean, that you never load the data you need and you can load it after checking for null. While "empty" means you already load the data but there are no entries
You should ideally decide based on your application logic whether to keep this list as empty or null.
One disadvantage with null is that in the places where you are not expecting this list as null, you have to add extra null checks in the application code.
One better way is to initialize your list using Collections. EMPTY_LIST.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
If I use the below code
List<String> listOfStrings=new ArrayList<>();
listOfStrings.add("first string");
or the following code
List<String> listOfStrings;
listOfStrings.add("first string");
to create a Java list, both the codes get compiled successfully and give same output on iterating the list. So what is the relevance of initializing the list
If listOfStrings is a local variable, the second example won't compile: you have to definitely assign a value to a variable before you can use it.
It listOfStrings is a member or static variable, the second example would yield a NullPointerException because you're invoking the add method on a null reference.
In the first example, you are not initialising the list, you are instantiating a class or creating the new object, the list. In the second example you will receive a runtime error, because the variable listOfStrings is null.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a list<something> which passed to a method but the method expects like List<Iterator<something>>.
How could i achieve this?
Thanks
Your question is like asking
My client wants apples but I only have oranges. How can I turn my oranges into apples?
If the method requires a list of Iterator then give it a list of iterators! Don't give it a list of objects!
If you have this problem, you must be using the method in the wrong way. But you didn't tell me what the method is. So I can't tell you how to use the method in the correct way.
But anyway, if you really want to do this, here's how
//Assume your list is called "list"
List<Iterator> newList = new ArrayList<>();
for (Object item : list) {
newList.add((Iterator)item);
}
Note: this might result in ClassCastException.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
List<Object> pairs1 = new ArrayList<Object>();
Pair p =new Pair();
p.date="2014-10-10 10:10:10";
p.no=11;
pairs1.add(p);
I created a list like this; now i want to sort it using date without using Collections.sort
Or
Show me how to get only date from list object
If you really want to keep the date implemented as a string and assuming Pair is a class you wrote yourself:
let it implement Comparable
write a suitable implementation of compareTo(Object o)
use Collections.sort(pairs1)
Two ways you can do:
Implement Comparable for your Pair class and implement compareTo method by comparing two dates and then call Collections.sort(pairs1);
Create another class which implements Comparator and implement compare method and then call Collections.sort(pairs1, mycomparator);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Friends
Im posting my question again as previous question was unclear .
I want to pass an object of an class who's member variables are string , Listof A String and a List of an class member variable to a method that copies the contents of above class objects and returns a Lsit of class object which can be used by an java method.
Any example will be nice
Now I have a
Where is the class for CallFlowResource??
A simple way would be for you to create a list and add the values as you want them to be outputted.
It you would look like this:
public List<CallFlowResource> getCallFlow(CallFlowObject obj)
{
List<CallFlowResource> callFlowRes = new ArrayList<>();
for(int i = 0; i<size; i++)
{
callFlowRes.add(obj.GetterMethodForCFR(i));
}
return callFlowRes;
}