小编Era*_*ran的帖子

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
查看次数

与&lt;T扩展Class&gt;行为混淆

所以我有这个AbstractBook

public abstract class AbstractBook<T extends AbstractPage> implements Serializable{
    public AbstractBook() {
    }

    public abstract void addPage(T var1);
}
Run Code Online (Sandbox Code Playgroud)

我有一个子类:

public class Book extends AbstractBook {

    private ArrayList<Page> allPages;
    private String title;

    Book(String title){
       allPages = new ArrayList<Page>();
    }

    @Override /** Doesn't Work */
    public void addPage(Page page) {
        this.allPages.add();
    }
}
Run Code Online (Sandbox Code Playgroud)

在子类addPage方法中,我无法以给定的形式覆盖该方法,因为该参数要求是一种AbstractPage类型,即使它Page是从扩展而来的AbstractPage

public abstract class AbstractPage implements Serializable {
    public AbstractPage() {
    }
}
Run Code Online (Sandbox Code Playgroud)

public class Page extends …
Run Code Online (Sandbox Code Playgroud)

java generics inheritance

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

在Map中减少For循环

这是我的代码:

package vvv;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class test {

public static void main(String args[]){
    Map<Integer, String> map = new HashMap<Integer, String>();
    map.put(1, "demo");
    map.put(20, "fdemo");
    map.put(60, "gdemo");
    map.put(500, "udemo");
    map.put(8000, "odemo");
    // etc
    int b = 7999;
    for(int i =1; i<=8000; i++)
    {
        if(i == b)
            System.out.println(map.get(b));
    }

}

}
Run Code Online (Sandbox Code Playgroud)

我不想仅仅为了从地图中找到结果而使用大的"for循环",此外,我无法更改地图中的键(例如,我无法将500更改为4).

我该怎么做才能减少循环条件?

java map

-1
推荐指数
1
解决办法
111
查看次数

查找Java错误

以下Java代码中是否有任何错误?我跑的时候没找到一个.

public class WayToGo 
{

    private int aa, bb;

    public void WayToGo(int a, int b) 
    {
        aa = a;
        bb = b;
    }
}
Run Code Online (Sandbox Code Playgroud)

java

-1
推荐指数
1
解决办法
45
查看次数

读取文本文件时无限循环

我想从文本文件中读取一些数据.这是文本文件中数据的格式:

A,20, ,0
B,30, ,0
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

public ArrayList rechercherSalle() 
{
    String nom;
    String ligne;
    ArrayList<Salle> listeSalles = new ArrayList<Salle>();
    Salle salle = new Salle();
    try {
        InputStream flux = new FileInputStream("salle.txt");
        InputStreamReader lecture = new InputStreamReader(flux);
        BufferedReader buff = new BufferedReader(lecture);
        ligne = buff.readLine();
        while (ligne != null) {
            String[] objetSalle = ligne.split(",");
            nom = objetSalle[0];
            String capacite_maxString = objetSalle[1];
            Integer capacite_max = Integer.parseInt(capacite_maxString);
            String capacite_actuelleString = objetSalle[3];
            Integer capacite_actuelle = Integer.parseInt(capacite_actuelleString);
            String proprietaire = objetSalle[2];
            salle = new Salle(); …
Run Code Online (Sandbox Code Playgroud)

java buffer inputstream outputstream

-1
推荐指数
1
解决办法
126
查看次数

在Java中,运算符优先级、递增和递减,为什么不执行第一个println语句?

class Example{
    public static void main(String args[]){
        int x=99;
        if(x++==x){
            System.out.println("x++==x : "+x); //Why this code line is not run?
        }
        if(++x==x ){
            System.out.println("++x==x : "+x); 
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

为什么第一条语句没有println执行?

java

-1
推荐指数
2
解决办法
91
查看次数