Collection 接口的特点是 存放list与set共性内容 没有直接实现类 没有明确的存储特点要求 ;
Set 接口的特点是元素 无 (有|无)顺序,不可 (可以|不可以)重复;
A.可以利用Set 创建集合对象,存储多个对象
B.可以利用下标操作集合中的数据
C.Set 集合中所有方法继承于父接口Collection
D.以上描述都不正确
A.编译不通过 B. 编译通过,运行时异常
C. 编译运行都正常,输出 3 D. 编译运行都正常,输出 4
hashCode里面的salary 前面加强转(int)
Equals 里面
Public boolean equals (Object o){
If(this == o) return true;
If(this.getClass != o.getClass || o == null) return false;
Worker w = (Worker)o;
return name.equals(o.Name) && salary == w.salary && age ==w.age;
}
将第三个add的0删掉,set的集合不能用下标赋值
①创建多个商品对象,存储在 Set 集合中,集合中不能出现重复商品,商品名相同即为重复,在 Product 类中添加必要的方法;
②显示所有的商品信息;
③打印输出商品价格 > 1000 的所有商品信息;
④打印售空的商品的信息;
⑤打印输出商品产地为"北京"的商品信息;
⑥输入一个商品名,查询出此类商品的信息,如果不存在,则打印商品"商场无此商品!!!"
⑦输入一个价格段,查询出集合中所有在这个价格区间的所有商品信息。
public class Product {
private String goosName;
private double price;
private int quantity;//数量
private String area;
public Product() {
}
public Product(String goosName, double price, int quantity, String area) {
this.goosName = goosName;
this.price = price;
this.quantity = quantity;
this.area = area;
}
public String getGoosName() {
return goosName;
}
public void setGoosName(String goosName) {
if(!this.goosName.equals(goosName)){
this.goosName = goosName;
}
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
@Override
public String toString() {
return "Produtor{" +
"goosName='" + goosName + '\'' +
", price=" + price +
", quantity=" + quantity +
", area='" + area + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Product product = (Product) o;
return goosName.equals(product.getGoosName());
}
@Override
public int hashCode() {
return goosName.hashCode();
}
}
public class zy_6 {
public static void main(String[] args) {
Set set = new HashSet<>();
set.add(new Product("小米",1000,100,"北京"));
set.add(new Product("小米",2000,99,"beijing"));
set.add(new Product("红米",3000,100,"杭州"));
set.add(new Product("锤子",4000,0,"苏州"));
set.forEach(p -> System.out.println(p));
for (Product product : set) {
if(product.getPrice()>1000){
System.out.println(product);
}
}
for (Product product : set) {
if(product.getQuantity() ==0){
System.out.println(product);
}
}
for (Product product : set) {
if(product.getArea().equals("北京")){
System.out.println(product);
}
}
Scanner sc = new Scanner(System.in);
System.out.println("输入要查找的手机品牌");
String name = sc.next();
boolean flag = false;
for (Product product : set) {
if(product.getGoosName().equals(name)){
System.out.println(product);
flag = true;
}
}
if(!flag){
System.out.println("商场无此商品!!!");
}
System.out.println("7======================");
System.out.println("输入要查找的价格区间例如:1000-2000");
String price = sc.next();
String[] split = price.split("-");
int price1 = Integer.parseInt(split[0]);
int price2 = Integer.parseInt(split[1]);
boolean flag2 = false;
for (Product product : set) {
double d = product.getPrice();
if(d > price1 && d
int n = rd.nextInt(n); //0~n -1之间的整数
public class zy_7 {
public static void main(String[] args) {
Set set = new HashSet<>();
Random r = new Random();
while (set.size()<50){
int n = r.nextInt(50+1);
set.add(n);
}
System.out.println(set.size());
}
}
package com.by.homework4;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Scanner;
import java.util.Set;
public class Test8 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入字符串");
String str=sc.next();
String []strs=str.split("");
Set set=new LinkedHashSet<>();
for (int i = 0; i < strs.length; i++) {
set.add(strs[i]);
}
set.forEach(s -> System.out.print(s));
}
}
public class zy_9 {
public static void main(String[] args) {
Set set = new HashSet<>();
Scanner sc = new Scanner(System.in);
while (true){
System.out.println("输入学生信息,格式为:张三/18/男/99.5 输入:0退出");
String s = sc.next();
if(s.equals("0")){
break;
}
String[] ss = s.split("/");
Double score = ss[3].equals("null") ? null : Double.valueOf(ss[3]);
set.add(new Student(ss[0],Integer.parseInt(ss[1]),ss[2],score));
}
System.out.println("1===================");
for (Student s : set) {
if(s.getScore() != null && s.getScore()>=80){
System.out.println(s);
}
}
System.out.println("2===================");
for (Student s : set) {
if(s.getScore() == null){
System.out.println("没有参加考试的学生:"+s);
}
}
System.out.println("5===================");
Set set2 = new TreeSet<>(new Comparator() {
@Override
public int compare(Student o1, Student o2) {
if(o1.getScore()o2.getScore()){
return -1;
}else {
return 0;
}
}
});
set2.addAll(set);
for (Student student : set2) {
System.out.println(student);
}
}
}