我有以下对象的列表.使用StreamAPI,我可以得到User它出现的频率数List吗?
public class User {
string name;
int age;
}
Run Code Online (Sandbox Code Playgroud) 考虑到下面的功能,我注意到编译器没有发出任何问题:
private static int returnTwoTypes() {
int a = 1;
if (a == 1) {
return -1;
}
return 'a';
}
Run Code Online (Sandbox Code Playgroud)
为什么我可以在函数返回类型时返回a character和a ?intint
我试图找到有关它的信息但失败了.
如何size()进行HashMap或HashSet实施?它是如何工作的?是手术O(1)还是O(n)手术?
我有这段代码.我该如何改造它?功能风格?事实上,我有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) 每当我尝试从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)
为什么不起作用?
所以我有这个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) 这是我的代码:
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代码中是否有任何错误?我跑的时候没找到一个.
public class WayToGo
{
private int aa, bb;
public void WayToGo(int a, int b)
{
aa = a;
bb = b;
}
}
Run Code Online (Sandbox Code Playgroud) 我想从文本文件中读取一些数据.这是文本文件中数据的格式:
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) 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 ×10
java-8 ×2
java-stream ×2
arraylist ×1
buffer ×1
generics ×1
hashmap ×1
hashset ×1
inheritance ×1
inputstream ×1
map ×1
outputstream ×1