Monday, April 3, 2023

Containers in Python

Container is any object that can hold other objects. There are 4 built in containers:

1. List

A list is defined as an ordered, mutable, and heterogeneous collection of objects.

list1=[1,'a',5.6,'Python']   # create

list1[1],  list1[-4:7]    # read

list1[1]=1.2    # reassigning

for i in lst:     #  loop using in

print(i)

for i in range(len(lst)):     # loop on index

  print(lst[i])

new_list=[expression for element in list]   # list comprehension

eg: string="Python"

list3=[i for i in string]         # create

print(list3)    #  gives ['P','y','t','h','o','n']


list1=[1,2,3,4,5,6]

list2=[ i if i%3==0 else -1 for i in list1 ]          # list comprehension with if else (if no else, if part is at the end)

print(list2)


list1.append(17)   # add at end

list1.extend([2,8.0,'l'])   # add >1

list1.insert(2,'a')    #  add at 2nd index



del list1[1:4] #  deleting 2nd to 4th elements

lst.pop()  # delete last element

lst.pop(1)  # delete 1st index element

lst.remove('x')  # delete element's first occurrence


len(lst), min(lst), sum(lst)

list('abc')   # type casting

any(lst),  all(lst)


lst.index('s')   # find     (in can also be used to find)

lst.count('x')     # find

lst.reverse()     # reverses, no sorting

lst.clear()


m_list=[[1,2,3,4],['a','b','c'],[4.5,6.7,1.3]]     #2d list

print(list1+list2)     #   Concatenation

print(list2*3)   # repeats thrice

print('b' not in list2)    #  boolean

lst.copy(),   list(lst)      # copy

lst = [ ]  # empty list

Sort

lst.sort()    #   sorts the list    , available only in list

sorted(lst)    # returns sorted list, original unaffected, method in other containers also

def check(item1, item2):    # comparator method

    return lst(item1) - lst(item2)

from functools import cmp_to_key

sorted(mylist, key=cmp_to_key(check))



2. Tuple

Tuples are ordered and heterogenous, but they are immutable.

tup = ("python", 1.1, 3)      # create

tup = tuple(lst) ,   tup = tuple(str),   tup = (3,)   # create

tup[0], tup[-4:-1]  # access

a, b, c = tup  #  unpacking

Addition to a tuple can be done in 2 ways - by concatenation, or by converting to list and append.

Tuple3 = Tuple1 + Tuple2    #   concatenation. 

No update or reassign can be done on tuple since immutable.

del tup  # delete

tup.index(), tup.count()    # methods to find

len(tup), any(tup), all(tup), max(tup), sorted(tup)   #   functions on tuple  # sorted returns list

Methods that cannot be used for tuples:   append(), insert(), remove(), pop(), clear(), sort(), reverse()

for i in tup:    # loop

tup1=(1,2,3), tup2=(1,2,3), tup1 is tup2    # returns false

tup = ( )   # empty tuple



3. Set

Sets are unique, unordered, heterogenous and mutable. But a set cannot have mutable elements like lists, sets, or dictionaries (Strings and tuples are immutable along with all primitives)

Set = {'A', 'B', 'C', 'D'}     #   create

Set = set(lst), Set = set(tup)    # create

for x in Set:      # loop

We cannot read from set using index.

Set.add('E')     # insert

Set.update(set1)    #   concatenate

Set.update(lst)

set2 = Set.union(set1)  # concatenate with assignment

Set.remove('D')     # delete

Set.discard('D')

Set.pop()    # cannot give index

Set.clear()

del Set


len(Set), all(Set), any(Set), sorted(Set)     # built-in functions

max(Set), sum(Set) for homogeneous 

set2 = Set.intersection(set1)    # common elements

Set.isdisjoint(set1)     # True if no common elements

set2 = Set.difference(set1)     # minus

set2 = Set.symmetric_difference(set1)   # exclude common

set1.issubset(Set) 

set1 = Set.copy()

Cannot use Set={ } for empty set since that becomes a dictionary.

set1={'abab'} gives {'abab'}

set2=set('abab') gives {‘b’, ‘a’}



4. Dictionary

They store values in key-value pairs.  They are ordered (from Python 3.7) with unique keys. They are mutable. But their keys should be immutable.

dic = {'a':1,'b':2,'c':3}   #   create

dic = dict(a = "John", b = 36, c = "Norway")    #      create with equal to and no quotes for keys

dict[lst]    

dic['a'], dic.get('a')      #    access

dic = { }   #  empty dictionary

dic.keys()  ,   dic.values()

dic['a'] = 5      #    add ,  reassign ,   update

dic.pop('a') ,  del dic,  dic.clear() , dic.popitem()    #   delete

for i in  dic:      # loop keys

for x, y in dic.items():

dic.copy(),  dict(dic)    #  copy

Nested dictionary has dictionary as value, accessed as  dict['a']['age']

len(dic), any(dic),  all(dic), sorted(dic)

dic.fromkeys({0,1,2,3,4})    # create None valued dictionary with keys

dic.has_key('a')

dic={x:x*2 for x in ['a','b','c','d','e']}   #    dictionary comprehension

dic = {1: 'Geeks', 2: 'For',

        3: {'A': 'Welcome', 'B': 'To', 'C': 'Geeks'}}     # nested dictionary


Strings in Python

String is an immutable data structure in Java. That means, once value has been assigned to a String, it can’t be changed and if changed, a new object is created. It is also a sequence of characters. Python does not have a character data type, a single character is simply a string with a length of 1. 

Create

String str = "Test"

String str = 'Test'

String str = '''Tes

t'''

Retrieve

String characters can be retrieved with the index.

s = str[3] # fourth character

s = str[-2] # second last character

s = str[1:3] # slicing, returns characters from 1 to 2

i = str.index('e') # returns index

str.index('e', 2, 7) # start and stop locations

str.rindex('e')  # last index


Add/Update/Delete

str = "Hello".join(str)  # using join

str = "Hello" + str[0] + 'eeeee' + str[1:]  #  using plus  # indexing and slicing

str += "hi" 

str = str.replace('l','m')

str = str[0:4]   # deletion

del str

str[0] = 'x' will give error


In all the three cases, the result has to be reassigned to the value, else value will return unchanged.


Formatting

str = r"The \n stands for next line"    #  raw string

str = "{} {} {}".format('I', 'like', 'mango')

str = "{0} {2} {1}".format('I', 'mango', 'like')

var="mango"

str = f"I like {var}"

num = 12.3456789

print('Two decimals: %3.2f' %num)

print('Four decimals: %3.4f' %num)

print(a,b)  # concat

str1 = ' '

str2 = 'geeks'

print(repr(str1 and str2))  # returns ' '   # repr is printable representation to print quotes

print('a' and 'b')  # returns b or second element for and

print('a' or 'b')  # returns a or first element for or

'a' * 3 # returns aaa


Operations

Other operations we can do with a String are,

len(str),  str.upper(), str.strip() 

str.split(',')   # returns list

't' in str, str.find("t"),   str.index("t")

str[2:10:2]   # start,stop,step

str[::-1]   # to reverse

print(type(str)) is String

for x in "banana":

  print(x)


str(obj)  # type casting

ord(str) to get ascii

chr(str) to get back string from ascii

str[len(str)-1]

str.isalnum()   # alphanumeric


Comment inside a method given at the top with triple quotes is called DocStrings.


Memory

str="check" and str1="check" are saved in the same memory location.

str==str1 checks for value and returns true.

str.__eq__(str1) also checks for value and returns true.

__eq__() method can be overridden in a class.

str is str1 checks for memory location and returns true


(In Java, == checks for location and .equals() of String is overridden to check for value. Location is same within Stringpool.)


Now, str="check@" and str1="check@" are saved in different memory locations due to special character.

So str==str1 is true, str.__eq__(str1) is true, but str is str1 is false.

String objects are saved in the heap just like any other object. But String interning is a process that reuses basic alphanumeric Strings. This saves a lot of memory.

OOP concepts in Python

Python is an Object Oriented Programming Language. In Python, everything is object, but everything is not in classes. The Basic OOP concepts are,

Abstraction

Python has to import Abstract Base Class to add the feature of abstraction.

from abc import ABC,abstractmethod

class Polygon(ABC):

   @abstractmethod    

   def sides(self):   

      pass  

  

class Triangle(Polygon):  # Polygon is parent

   def sides(self):   # all methods should pass self

      print("Triangle has 3 sides")   

class Square(Polygon):   

   def sides(self):   

      print("Square has 4 sides")       


Triangle().sides()   # Class() creates object

Square().sides()   # function call is outside any class


Polymorphism

Two ways of achieving this are,

1. Overloading

def add(p, q, r = 0):  # default value given to make parameter optional
    return p + q + r  
print (add(6, 23))  
print (add(22, 31, 544))  

2. Overriding

class Bird:  
    def intro(self):  
        print("There are multiple types of birds in the world.")  
    def flight(self):  
        print("Many of these birds can fly but some cannot.")  
  
class Sparrow(Birds):  
    def flight(self):  
        print("Sparrows are the birds which can fly.")  
      
class Ostrich(Birds):  
    def flight(self):  
        print("Ostriches are the birds which cannot fly.")  
      
Bird().intro()
Sparrow().intro()  # same as bird
Ostrich().flight()   # overridden method


Inheritance

A child class extends the parent and overrides its necessary methods. 

class Vehicle:
    def drive(self):
print("Parent")

class Car(Vehicle):   # single inheritance
    def __init__(self):  # constructor
       Vehicle.__init__(self)  # calling parent constructor
    def drive(self):
print("Child")

class Nano(Car,Bus):  # multiple inheritance
    def drive(self):
print("Grand Child")

print(issubclass(Car,Vehicle))  # returns True
n = Nano()  
print(isinstance(n,Nano))   # returns True

Every class inherits from the base class called Object.



Encapsulation

In Python, instance variables are declared inside the constructor. They can be of 3 types 

class Employee:
    def __init__(self, name, project, salary):
        self.name = name   # public- accessible everywhere
        self._project = project   # protected with single underscore- accessible within class and subclasses
        self.__salary = salary   # private with double underscore- accessible only within class

Private members can be accessed using public getter methods or by name mangling. Getters and Setters are declared only for private variables, others can be accessed with dot.

def get_salary(self):
    print('Salary:', self.__salary)
print('Salary:', new Employee()._Employee__salary) # one underscore before and two after
print('Project:', new Employee()._project)

Saturday, March 25, 2023

Small Programs using Java

1. How to find the minimum value in a list?

Primitive

List<Integer> lst = Arrays.asList(1, 2, 3, 4, 56, 7, 89, 10);

System.out.println(Collections.min(lst));

Object:

List<Employee> lst = new ArrayList<Employee>();

lst.add(new Employee(10, "Raghu", 25000));

lst.add(new Employee(120, "Krish", 45000));

lst.add(new Employee(210, "John", 14000));

Employee minSal = Collections.min(lst, new EmpComp());

System.out.println(minSal);

 

class EmpComp implements Comparator<Employee>{ 

    public int compare(Employee e1, Employee e2) {

        return e1.getSalary().compareTo(e2.getSalary());

    }

}

Stream

lst.stream.min((e1, e2) -> e1.getSalary().compareTo(e2.getSalary())).get();


2. How to get a list sorted?

Primitive

Collections.sort(lst));

Object:

Collections.sort(lst, new EmpComp());

Stream:

lst.stream.sorted((e1, e2) -> e1.getSalary().compareTo(e2.getSalary())).collect(Collectors.toList()); 

Reverse Order:

Collections.sort(lst, Collections.reverseOrder());  

lst.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());


3. How to reverse a String?

Char Array:

Scanner scanner = new Scanner(System.in);

String str = scanner.nextLine();

char[] arr = str.toCharArray();

for (int i = arr.length - 1; i >= 0; i--)

System.out.print(arr[i]);

StringBuilder:

StringBuilder sb = new StringBuilder(str);

sb.reverse().toString();

Arraylist:

List<Character> lst = new ArrayList<>();

for (char c : str)

    lst.add(c);

Collections.reverse(lst);

Stream:

Stream.of(str)

      .map(word->new StringBuilder(word).reverse().toString())

      .collect(Collectors.joining(" ")).get(0);

Stream with index:

char[] arr = str.toCharArray();

IntStream.range(0, arr.length)

        .mapToObj(i -> arr[(arr.length - 1) - i])

        .forEach(System.out::print);

A String is a palindrome if the String is same as the Reversed String.


4. How to search an element?

In String:

if(str.indexOf("word") > -1)

In Arraylist of Primitives:

boolean found = lst.contains(2);

In Arraylist of Objects:

for (Employee emp : emplist) {

    if (emp.getName().equals(name)) {

        return emp;

    }

}

Using Stream:

Employee empl = emplist.stream()

  .filter(emp -> name.equals(emp.getName()))

  .findAny()

  .orElse(null);



5. How to count the occurrences? 

String:

Linear search:

for (int i=0; i<str.length(); i++){

    if (s.charAt(i) == c)

        count++;

    }

return count;

Binary Search:

while (low <= high) {

        mid = (low + high) / 2;

        if (a[mid].compareTo(x) < 0) {

            low = mid + 1;

        } else if (a[mid].compareTo(x) > 0) {

            high = mid - 1;

        } else {

            return mid;

        }

    }

Arraylist:

Collections.frequency(list, new Employee(321222, "John", 29)); 

public boolean equals(Employee e) {

    if(this.id.equals(e.getId()) && this.name.equals(e.getName())) {

        return true;

    }

    return false;

}

Stream:

lst.stream().filter(str -> str.indexOf("ch") > -1).count();

To get count of each item,

lst.stream().collect(Collectors.groupingBy(Function.identity(),Collectors.counting()));



6. How to replace an element?

String:

str.replace('h', 'm');

Arraylist

Collections.replaceAll(lst, null, " ");

Stream:

List<String> newList = lst.stream()

                .map(s -> s != null ? s : "")

                .collect(Collectors.toList());


Friday, March 24, 2023

Design Patterns in Java

 Design Patterns provide an industry-standard approach to solving a recurring problem.


1. Creational Design Patterns

They define the way an object is created.

Factory Method: This ensures that the correct object is created according to a parameter received.

 Implementation: A Factory class is created which returns different Child objects based on an input String passed to its method/constructor and using a switch case. 

Eg:  A car company gets the request to create a car. This request is given to the factory which decides how the car should be created based on its model.

Abstract Factory method: This ensures that the correct object is created according to a parameter when there are multiple levels of subclasses.

Implementation: When there are two levels of overriding, an abstract factory class or an interface is used to identify the first level of child class and for identifying the second level, there are separate concrete factory classes. 

Eg: When a car factory handles different companies, it should first decide for which company and then decide which model to be manufactured.

Singleton: This ensures that only one object for a class is created. 

Implementation: A private constructor is created and a public synchronized static getInstance() method to call it. 

Eg: In an apartment, the residents decide that only one borewell should be dug for the entire community. 

Prototype: This ensures that a copy of the existing object is returned when a new object is requested. This is used when an object creation is costly due to some reason (like db calls).

Implementation: A Prototype class is created which implements Cloneable and defines clone method (shallow or deep). This also has a list of predefined objects. So getInstance() method simply clones the predefined objects when called.

Eg: A Xerox machine just takes multiple copies of the same content.

Builder: This ensures that an object is created step by step.

Implementation: Instead of the actual class using its setter methods, a builder class is populated with its setters step by step. Finally this builder object is passed as a parameter to the actual class' constructor to create the actual object. 

Eg: An excel/pdf report is generated after formatting it line by line.


2. Structural Design Patterns

They define the way different types of objects can have a connection.

Adapter: This ensures that a class gets some of the features from another class.

Implementation: If a class cannot use features from another class directly due to its hierarchy, another adapter class is created as a child of both the classes (implements interface of our class, extends other class) and this class' object is created for processing.

Eg: A phone charger has an adapter that converts AC to DC since phone cannot use AC directly from the socket.

Bridge: This ensures that two classes are loosely coupled.

Implementation: Since inheritance creates tight coupling, composition (has-a relationship) is used. A class has another class as its instance variable and methods to operate on that class. This helps in doing operations on both classes without affecting their hierarchies.

Eg: A switch can be used in a house to operate a fan without knowing the technical details of the fan. So switch acts as the bridge here.

Composite: This ensures that a hierarchy is defined in a tree model.

Implementation: An interface can have its implementations which in turn can have multiple child classes.

Eg: Employee hierarchy in a company. Each manager can have employees under him and managers are in turn coming under General Managers or Vice Presidents.

Decorator:  This ensures that the behaviour of all sub classes can get altered without actually modifying them.

Implementation: An interface has some subclasses. Another abstract class implements this interface and adds the extra behaviour. The abstract class has a constructor parameterized with an interface object. This way the extra behaviour can be added to the interface.

Eg: Pizza and its sizes come under one hierarchy. Decorations on the Pizza can be decided from a separate Decorator class.

Facade: This ensures that multiple classes can be accessed from only one central class without knowing their internal details.

Implementation: One Facade class has instance variables of multiple classes and a constructor in which other classes are instantiated.

Example: In a restaurant only the menu card is displayed to the customers hiding the background details.

Flyweight: This ensures that a lot of objects can be created without much memory requirement.

Implementation: This combines factory and prototype design patterns. A factory has a hashmap to save objects. When a new object request comes with its type passed as String, if the key (ie, type) is already present in the hashmap, it returns that object, else it creates new object using a switch case. Flyweight resources are immutable.

Example: String pool which reuses String literals.

Proxy: This ensures that an actual class is accessed only through its proxy.

Implementation: A Proxy class is created whose constructor actually creates objects for the Real class.

Example: UPI is a proxy for a Bank account.


2. Behavioural Design Patterns

They define the way objects talk to each other.

Chain of responsibility: This ensures that different classes are called one after the other. 

Implementation: Either we can call the classes in the order, or we can create objects inside constructor in the order.

Example: Supply chain of products

Iterator: This ensures that objects can be iterated through.

Implementation: An Iterator Class implements Iterator and overrides its next()/hasNext() methods. The actual class has iterator() method to create the object of the Iterator class, using which it can traverse through the lists in the Actual class.

Example: A video playlist

Mediator: This ensures that objects talk to each other only through the Mediator.

Implementation: A Mediator class has instance variables of other classes. The Mediator object is used to work on the other classes

Example: Travel Agencies work as Mediator for different air services.

Observer: This ensures that all Observers are updated when there is a change.

Implementation: A common class has instance variables of all Observer classes. It also has methods to update the Observers whenever its onchange() method is called.

Example: All travelers should be informed when there is a change in schedule.

Strategy: This ensures that different child objects can have different behaviours.

Implementation: An interface can add a behaviour to a particular class. But if it needs its implementation also, then that interface's concrete child is added as instance variable to the required child classes.

Example: An IT team may be looking for the skills Java, .Net etc. from the resources while all resources are part of the Employee hierarchy.




Wednesday, March 22, 2023

Functional Interface in Java

 Any interface with a SAM(Single Abstract Method) is a functional interface. It can have other static or default methods. 

public interface Testable {

    void printName(String name);

}

There are five ways of implementing this Single Abstract Method:

1. Using normal function inside the implementation class

class Child implements Testable{

    void printName(String name) {

        System.out.println("Hi " + name);

    }

}   

new Child().printName("Tester");

2. Using anonymous class to override

Anonymous class can extend or implement only one class/interface.

class Test {

    Testable obj = new Testable() {           

        void printName(String name) {

            System.out.println("Hi " + name);

        }

    };

    obj.printName("Tester");  // Parent object

 

3. Using lambda operator

class Test {

    Testable obj = (name) -> {System.out.println("Hi " + name)

// method name skipped since only one method

    obj.printName("Tester"); 

}   

4. Using method reference

Method reference can be used if lambda just calls one method.

class Test {

    Testable obj = System.out::println // Cannot print Hi // Skipping arguments

    obj.printName("Tester"); 

}

Method reference is of 3 types:

For calling Child's static method,

Testable obj = Child::print;      obj.printName("Tester"); 

For calling Child's non-static method,

Testable obj = new Child()::print;      obj.printName("Tester"); 

For calling Child's constructor,

Testable obj = Child::new;      obj.printName("Tester"); 


5. Using Built-in Functional Interfaces

Built-in Function interfaces are needed if the argument passed and the value returned are different, or if more than one argument needs to be passed.

Function

Function<Student,String> f = s->"Name:"+s.name +" and Age:"+s.age;

String str = f.apply(student) //Student object passed, String returned

BiFunction

BiFunction<Integer, Integer, Integer> bf = Arithmetic::add;  

int result = bf.apply(10, 20);  // Two integers passed, one Integer returned

Consumer

Consumer<String> c = System.out::println;

c.apply("Test"); // String passed, nothing returned.

There are also other variants of the Consumer — DoubleConsumer, IntConsumer, and LongConsumer. 

BiConsumer

Consumer<String,Integer> c = (name,age)->System.out.println("Name is "+name+" Age is "+age);

c.apply("Joe",12); // String and Integer passed, nothing returned.

Predicate

Predicate<String> p = (value) -> value != null;

p.apply("Test"); // String passed, boolean returned.

There are also other variants of the Predicate — DoublePredicate, IntPredicate, and LongPredicate. 

BiPredicate

BiPredicate<Integer,Integer> bp = (age,min)->age>=min;

bp.apply(22,18); // Two Integers passed, boolean returned.

Supplier

Supplier<String> s = ()->System.out.println("Hi");

s.apply("Test"); // Nothing passed, String returned.

There are also other variants of the Supplier — DoubleSupplier, IntSupplier, and LongSupplier. 


Other Built-in Java Functional Interfaces

Runnable –> This interface only contains the run() method.

Comparable –> This interface only contains the compareTo() method which can be overridden as 

Comparator<Book> byAuthor = (b1, b2) -> b1.getAuthor().compareTo(b2.getAuthor());

ActionListener –> This interface only contains the actionPerformed() method.

Callable -> This interface only contains the call() method.

Tuesday, March 21, 2023

Streams in Java

Streams are wrappers around a data source (Collection, array etc.) allowing us to operate on that data source. A stream does not store data and is not a data structure. It also never modifies the underlying data source.

Any operation involving Streams should have three sections - a source, one or more intermediate operations and a terminal operation

 

Source

1. An empty Stream is created as, 

Stream<String> stream = Stream.empty();

2. Any Collection object can be converted to stream using stream() method. 

List<String> lst = Arrays.asList("A", "B", "C", "D");

Stream<String> stream = lst.stream();

Set<String> set = new HashSet<>(list);

Stream<String> strem = set.stream();

3. A Stream can be created from an array in two ways as,

Stream<String> strm = Stream.of("a", "b", "c");

String[] arr = new String[] { "a", "b", "c" };

Stream<String> stream = Arrays.stream(arr);

4. By adding elements individually using the mutable Builder.

Stream.Builder<String> builder = Stream.builder();

Stream<String> stream = builder.add("a").add("b").add("c").build();

5. Create an infinite Stream using Stream.generate() method.

Stream<Double> stream = Stream.generate(Math::random).limit(4);

6. Create an infinite Stream using Stream.iterate()

Stream<Integer> IntegerStream = Stream.iterate(0, n -> n + 1).limit(10);

7. A File can be converted to stream as,

Stream<String> stream = Files.lines(filepath);

8. Using Primitive Streams

IntStream intStream = IntStream.range(1, 3); //1 2

LongStream longStream = LongStream.rangeClosed(1, 3); //1 2 3

9. Stream from a pattern match

Stream<String> stream = Pattern.compile("\\s").splitAsStream("A String");

 

Intermediate Operations

The Streams are further processed by zero or more intermediate methods combined together (pipelined). But actual processing doesn’t start till a terminal operation is invoked. This is called lazy evaluation.

1. filter() to exclude unwanted streams with a condition (known as Predicate)

stream.filter((Employee e)-> e.getAge() > 24)

2. sorted() to arrange in order. A comparator can be passed as parameter.

stream.sorted()

3. distinct() to get non-duplicate elements

stream.distinct()

4. map() to transform the stream using a Function.

stream.map(String::toUpperCase)

5. limit() to restrict the result count

stream.limit(5)

6. peek() to debug every intermediate operation

stream.filter(e -> e.length() > 3).peek(e -> System.out.println("Filtered value: " + e))

 

Terminal Operations

They give the final result. They don't return a stream.

toArray() : to get an array from the stream.

collect() : to convert stream into a Collection. eg: stream.collect(Collectors.toList())

count() : to get the count

reduce() : to get a single result from a sequence. eg: stream().reduce("", String::concat)

forEach() : to do something for each element. eg: stream.forEach(System.out::println)

min() : to get the minimum value based on comparator. Returns an Optional element. 

        eg: Optional<Address> mini = stream.min((i, j) -> i.compareTo(j))

              Address addy = mini.get();

              mini.ispresent(); // False if Optional is empty

max() : to get the maximum value based on comparator. Returns an Optional element.

anyMatch() : True if any element matches, else false. eg: stream.anyMatch(p -> p.getAge() < 20)

allMatch() : True if all elements matche, else false. eg: stream.allMatch(p -> p.getAge() < 20)

noneMatch() : True if no element matches, else false. eg: stream.noneMatch(p -> p.getAge() < 20)

findAny() : Returns Optional with any element.

findFirst() : Returns Optional with the first in order element.

 

Parallel Stream

Parallel Streams are executed parallelly. 

eg: List<Integer> lst = Arrays.asList(1, 2, 3, 4);

int sum = lst.parallelStream().reduce(5, Integer::sum);

Here the result is calculated as (5+1) + (5+2) + (5+3) + (5+4) where as in sequential it would have been 1+2+3+4+15. 

Another way of creating prarallel stream is, stream.parallel().forEach(System.out::println);