首页 > 其他分享 >练习——hashset简单练习

练习——hashset简单练习

时间:2023-04-07 16:11:22浏览次数:33  
标签:return name hashset int 练习 birthday 简单 Employee public

package com.collection_.set_;

import java.util.HashSet;
import java.util.Objects;

public class HashSetExercise {
    public static void main(String[] args) {
        /*
        定义一个Employee类,该类包含private成员属性name , age要求:
        创建3个Employee 对象放入 HashSet中
        当name利lage的值相同时,认为是相同员工,不能添加到HashSet集合中

        HashSet HashSet = new HashSet();
        HashSet.add(new Employee("lihua",18));
        HashSet.add(new Employee("john",28));
        HashSet.add(new Employee("lihua",18));

        System.out.println(HashSet);
    */

        /*
        定义一个Employee类,该类包含:private成员属性name,sal,birthday(MyDate类型),
        其中 birthday 为 MyDate类型(属性包括:year, month, day),要求:
            1.创建3个Employee 放入 HashSet中
            2.当name和birthday的值相同时,认为是相同员工,不能添加到HashSet集合中
         */

        HashSet hashSet = new HashSet();
        hashSet.add(new Employee("lihua",12000,new Mydate(2001,1,5)));
        hashSet.add(new Employee("zhouqi",12000,new Mydate(2001,1,5)));
        hashSet.add(new Employee("zhouqi",12000,new Mydate(2001,2,5)));
        hashSet.add(new Employee("zhouqi",11000,new Mydate(2001,1,5)));
        hashSet.add(new Employee("lihua",12000,new Mydate(2001,1,5)));

        System.out.println("hashset" + hashSet);
    }
}


/*class Employee{
    private String name;
    private int age;

    public Employee(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }


    //如果name和age相同,则返回相同的hash值

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Employee employee = (Employee) o;
        return age == employee.age && Objects.equals(name, employee.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}
*/
class Employee{
    private String name;
    private int sal;
    private Mydate birthday;

    public Employee(String name, int sal, Mydate birthday) {
        this.name = name;
        this.sal = sal;
        this.birthday = birthday;
    }

    public String getName() {
        return name;
    }

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

    public int getSal() {
        return sal;
    }

    public void setSal(int sal) {
        this.sal = sal;
    }

    public Mydate getBirthday() {
        return birthday;
    }

    public void setBirthday(Mydate birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", sal=" + sal +
                ", birthday=" + birthday +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Employee employee = (Employee) o;
        return Objects.equals(name, employee.name) && Objects.equals(birthday, employee.birthday);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, birthday);
    }
}

class Mydate{
    private int year;
    private int month;
    private int day;

    public Mydate(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    @Override
    public String toString() {
        return "Mydate{" +
                "year=" + year +
                ", month=" + month +
                ", day=" + day +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Mydate mydate = (Mydate) o;
        return year == mydate.year && month == mydate.month && day == mydate.day;
    }

    @Override
    public int hashCode() {
        return Objects.hash(year, month, day);
    }
}

标签:return,name,hashset,int,练习,birthday,简单,Employee,public
From: https://www.cnblogs.com/Q1u-ovo/p/17296496.html

相关文章

  • Halcon练习
    1、数组操作:arr:=[1,2,3,4,5]//定义数组cnt:=|arr|//数组长度a03:=arr[2]//取单个值par:=arr[0:2]//取多个值copy:=arr[0:cnt-1]//复制数组  区域生长图像分割regiongrowing(Image,Outregion:Row,Col,Tolerance,MinSize)函数:regiongrowing(Im......
  • django-content-type简单示例
    fromdjango.contrib.contenttypes.fieldsimportGenericForeignKeyfromdjango.contrib.contenttypes.modelsimportContentTypefromdjango.dbimportmodelsclassComment(models.Model):content_type=models.ForeignKey(ContentType,on_delete=models.CASCA......
  • proe5.0学习笔记(13)建模练习-管道
    1.建模练习-管道        点击名称下面一行  第一个点和第二个点的位置  ......
  • j2me小练习,教学用
    有用的J2ME功能函数swap(inta,intb){a=a^b;b=a^b;a=a^b;}/***求平方根*@paramvalue定义域*@return值域*/finalpublicstaticintsqrt(intvalue){intsqrt=0;for(intk=0x100000;k!=0;k>>=2){in......
  • 自己早期(android1.1)做的一个绘图练习
    仅仅是个练习而已!只做了第一个菜单的事件。其他都没事件。代码片段publicmPayCanvas(Contextcontext){ super(context); //获得屏幕宽高 WindowManagerwindowManager=getWindowManager(); Displaydisplay=windowManager.g......
  • Dialog&Notification&OptionsMenu练习
    下面的menu没有意义,仅仅是个练习而已,看图先:布局:<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"androi......
  • ImageSwitcher&Gallery练习
    先看图再说:布局如下:<?xmlversion="1.0"encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout......
  • SQLiteOpenHelper&SharedPreferences练习
    目录结构:packagecom.dc.app;importjava.text.DecimalFormat;importjava.util.Locale;importandroid.app.Activity;importandroid.app.AlertDialog;importandroid.app.Dialog;importandroid.app.Notification;importandroid.app.Notificati......
  • android打造的最简单计算器界面
    先看图:这里主要是锻炼一下TableLayout布局,注意其中的android:stretchColumns="0,1,2,3"属性,该属性可以控制每列的宽的权重,类似weight,由于这里4列都是“平等的”,所以是“0,1,2,3”,全部布局文件如下:<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:an......
  • android之简单数据存储Preference
    这里的持久化其实就是本地配置文件的读写,实现方法是通过Activity.getPreferences(int)获取SharedPreferences对象,然后操作配置文件的读写,值得注意的是以下几点:1)Activity.getPreferences(intmode)等价于Content.getSharedPreferences(Stringfilename,intmod......