import java.util.List;
import java.util.Random;
import java.util.function.Function;
class RandomFunctor {
private final List<Function<Integer, String>> functionList = List.of(
integer -> {
return "this is from first " + integer; //TODO :: implement me!
},
integer -> {
return "this is from second " + integer; //TODO :: implement me!
},
integer -> {
return "this is from third " + integer; //TODO :: implement me!
});
public String getRandomString(Integer input) {
var rand = new Random();
var function = functionList.get(rand.nextInt(functionMap.size()));
return function.apply(input);
}
}
public static void main(String args[]) {
LinkedList<Integer> linkedList = new LinkedList();
linkedList.add(1);
linkedList.add(2);
linkedList.add(3);
linkedList.add(4);
linkedList.add(5);
linkedList.add(6);
//finding middle element of LinkedList in single pass
Iterator<Integer> current = linkedList.iterator();
int length = 0;
Iterator<Integer> middle = linkedList.iterator();
Integer result = 0;
while (current.hasNext()) {
length = current.next();
if (length % 2 == 0) {
result = middle.next();
}
}
if (length % 2 == 1) {
result = middle.next();
}
System.out.println("length of LinkedList: " + length);
System.out.println("middle element of LinkedList : " + result);
}
import java.util.Arrays;
import java.util.Scanner;
public class Main {
enum TestEnum {
One(1), Two(2), Three(3);
private int code;
TestEnum(int code) {
this.code = code;
}
}
public static void main(String[] args) {
int n = new Scanner(System.in).nextInt();
boolean isPresent = Arrays.stream(TestEnum.values()).anyMatch(element -> element.code == n);
System.out.println("n in TestEnum? " + isPresent);
}
}