小编Era*_*ran的帖子

将<Map <String,String >>流<java>中的Set <String>值

我有一个Map流我怎么能得到具有Map值的Set?

我在这里部分地做了什么

Set<String> jcfTargetTables = measure.getConditionMap().values()
        .stream()
        .map(Condition::getJoinConditionFilter)
        .filter(jcf -> jcf!=null)
        .map(JoinConditionFilter::getTableMapping);
Run Code Online (Sandbox Code Playgroud)

最后一行给了我一个Stream<Map<String,String>>,我如何继续获取Set是Map的值?

java java-8 java-stream

0
推荐指数
1
解决办法
970
查看次数

如何使用Streams将一种类型的List转换为另一种类型的List?

我想用Streams来实现以下目的:

我有完整不同结构的列表InputOutput对象.

使用for循环,我可以将a转换List<Input>为a List<Output>,如下所示:

for (Input input : listOfInput) {
    Output currentOutPutInstance = new Output();
    currentOutPutInstance.setArg1(input.getArg2());
    currentOutPutInstance.setArg2(input.getArg7());
    listOfOutPuts.add(currentOutPutInstance);
}
Run Code Online (Sandbox Code Playgroud)

有了溪流,我试过这样的事情:

private List<Output> getOutPutListFromInputList(List<Input> inPutList) {
    List<Output> outPutList = new ArrayList<Output>();
    outPutList = listOfPoolsInRun.stream.filter(<Somehow converting the input into output>)
                                 .collect(Collectors.toList()); 
}
Run Code Online (Sandbox Code Playgroud)

注意: 我不确定Stream我应该使用哪种方法.我filter只是用来显示一些虚拟代码.

java java-8 java-stream

0
推荐指数
1
解决办法
125
查看次数

如何为lambda表达式指定类型?

我试过了:

Stream stream = Pattern.compile(" ").splitAsStream(sc.nextLine());
stream.forEach(item) -> {});
Run Code Online (Sandbox Code Playgroud)

得到了:

Compilation Error... 
 File.java uses unchecked or unsafe operations.
 Recompile with -Xlint:unchecked for details.
Run Code Online (Sandbox Code Playgroud)

所以我试过了:

Stream stream = Pattern.compile(" ").splitAsStream(sc.nextLine());
stream.forEach((String item) -> {});
Run Code Online (Sandbox Code Playgroud)

得到了:

Compilation Error... 
15: error: incompatible types: incompatible parameter types in lambda expression
            stream.forEach((String item) -> {});
                           ^
 Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error
Run Code Online (Sandbox Code Playgroud)

我怎样才能进行这个.forEach()传递编译?

java generics java-8

0
推荐指数
1
解决办法
1368
查看次数

实例化具有默认方法的接口

我遇到了这个问题:

以下是什么输出?

1  public class A {
2      public static void main(String[] args){
3          I i = new I() {};
4          System.out.println(I.x + i.getValue() + "" + i);
5      }
6  }
7
8  interface I {
9      int x = 10;
10
11     public default int getValue() {
12         return 5;
13     }
14
15     public default String toString() {
16         return "I";
17     }
18 }
Run Code Online (Sandbox Code Playgroud)

我的想法:

我的第一直觉告诉我 - 我是新的我(){}?因此我们无法实例化接口 - 问题1.

然后我认为公共默认String toString()?重写Object类方法?问题听起来不错 - …

java interface object java-8

0
推荐指数
1
解决办法
923
查看次数

Java 8中的Comparator.cominging为Map.Entry

给定以下代码:

@Test
public void test7() {
    Map<String, Integer> sortedData = new HashMap<>();
    sortedData.put("One", 1);
    sortedData.put("Two", 2);
    sortedData.put("Three", 3);

    Stream<Map.Entry<String, Integer>> stream = sortedData.entrySet().stream();
    List<String> sorted = stream
            .sorted(Comparator.comparing(Map.Entry::getValue))
            .map(Map.Entry::getKey)
            .collect(Collectors.toList());
} 
Run Code Online (Sandbox Code Playgroud)

它可以成功编译,但是当我更改时

.sorted(Comparator.comparing(Map.Entry::getValue))
Run Code Online (Sandbox Code Playgroud)

.sorted(Comparator.comparing(Map.Entry::getValue).reversed())
Run Code Online (Sandbox Code Playgroud)

编译器抱怨 Non static method can't be referenced in static context

我可以想象这是因为getValue不是的静态方法Map.Entry,但是我在这里无法解释问题。

java java-8 java-stream

0
推荐指数
1
解决办法
1722
查看次数

Java Stream出现次数

我有以下对象的列表.使用StreamAPI,我可以得到User它出现的频率数List吗?

public class User {
    string name;
    int age;
}
Run Code Online (Sandbox Code Playgroud)

java java-8 java-stream

0
推荐指数
1
解决办法
83
查看次数

为什么编译器在返回错误类型时不会抱怨?

考虑到下面的功能,我注意到编译器没有发出任何问题:

private static int returnTwoTypes() {
    int a = 1;
    if (a == 1) {
        return -1;
    }
    return 'a';
}
Run Code Online (Sandbox Code Playgroud)

为什么我可以在函数返回类型时返回a character和a ?intint

java

0
推荐指数
1
解决办法
104
查看次数

Java中的HashMap和HashSet size()

我试图找到有关它的信息但失败了.

如何size()进行HashMapHashSet实施?它是如何工作的?是手术O(1)还是O(n)手术?

java hashmap hashset

0
推荐指数
1
解决办法
107
查看次数

在流中打开流

我有这段代码.我该如何改造它?功能风格?事实上,我有List<X>.每个X包含List<V>.此列表中的每个V都有List<M>一个参数.我需要构建Map<X,Y>,其中Y是存储在对象X中聚合的所有V对象中的所有M个对象的数量.

HashMap<Country, Integer> modelsPerCountryMap = new HashMap<>();
int count;
for (Country country : CountryDataSingleton.getCountryDataCollection()) {
    count = 0;
    for (CarMaker cm : country.getListOfMakers()) {
        count += cm.getModels().size();
    }
    modelsPerCountryMap.put(country, count);
}
Run Code Online (Sandbox Code Playgroud)

java functional-programming java-8 java-stream

0
推荐指数
1
解决办法
105
查看次数

List的removeIf()没有按预期工作

每当我尝试从List使用list.removeIf(condition)中删除一个元素时,它会抛出UnsupportedOperationException:

public class Test 
{
    public static void main(final String[] args) 
    {
        String[] stringArray = new String[]{"A","B","C","D"}; 
        List<String> stringList = Arrays.asList(stringArray); 
        stringList.forEach(System.out::println); 
        stringList.removeIf((String string) -> string.equals("B")); 
        stringList.forEach(System.out::println); 
    }
}
Run Code Online (Sandbox Code Playgroud)

为什么不起作用?

java arraylist

0
推荐指数
1
解决办法
549
查看次数