Posts

Showing posts with the label java-8

Can lambda expressions be the alternative of polymorphism?

Image
Clash Royale CLAN TAG #URR8PPP Can lambda expressions be the alternative of polymorphism? I am learning lambda expressions and functional interfaces. We can directly write an implementation of the interface by the lambda expression. So I think, it could be the alternative for polymorphism. I have some code using polymorphism, interface Drawable { public void draw(); } class Shape { protected String name; public Shape(String name) { this.name = name; } } class Rectangle extends Shape implements Drawable { public Rectangle(String name) { super(name); } @Override public void draw() { System.out.println("I am "+this.name); System.out.println("Drawing rectangle with 2 equal sides."); } } class Square extends Shape implements Drawable { public Square(String name) { super(name); } @Override public void draw() { System.out.println("I am "+this.name); System...

Reactor core 3 , webflux & websocket, send to specific session id

Image
Clash Royale CLAN TAG #URR8PPP Reactor core 3 , webflux & websocket, send to specific session id Every session data passed into the socket is broadcasted to all users since every session subscribes to the UnicastProcessor eventPublisher. How can I send by event data to a single session id and not to all of them? @Override public Mono<Void> handle(WebSocketSession session) { WebSocketMessageSubscriber subscriber = new WebSocketMessageSubscriber(eventPublisher); session.receive() .map(WebSocketMessage::getPayloadAsText) .map(this::toEvent) .subscribe(subscriber::onNext, subscriber::onError, subscriber::onComplete); return session.send(outputEvents.map(session::textMessage)); } My use-case requires me to include both options for broadcasting any changed state with any client to all sockets connected plus the abillity to send response to a specific client (sessionId) that send a request within a specific event Github link or should...

how sort when hashmap value is list of objects by multiple properties java 8

Image
Clash Royale CLAN TAG #URR8PPP how sort when hashmap value is list of objects by multiple properties java 8 Suppose I have like : Map<String, List<MyState>> map = new HashMap<>(); map.computeIfAbsent(key, file -> new ArrayList<>()).add(myState); map.put("aa",list1..) map.put("bb",list2..) map.put("cc",list3..) public class MyState { private String state; private String date; } I want to sort the map values List<MyState> by MyState::date and then by MyState::state List<MyState> MyState::date MyState::state Sorting map entries : HashMap is not ordered. Also, lists will have multiple entries. Which of the list elements do you use to sort values of the map itself? – ernest_k 33 mins ago map entries HashM...

Java 8 Streams - advanced usage of filter and map with overlapping period

Image
Clash Royale CLAN TAG #URR8PPP Java 8 Streams - advanced usage of filter and map with overlapping period I'm struggling to try to use Java 8 stream in this scenario. I have a list of plans being that each plan has its own version number and period. Given that a list of plans I have to consider only the latest version of each week/year. For instance: public class Period { int year; int week; } public class Plan { Period start; Period end; int version; } public class WeeklyPlan { int week; int year; Plan plan;//latest version } List of plans... The outcome should be a list of WeeklyPlan with: What is the best way to implement that using Java 8 stream? Whoever up-voted this, please don't -- the OP hasn't shown any attempt yet at all, or told us yet what problems he's had with his attempts – Hovercraft Full Of Eels 1 min ago ...

Escaping New Line character within SQL query

Image
Clash Royale CLAN TAG #URR8PPP Escaping New Line character within SQL query I am reading a table using my java code and creating a csv file out of it. So the 4 rows that I have get converted similar to below - sam , 18 , banker , He likes to play football jam , 28 , hacker , he likes nothing However in certain cases when the last varchar2 column contains n it becomes like this sam , 18 , banker , He likes to play football jam , 28 , hacker , he likes nothing When I try to read the file , each line is read one at a time and I'm not able parse the data due to few words being pushed to subsequent lines. Is there a way to escape the new line character within the column in my query to make it into a single line? My sql query select name , age , job , hobbies from person_details I am using csvwriter to generate the csv file - CSVWriter csvWriter = new CSVWriter(new FileWriter(results), DELIMITER, Character.M...

Return null or new object using Optional object fields

Image
Clash Royale CLAN TAG #URR8PPP Return null or new object using Optional object fields We have a method, wherein we receive an Optional<SomeType> object. If the contained SomeType object is not null then we have to initialize a SomeOtherType object using the fields of the SomeType object and return that new object; otherwise we have to return null Optional<SomeType> SomeType SomeOtherType SomeType We found multiple different solutions where we perform this task with two statements, first retrieving the optional object and then second creating the other type object e.g. private SomeOtherType ourMethod() { SomeType someObject = getOptionalSomeTypeFromSomeWhere().orElse(null); return someObject != null ? new SomeOtherType(someObject.getField1(), someObject.getField2(), ...) : null; } Is it possible to cover this with one statement? So far we could not figure to do the null checks, accessing the fields, new object creation etc. all in one Basically a more complex case of...

How return null by using Stream API?

Image
Clash Royale CLAN TAG #URR8PPP How return null by using Stream API? So, I have an ArrayList of autogenerated strings. I want to find first element that contains some character or else, if there is no one element that mach this filter, to apply other filter. In other way I want to return null object. ArrayList null So I write this lambda expression: str.stream() .filter(s -> s.contains("q")) .findFirst() .orElseGet(() -> str.stream() .filter(s -> s.contains("w")) .findFirst() .orElseGet(null)) But if there is no one element that mach this two filters I will have NullPointerException. Haw can I get somethink like: return null ? return null Don't. Instead, return an Optional and let the client decide what to do with it. – Luiggi Mendoza 17 secs ago Optional ...

speeding up select query using parallel processing

Image
Clash Royale CLAN TAG #URR8PPP speeding up select query using parallel processing I am running a simple select query to fetch all the values from the table and writing the resultset into CSV file using Java code. My select query is similar to below select * from <tablename> This table contains data for the past 3 years. One of the ways that I was thinking of speeding up the process is to run a separate thread thereby spinning up 36 threads , each querying 1 month worth of data ( after making sure db can handle 36 connections) and aggregating the result into a single file. Is there any library within java or oracle function that helps in achieving the same by querying the data in parallel and aggregating the result set. By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to...