相关疑难解决方法(0)

Java 8 Streams - 比较两个列表的对象值并将值添加到新列表?

我有两个List包含此类对象的 s:

public class SchoolObj
{
    private String name;
    private String school;

    public SchoolObj()
    {
        this(null, null);
    }

    public SchoolObj(String nameStr, String schoolStr)
    {
        this.setName(nameStr);
        this.setSchool(schoolStr);
    }

    public String getName()
    {
        return this.name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getSchool()
    {
        return this.school;
    }

    public void setSchool(String school)
    {
        this.school = school;
    }

    @Override
    public String toString()
    {
        return this.getName() + ' ' + this.getSchool();
    }
}
Run Code Online (Sandbox Code Playgroud)

我想通过name和比较这两个列表中的对象school。如果它们相等,我需要创建一个 …

java lambda arraylist java-8 java-stream

5
推荐指数
2
解决办法
5万
查看次数

Java HashSet包含无法正常工作的函数

我正在写一个简单的程序如下:给定两个数字M和N,p来自[M,N],q来自[1,p-1],找到p/q的所有不可约分数.我的想法是蛮力所有可能的p,q值.并使用HashSet来避免重复的分数.但是,不知何故,contains函数没有按预期工作.

我的代码

import java.util.HashSet;
import java.util.Set;

public class Fraction {
    private int p;
    private int q;

    Fraction(int p, int q) {
        this.p = p;
        this.q = q;
    }

    public static int getGCD(int a, int b) {
        if (b == 0)
            return a;
        else 
            return getGCD(b, a % b);
    }

    public static Fraction reduce(Fraction f) {
        int c = getGCD(f.p, f.q);
        return new Fraction(f.p / c, f.q / c);
    }

    public static HashSet<Fraction> getAll(int m, int n) {
        HashSet<Fraction> res = …
Run Code Online (Sandbox Code Playgroud)

java hash hashcode hashset

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

标签 统计

java ×2

arraylist ×1

hash ×1

hashcode ×1

hashset ×1

java-8 ×1

java-stream ×1

lambda ×1