Rambrij Chauhan
BAN USERpublic LinkedList<T> reverse(T k) {
LinkedList<T> result = new LinkedList<>();
Node<T> current = head;
current = reverse(result, current, k);
reverse(result, current);
return result;
}
private Node<T> reverse(LinkedList<T> result, Node<T> current, T k) {
if (current != null && k != null) {
if (current.value.equals(k)) {
k = null;
}
Node<T> temp = reverse(result, current.next, k);
if (current != null) {
result.add(current.value);
}
return temp;
}
return current;
}
public V getMiddleElement(){
Node<V> fast= firstNode;
Node<V> slow = firstNode;
while(fast!=null && fast.next != null){
fast= fast.next.next;
slow = slow.next;
}
return slow.value;
}
Here is code:-
public Node<V> merge(Node<V> list1, Node<V> list2) {
Node<V> temp = list1;
int index = 0;
while (list1 != null && list2 != null) {
if (compare(list1, list2) >= 0) {
Node<V> node = new Node<>();
node.setValue(list2.value);
if (index == 0) {
node.next = list1;
list1= node;
temp = list1;
list1 = list1.next;
} else {
temp.next = node;
node.next = list1;
list1 = node;
index = 0;
}
list2 = list2.next;
} else{
list1 = list1.next;
}
if (index > 0) {
temp = temp.next;
}
index++;
}
if(list2 != null){
temp.next = list2;
}
return list1;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
private int compare(Node<V> node1, Node<V> node2) {
Comparable<V> comparable = (Comparable) node1.value;
return comparable.compareTo(node2.value);
}
private static class Node<V> {
private V value;
private Node<V> next;
public V getValue() {
return value;
}
public void setValue(V value) {
this.value = value;
}
}
Here is the solution:-
public class ProducerConsumerProblem {
private BlockingQueue<String> items = new LinkedBlockingQueue<String>(5);
private Scanner scanner = new Scanner(System.in);
public ProducerConsumerProblem() {
consume();
produce();
}
public void produce(){
Runnable runnable = new Runnable() {
@Override
public void run() {
while(true){
System.out.print("Name the new Item - ");
try {
items.put(scanner.next());
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
Thread t = new Thread(runnable);
t.start();
}
public void consume(){
Runnable runnable = new Runnable() {
@Override
public void run() {
while(true){
try {
System.out.print("Retrieving the item - "+items.take());
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
Thread t = new Thread(runnable);
t.start();
}
public static void main(String[] args) {
new ProducerConsumerProblem();
}
}
Using Semaphore class:-
public class PrintList {
private List<Integer> list1 = Arrays.asList(new Integer[]{1,2,3,4,5});
private List<Integer> list2 = Arrays.asList(new Integer[]{1,2,3,4,5});
private List<Integer> list3 = Arrays.asList(new Integer[]{1,2,3,4,5});
private int listSize = 5;
private Semaphore semaphore = null;
public PrintList() {
semaphore = new Semaphore(3);
}
public void print() throws InterruptedException{
semaphore.acquire();
for(int i = 0;i<listSize;i++){
System.out.println(list1.get(i));
System.out.println(list2.get(i));
System.out.println(list3.get(i));
}
semaphore.release();
}
public static void main(String[] args) {
final PrintList printList = new PrintList();
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
printList.print();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
Thread t1 = new Thread(runnable);
Thread t2 = new Thread(runnable);
Thread t3 = new Thread(runnable);
t1.start();t2.start();t3.start();
}
}
Here is the solution...
public class InsteadJoin {
public Thread createThread(String name){
Thread t = new Thread(name){
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
};
return t;
}
public void myJoin(Thread t){
while(t.isAlive()){
}
}
public static void main(String[] args) {
InsteadJoin ij = new InsteadJoin();
System.out.println("Start");
Thread t = ij.createThread("A");
Thread t1 = ij.createThread("B");
Thread t2 = ij.createThread("C");
t.start();
ij.myJoin(t);
t1.start();
ij.myJoin(t1);
t2.start();
ij.myJoin(t2);
System.out.println("Done");
}
}
I think, its working fine. I done it through Semaphore class, it can be also done with legacy thread mechanism using wait and notify.
import java.util.concurrent.Semaphore;
public class PrintEvenOddWithSemaphore {
public static void main(String[] args) {
Test test = new Test(new CalculateEvenOdd(10));
Thread t1 = new Thread(test);
Thread t2 = new Thread(test);
t1.start();
t2.start();
}
}
class Test implements Runnable{
CalculateEvenOdd evenOdd = null;
public Test(CalculateEvenOdd evenOdd) {
this.evenOdd = evenOdd;
}
@Override
public void run() {
try {
evenOdd.calEvenOdd();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class CalculateEvenOdd{
private Semaphore semaphore = null;
private int range = 10;
public CalculateEvenOdd(int range) {
semaphore = new Semaphore(2);
this.range = range;
}
public void calEvenOdd() throws InterruptedException{
semaphore.acquire();
for(int i=1;i<=range;i++){
if(i%2==0){
System.out.println(i+" is Even Number");
}
else{
System.out.println(i+" is Odd Number");
}
}
semaphore.release();
}
}
Here is the solution...
public static void main(String args[]){
String numStr = "123456";
// 1*100000 (10^5)= 100000
// 2*10000 (10^4) = 20000
// 3*1000 (10^3) = 3000
// 4*100 (10^2) = 400
// 5*10 (10^1) = 50
// 6*1 (10^0) = 6
// ------------------------
// 123456
// ------------------------
int length = numStr.length();
char[] charArr = numStr.toCharArray();
int total = 0;
int index=length-1;
for(int i=0;i<length;i++){
total = (int) (total + (charArr[index]-48)*Math.pow(10, i));
index--;
}
System.out.println(total);
}
public static void main(String[] args) {
String max = "";
int maxNum = 0;
String []str = {"rat", "xyzcatratabc", "cat", "xyz",
"abcxyz", "ratcatabc", "abc"};
HashMap<String, Integer> map = new HashMap<String, Integer>();
int length = str.length;
for(int i=length-1;i>=0;i--){
for(int j=0;j<length-1;j++){
if(i==j)
break;
if(str[i].contains(str[j])){
if(map.get(str[i])!=null){
int temp = map.get(str[i])+1;
if(temp>maxNum){
max = str[i];
maxNum = temp;
}
map.put(str[i], temp);
}
else{
map.put(str[i], 1);
}
}
else
if(str[j].contains(str[i])){
if(map.get(str[j])!=null){
map.put(str[j], map.get(str[j])+1);
}
else{
map.put(str[j], 1);
}
}
}
}
System.out.println(map);
System.out.println("'"+max+"' is the String which made with '"+maxNum+"' other strings.");
}
Here is the code......
import java.util.concurrent.TimeUnit;
public class MyJoinTest{
public static void main(String[] args) {
MyThread t1 = new MyThread(new Runnable() {
@Override
public void run() {
for(int i=0;i<5;i++){
System.out.println("-----"+i+"-----");
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
MyThread t2 = new MyThread(new Runnable() {
@Override
public void run() {
for(int i=5;i<10;i++){
System.out.println("-----"+i+"-----");
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
MyThread t3 = new MyThread(new Runnable() {
@Override
public void run() {
for(int i=10;i<15;i++){
System.out.println("-----"+i+"-----");
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
t1.start();
t1.myJoin();
t2.start();
t2.myJoin();
t3.start();
t3.myJoin();
System.out.println("In Main, After Join");
}
}
class MyThread{
private Runnable runnable = null;
private Thread thread = null;
public MyThread(Runnable runnable) {
this.runnable = runnable;
}
public void start(){
thread = new Thread(runnable);
thread.start();
}
public void myJoin(){
while(thread.isAlive()){
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
You should try serialization to send the recorded voice over network. please find below the steps:-
1. Create a simple pojo class and it should implement serializable interface.
2. Create setter and getter for a property that must be byte array type
for example :-
Class AudioSerial implements serialization{
private byte[] audio = null;
}
3. Store the audio into this property as byte array.
4. Serialize above pojo class over the network and deserialize in the same way on targeted path where you want to store and play this audio.
Hope it will work...
Improved Solution:-
- Rambrij Chauhan February 06, 2020