有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

从字符串中提取数字并用lambda java列表在中间添加一些文本的方法有哪些

所以我的任务是修改字符串列表(或从中提取更多)的部分,然后在开头、中间添加一些文本,并将数字转换为整数,然后乘以给定的速率。在任务的一部分中,我需要使用lambda和我自己的类,在下一部分中,我必须使用streams来提取和修改所有这些。 我寻找的更多的是线索而不是密码。使用什么方法以及如何使用的一些说明。更多的要求在注释中的代码中 我真的很感激

import java.util.*;

public class ListCreator<S,T> {

List<T> list;

protected ListCreator(List<T> listcreate){
    this.list = listcreate; //kreator robi nowa liste zeby nie modyfikowac
}

public static <S,T> ListCreator<S,T> collectFrom(List<T> srclist){
    ListCreator<S,T> listCreator = new ListCreator<S,T>(srclist);
    listCreator.list = srclist; //tworzy liste lc z zawartoscia listy ktora chce zbadac/zmodyfikowac
    return listCreator;
}

public ListCreator<S,T> when(Selector<T> selector){
    List<T> nextlist = new ArrayList<>();
    for (int i = 0; i < list.size(); i++){
        T arg = list.get(i); //biore indeksy kazdego z elementow
        if(selector.select(arg)){ //patrze czy spelniaja warunki
           nextlist.add(arg);

        }
    }
    this.list = nextlist; /// w koncu przepisuje nextlist do tego obiektu uffff zmarnowalam na tym najwiecej czasu
    return this;
}

public <S> List<S> mapEvery(Mapper<T,S> mapper){
    List<S> dlist = new ArrayList<>();
    for (int i = 0; i < list.size(); i++){
        T arg = list.get(i); //zbieram indeksy elementow jako typu T
        S modarg = mapper.map(arg); //kazdy argument modyfikuje i przypisuje jako argument danego typu
        dlist.add(modarg); // dodaje zmodyfikowany element
    }
    return dlist;
}
}  

import java.util.*;

public class Main {

static List<String> getPricesInPLN(List<String> destinations, double xrate) {
return ListCreator.collectFrom(destinations)
                   .when(e -> e.startsWith("WAW") /*<-- lambda wyrażenie
                            *  selekcja wylotów z Warszawy (zaczynających się od WAW)
                            */
                    )
                   .mapEvery(f -> f.
                           ); /*<-- lambda that creates (multiplies by) the price using the following rate of ratePLNvsEUR and then maps all of it to the result text on console
                               */

   }

  /* main class for first task using lambdas and listcreator to extract 
 all the factors into following result on the console:
 to HAV - price in PLN: 5160
 to DPS - price in PLN: 8600
 to HKT - price in PLN: 4300
   */
rate
public static void main(String[] args) {
    // these are the destination airports 
List<String> dest = Arrays.asList(
  "bleble bleble 2000",
  "WAW HAV 1200",
  "xxx yyy 789",
  "WAW DPS 2000",
  "WAW HKT 1000"
);
double ratePLNvsEUR = 4.30;
List<String> result = getPricesInPLN(dest, ratePLNvsEUR);
for (String r : result) System.out.println(r);
 }
 }

 // main to second task where I need to do all of the previous one but in 
 // one stream without using the listcreator class

  /*<-- imports */

 public class Main {

 public static void main(String[] args) {
// Lista destynacji: port_wylotu port_przylotu cena_EUR 
List<String> dest = Arrays.asList(
  "bleble bleble 2000",
  "WAW HAV 1200",
  "xxx yyy 789",
  "WAW DPS 2000",
  "WAW HKT 1000"
);
double ratePLNvsEUR = 4.30;
List<String> result = 
/*<-- here should be a stream

for (String r : result) System.out.println(r);
}
}

共 (0) 个答案