How to reverse the SingleLinkedList in the Java? [closed] - java

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 9 years ago.
Improve this question
Hi I want to print the SingleLinkedList in reverse.

static void Collections.reverse(List list):
http://download.oracle.com/javase/1.4.2/docs/api/java/util/Collections.html

See Collections.reverse (JavaDoc)

I don't know for "The SingleLinkedList" class (is it even a List implementation ?) but if it works as its name states, the best way (more effective one) would be to go through the data structure and put every element in a stack. Then simply read the stack.

public print(Element el) {
if (el.next!=null)
print(el.next);
System.out.print(el.value+" ");
}
Basicaly its puting it in (call) stack an reading it (as someone else sugested). Nonrecursive version using explicit stack is usualy faster, does not risk StackOverflowException and takes longer to write.

Related

What are the pros and cons of using Stream sorting over Collections sorting in Java? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Lets say I have a pojo Model as:
class Model{
String id;
String name;
}
List in Java, and I want to sort an already filled list List models.
For now, I'm considering two options:
Using Colletions.List :
models.sort(.sort(Comparator.comparing(Model::getId)))
Using sorted function of Java8 Stream API:
models..stream().sorted(Comparator.comparing(Model::getId)).collect(Collectors.toList())
Can anyone please explain pros and cons of using Method 2 over Method 1?
I believe the biggest difference is that if you use list.sort() it actually sorts the list. If you use list.stream().sorted() that returns a sorted list but doesn't actually sort the list you start from. There might be cases for both - depending on what you prefer.

Memory issues with substring [closed]

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
How does substring method work internally and how can it create memory issue ?
How can we solve it?
Prior to Java 7 udate 6, substring used to return a view on the original string. So imagine you had a String of 1,000,000 characters and called s.substring(0, 1) because you are only interested in the first character, the original string would have stayed in memory.
Since Java 7u6 substring returns a new string which prevents that issue.

Why doesn't java.lang.Short have a reverse() method similar to java.lang.Integer [closed]

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 8 years ago.
Improve this question
Why doesn't java.lang.Short (or Float or Double) class have a reverse method similar to java.lang.Integer ?
They both do have reverseBytes method though.
Why isn't the API list consistent ?
short can hold 2 bytes and it would have made sense to have a reverse method as well.
Wouldn't it ?
Thanks
While I agree on the API criticism, it's pretty simple to emulate:
short input = ...;
short reversed = (short)(Integer.reverse(input) >> 16);
So maybe the answer is:
Not enough people felt it was necessary
It's easy enough to simulate
Someone wanted to show off with the implementation of Integer.reverse()
Every line of code needs to maintained. Less code == less bugs, lower cost, easier maintenance.

I need a three-valued logic in Java [closed]

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
I have tried to return null as third value in my boolean function, but it won't compile. I need to return three values from my class method - true, false and null (for example). Is there any standard way how can I do it?
Please use an enumeration with three values defined. Hacking things together is no solution.
Similar question has been asked, it should help.
You can make your own POJO object with this logic in getXX() method. From your method return this POJO with value and test it in code.
Generaly, don't use null values as state indicators.

What's the best way to get Attribute value with Reflection in Java [closed]

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 1 year ago.
Improve this question
I know i can get the attribute names, types, and set their values from an Object.
I wanna know what's the best way to get their values via reflections.
EDIT:
For meaning. Best is like, less code, less memory and faster execution.
Like:
Is it better if I try to invoke the methods to get their values, or if I use something like this:
Object obj;
Class cls = obj.getClass();
cls.getField("atribute1").get(obj).toString();
The best way I know of to do that is to use Apache BeanUtils

Categories

Resources