https://github.com/hariomgola/java
Competitive Coding in java
https://github.com/hariomgola/java
algorithms java
Last synced: 8 months ago
JSON representation
Competitive Coding in java
- Host: GitHub
- URL: https://github.com/hariomgola/java
- Owner: hariomgola
- License: mit
- Created: 2020-09-25T17:53:45.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2025-03-21T07:57:05.000Z (9 months ago)
- Last Synced: 2025-04-24T03:48:23.548Z (8 months ago)
- Topics: algorithms, java
- Language: Java
- Homepage:
- Size: 421 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Java
This File contains few example of competitive coding in Java and made for Education purpose only.
Any further Use of these program is Prohibited.
# Made with :heart:
Portfolio :computer: https://hariomgola.github.io/
# Java List and Hash
List
# Array List
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ArrayListFunctionality {
public static void main(String[] args) {
List cars = new ArrayList();
cars.add("Suzuki");
cars.add("Tata");
cars.add("Mahindra");
for (int i = 0; i < cars.size(); i++) {
System.out.println(i + " car - " + cars.get(i));
}
Collections.sort(cars);
for (String i : cars) {
System.out.println("car - " + i);
}
}
}
# Linked List
- addFirst()
- addLast()
- removeFirst()
- removeLast()
- getFirst()
- getLast()
Hash
# Hash Map
import java.util.HashMap;
public class HashMapFunctionality {
public static void main(String[] args) {
HashMap users = new HashMap();
users.put("one", "Hari");
users.put("two", "Raj");
users.put("three", "Manoj");
System.out.println(" |> " + users);
// accessing
users.get("two");
// Removing
users.remove("three");
// Size
users.size();
// Print
for (String i : users.keySet()) {
System.out.println("Keys " + i);
}
for (String i : users.values()) {
System.out.println("Values " + i);
}
}
}
# Hash Set
import java.util.HashSet;
public class HashSetFunctionality {
public static void main(String[] args) {
HashSet cars = new HashSet();
cars.add("Suzuki");
cars.add("Tata");
cars.add("Mahindra");
cars.add("BMW");
System.out.println("|> " + cars);
// functionality
cars.contains("BMW");
cars.remove("Tata");
cars.size();
for (String i : cars) {
System.out.println(i);
}
cars.clear();
}
}
Thread
class ThreadHelper extends Thread {
public void run() {
System.out.println("This Code is Running in a Thread With Class ThreadHelper");
}
}
class ThreadHelper2 implements Runnable {
public void run() {
System.out.println("This Code is Running in a Thread With Class ThreadHelper2");
}
}
public class ThreadFunctionality {
public static void main(String[] args) {
ThreadHelper thread = new ThreadHelper();
thread.start();
System.out.println("This Code is outside of a Thread");
}
}
public class ThreadFunctionality {
public static void main(String[] args) {
ThreadHelper2 threadHelper = new ThreadHelper2();
Thread thread = new Thread(threadHelper);
thread.start();
System.out.println("This Code is outside of a Thread");
}
}
# Java 8 Features
Functional Interface
# Functional Interface
- Functional interface is an interface that has excatly one abstract menthod.
- Since functional interface only have single functional it can easily implement using lambda.
- @FunctionalInterface - Its need to be shown at the interface class level.
- Above mentioned anotation is optional can be used or cannot be.
- Some Build in functional interface in java belongs to [java.util.function]
- Predicate, Function, Supplier, Consumer
Lambda Expression
# Lambda Expression
* parameter -> expression
* (parameter1, parameter2) -> { code block }
* Labda express also store in the variable name [Consumer] and you can use the reference
Collection Sort Logic for Object based
Collections.sort(user, new Comparator() {
@Override
public int compare(User a, User b) {
if (a.getSalary() > b.getSalary()) {
return -1; // Here -1 for placing the object before
} else if (a.getSalary() < b.getSalary()) {
return 1; // Here +1 for placing the object after
}
return 0; // Here 0 no change
}
});
Stream API
# Stream Api
* syntax for stream - Stream stream;
* Stream is not a data structure instead it takes input from the collections.
* Stream don't change the actual daya structure, They only provide the result for pipeline menthod.
* Each intermediate operation is lazily executed and return a stream as a result.
* Hence various intermediate opertaions can be pipelined.
* Terminal opertaions mark the end of the stream and return the result.
# Types of Streams Opertaion
* Intermediate Operations
- map()
- filter()
- sorted()
- flatMap()
- distinct()
- peek()
* Terminate Operations
- collect()
- forEach()
- reduce()
- count()
- findFirst()
- allMatch()
- anyMatch()
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Stream;
public class StreamAPI {
private static List user = new ArrayList();
// Once this Runs data will be available
static {
user.add(new User(1, "H", 5.25));
user.add(new User(2, "Ha", 14.50));
user.add(new User(3, "Har", 22.02));
user.add(new User(4, "Hari", 16.50));
user.add(new User(5, "Hario", 24.00));
}
public static void main(String[] args) {
Stream userStream;
user.stream().map(_user -> {
_user.setId(_user.getId() * 2);
return _user;
}).forEach(_user -> {
System.out.println(_user.getId());
});
}
}