Skip to main content

Posts

Showing posts from April, 2023
What distinguishes a collection from a stream?  A collection and a stream are both ways to work with sequences of elements in Java, but they differ in several ways. A collection is an in-memory data structure that holds a finite number of elements. Collections can be indexed and accessed in a random order. They typically provide methods for adding, removing, and querying elements. A stream, on the other hand, is a sequence of elements that can be processed in a functional way, without the need to store them in memory. Streams are designed to support parallel processing of large data sets, and they can be created from various sources, such as collections, arrays, files, or other data sources. Here are some key differences between collections and streams: Storage: Collections store all their elements in memory, whereas streams do not necessarily store all their elements in memory. Streams can work with data from various sources, and they can process data on the fly, as it becomes ava...

Java Stream API

The Stream API provides a way to work with collections of data in a declarative manner, using functional programming constructs It was introduced in Java 8 as a way to make it easier to process data in parallel, and it has since become an essential tool for developers working with large data sets. Stream API gives the ability to work with collections in a lazy and parallel manner. This means that the operations are only executed when necessary, and that they can be split across multiple threads to take advantage of multi-core processors. This can lead to significant performance improvements when working with large data sets. The Java Stream API has several key features that make it a powerful tool for working with collections of data. Here are some of the most important features of Java Streams: 1. Java Streams are lazy in processing , meaning that intermediate operations are only performed when a terminal operation is invoked. This allows for more efficient processing of large data se...

Java 8 - Lambda Expression

  Lambda expressions are known to be the biggest feature introduced with Java 8. This was the Java’s first step to Functional Programming. Why Lambda Expressions : Lambda Expression facilitates functional programming and simplifies the development a lot.  It provides a clear and concise way to represent one method interface using an expression. It is very useful in the collection library. It helps to iterate, filter, and extract data from the collection. The Lambda expression is used to provide the implementation of an interface that has a functional interface. It saves a lot of code. In the case of the lambda expression, we don't need to define the method again for providing the implementation. Here, we just write the implementation code. Java lambda expression is treated as a function, so the compiler does not create a .class file. They are used with functional interfaces: Lambda expressions are used with functional interfaces, which are interfaces with a single abstract met...

Problems

1. An electric car is a car that does not run on gas or other fossil fuel. It is powered by a battery that needs to be recharged regularly.  You want to travel from city A to city B in an electric car. There are charging stations in all cities and you are given the following data. 1. Map of the country in the form of cities and roads connecting them. 2. length of the roads connecting the cities. 3. All roads support two way traffic Assuming that K units of battery charge lets you travel across a distance of K units. What is the minimum battery capacity that you need to reach city B, without getting stranded between any two cities while travelling. Note  1. There will be N cities in the country and City A is the 1st City and City B is the Nth city, these cities will be connected by M roads 2. It is guaranteed that city B can be reached from city A using some roads. Assumption  N = 3 M = 3 Roads = [{1, 2, 5}, {2, 3, 5}, {1, 3, 9}] The Map of the country is  If not poss...