как
import java.util.List;
import java.util.Random;
import java.util.stream.Stream;
public class Test {
public static void main(String[] args) {
// Create List
List<MyObj> objects = Stream.generate(MyObj::new).limit(10).toList();
System.out.println(objects);
// Get minimal index item
MyObj minIndexObj = objects
.stream()
.reduce((a, b) -> a.getIndex() < b.getIndex() ? a : b).orElse(null);
System.out.println(minIndexObj);
}
}
class MyObj {
private final int index;
public MyObj() {
index = new Random().nextInt();
}
public int getIndex() {
return index;
}
@Override
public String toString() {
return "MyObj { index= " + index + " }";
}
}
/*
https://www.baeldung.com/spring-webflux-cors
https://www.baeldung.com/spring-cors
*/
package com.broadview.bvreportsgenerator.configuration;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.config.CorsRegistry;
import org.springframework.web.reactive.config.EnableWebFlux;
import org.springframework.web.reactive.config.WebFluxConfigurer;
@Configuration
@EnableWebFlux
public class CorsGlobalConfiguration implements WebFluxConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**");
}
}
final List<String[]> splitNames = new ArrayList<>();
for (int i = 0; i < setFullNameList.size(); i++) {
String[] splitFullName = {"", "", "none"};
String[] temp = setFullNameList.get(i).split(" ");
if (temp.length < 2) continue; // некорректный ввод, переходим к следующему
splitFullName[0] = temp[0];
splitFullName[1] = temp[1];
if (temp.length == 3) splitFullName[2] = temp[2];
splitNames.add(splitFullName); // сохраняем массив splitFullName
}
import lombok.Getter;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;
public class Test {
public static void main(String[] args) {
HandleB handleB = new HandleB();
handleB.print();
HandleC handleC = new HandleC();
handleC.print();
}
}
// ТИПЫ
@Getter
abstract class A {
private String a = "aaa";
}
@Getter
class B extends A {
private String b = "bbb";
}
@Getter
class C extends A {
private String c = "ccc";
}
class MyFactory{
private final String type;
public MyFactory(String type) {
this.type = type;
}
public A getObject(){
return switch (type){
case "B" -> new B();
case "C" -> new C();
default -> throw new IllegalStateException("Unexpected value: " + type);
};
}
}
// КЛАССЫ для работы с типами
class HandleB {
Commons<B> commons = new Commons<>("B");
B instanceB = commons.getObject();
List<B> list = commons.getList();
void print(){
System.out.println(instanceB.getB());
}
}
class HandleC {
Commons2<C> commons = new Commons2<>(C::new);
C instanceC = commons.getObject();
List<C> list = commons.getList();
void print(){
System.out.println(instanceC.getC());
}
}
// КЛАСС с шаблонным кодом
// вариант с фабрикой
class Commons<T extends A> {
MyFactory factory;
public Commons(String type) {
this.factory = new MyFactory(type);
}
List<T> getList() {
List<T> list = new ArrayList<>();
for (int i = 0; i < 3; i++) {
T item = getObject();
list.add(item);
}
return list;
}
T getObject() {
return (T) factory.getObject();
}
}
// вариант с Supplier<>
// https://stackoverflow.com/questions/299998/instantiating-object-of-type-parameter
class Commons2<T extends A> {
private final Supplier<T> supplier;
Commons2(Supplier<T> supplier) {
this.supplier = supplier;
}
List<T> getList() {
List<T> list = new ArrayList<>();
for (int i = 0; i < 3; i++) {
T item = getObject();
list.add(item);
}
return list;
}
T getObject() {
return supplier.get();
}
}
public class Part {
public static void main(String[] args) {
int numberOfThings = 41;
long top = ((long) Math.pow(2, numberOfThings + 1));
long bottom = ((long) Math.pow(2, numberOfThings)) + 1;
int[] things = new int[numberOfThings];
for (int i = 0; i < numberOfThings; i++) {
things[i] = i;
}
while (bottom != top) {
char[] mask = Long.toBinaryString(bottom++).toCharArray();
List<Integer> comb = new ArrayList<>();
for (int i = 0; i < numberOfThings; i++) {
if (mask[i + 1] == '1') comb.add(things[i]);
}
// Обработка комбинации
System.out.println(comb);
}
}
}