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 5 years ago.
Improve this question
Is it possible to implement a custom Stream?
I would like to create a StringStream in addition to IntStream or LongStream.
I would like to add extra methods, like storeToFile(path).
I thought it is a lot of work to implement a Stream. The standard implementation is java.util.stream.ReferencePipeline, but I cannot instantiate it directy.
Is there a simple solution?
Thanks for help.
It sounds like you want a Collector that is custom, not a Stream. You can create a Stream out of anything with Stream.of(), but you would need collectors for this.
storeToFile would be a StoringToFileCollector or something...
Yeah, you can create a stream of anything you want.
Extend the interface BaseStream
public interface MyStream extends BaseStream<MyClass,MyStream>
Then implement it
public class DefaultMyStream implements MyStream
{
//...
}
Probably crosscheck another stream implementation when writing your implementation.
I don't know that there's necessarily a huge advantage to doing this. Maybe there's a valid use case. In any case, it's possible.
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 8 months ago.
Improve this question
While using streams, I learned how to handle exceptions using functional interfaces and wrapper functions.
If so, what is the best way to structure the package?
Is it defined together in the class where stream is used? Or should I create a separate common class and define the wrapper function in it?
Thanks!
I think it depends. If you have one instance of using this technique, then it probably makes sense to simply use an functional interface and a wrap function that are part of the class which utilizes it. If you are using this same pattern in several places (and they all have the same function interface signature) then you can define the functional interface at the package level and put the wrap function in a utility class.
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 6 years ago.
Improve this question
How can I, and What is the best/proper way (ie, most performant and clearest syntactically) in Java to create object instances based on a prototype object instance, when this will occur repeatedly and in a performance critical code path?
I have thought about cloning via a cloning support library, but is that the best/only way? (These need to be arbitrary objects, btw, not ones that implement Clonable).
To clarify what I mean: I have an existing instance of Class T, which has fields set on it, and I want to pop out many versions of the same object to use separately, with the best performance and syntactic clarity possible.
Thanks.
Create a builder, which receives this class instance:
Person newOne = new PersonBuidler(oldOne).setAge(42)
Implementation of this builder may use apache common BeanUtils for cloning Java Beans or some other utility library for cloning arbitrary class.
See How do I copy an object in Java?
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
I wanted to know can we change the way serialization works in java , like say if we have two fields in a class and we create an object of the class. Can we change the order in which the fields of the object is serialized ? also is this any way related to writeObject() method that is used to write the data into a .ser file..?? please help
Thanks in advance....
I wanted to know how to do custom serialization..
use Interface Externalizable. It will give you more control over your serialization and de-serialization process .
http://docs.oracle.com/javase/7/docs/api/java/io/Externalizable.html
[code example ] http://www.jusfortechies.com/java/core-java/externalization.php
from docs
Only the identity of the class of an Externalizable instance is written in the serialization stream and it is the responsibility of the class to save and restore the contents of its instances. The writeExternal and readExternal methods of the Externalizable interface are implemented by a class to give the class complete control over the format and contents of the stream for an object and its supertypes. These methods must explicitly coordinate with the supertype to save its state. These methods supersede customized implementations of writeObject and readObject methods.
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
If possible I would like to know what class provides input and output streams? Is it Object or System class? I am asking this because every class inherits Object class. This is a bit confusing to me.
Input and Output operations are performed by the reader class and it's sub-classes. Object is a base class which provides some "usefull" methods such as equals() and toString().
i don't know what exactly is your confusion , but i think its java.io.OutputStream and java.io.InputStream both are interfaces , based on what Stream you use , there definition in Stream class changes. i suggest you read some basic books and gets your fundamental of OOP strong.
and make full use of
Reference doc
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 been trying for a while, but now I'm wondering if I can. Is it possible for a method in an interface to return a variable or an array without needing two interfaces or methods(if possible)?
Every method has to specify a return type and stick with it. There are no "union types" like "returns a String or an int[]".
If you really have a method that can return two different things, you can
make a wrapper type (StringOrIntArray) that can hold both
or
have the method return Object which can be pretty much anything, including arrays, and use instanceof at the caller to see what you got.
Both options are not particularly attractive. Maybe take a step back and rethink the bigger picture.
What is wrong with
interface Foo{
int foo1();
//or....
int[] foo2();
}
?