P1.编写如下程序。
给定两个集合A和B,分别求AUB,A∩B,A-B的值。

package test;
import java.util.*;
//P1.编写如下程序。
//给定两个集合A和B,分别求AUB,A∩B,A-B的值。
public class Keep_9 {public static void main(String[] args) {List<String> ls = new ArrayList<String>();List<String> ls2 = new ArrayList<String>();ls.add("George");ls.add("Jim");ls.add("John");ls.add("Blake");ls.add("Kevin");ls.add("Micheal");ls2.add("George");ls2.add("Katie");ls2.add("Micheal");ls2.add("Kevin");ls2.add("Ryan");System.out.println("第一个集合");// 打印输出第一个集合ListIterator lit0 = ls.listIterator();while (lit0.hasNext()) {System.out.print("  " + lit0.next());}System.out.println();System.out.println("第二个集合");// 打印输出第二个集合lit0 = ls2.listIterator();while (lit0.hasNext()) {System.out.print("  " + lit0.next());}System.out.println();jiao(ls, ls2);System.out.println();System.out.println("两个集合的并");lit0 = bing(ls, ls2).listIterator();while (lit0.hasNext()) {System.out.print("  " + lit0.next());}System.out.println();cha(ls, ls2);}public static void jiao(List ls, List ls2) {System.out.println("两个集合的交");List list = new ArrayList(Arrays.asList(new Object[ls.size()]));Collections.copy(list, ls);list.retainAll(ls2);Iterator lit0 = list.listIterator();while (lit0.hasNext()) {System.out.print("  " + lit0.next());}}public static List bing(List ls, List ls2) {List list = new ArrayList(Arrays.asList(new Object[ls.size()]));Collections.copy(list, ls);list.addAll(ls2);// 两个集合的并return list;}public static void cha(List ls, List ls2) {ls = bing(ls, ls2);List list = new ArrayList(Arrays.asList(new Object[ls.size()]));Collections.copy(list, ls);list.removeAll(ls2);// 两个集合的差System.out.println("两个集合的差");Iterator lit0 = list.listIterator();while (lit0.hasNext()) {System.out.print("  " + lit0.next());}}}

P2. 编写如下程序。
1)创建HashSet对象,写入50个1-20的随机整数。
2)创建ArrayList对象,读取h的所有元素后写入。
3)创建HashMap对象,读取a的第5到第10个元素后写入,key初始值100,每存入一个元素+1。

package test;
import java.util.*;
//P2.   编写如下程序。
//1)创建HashSet对象,写入50个1-20的随机整数。
//2)创建ArrayList对象,读取h的所有元素后写入。
//3)创建HashMap对象,读取a的第5到第10个元素后写入,key初始值100,每存入一个元素+1。
public class  Keep_10{public static void main(String[] args) {Set<Integer> strList = new HashSet<Integer>();Random r=new Random(1);for(int i=0;i<50;i++){int ran1=r.nextInt(21);strList.add(ran1);}Iterator it = strList.iterator();ArrayList<Integer> a = new ArrayList<Integer>();while(it.hasNext()){   int i=(int)it.next();a.add(i);}System.out.println(a);HashMap t = new HashMap();int key=100;for(int i=0;i<6;i++){   List<Integer> c=a.subList(5, 5+i);t.put(key, c);key++;}System.out.println(t);}
}

P3. 编写如下程序(可选题)。
有三批苹果,采用交互指令方式(命令行,或是程序中输入)输入每批苹果数量,即分三次运输到仓库,苹果重量在0.5kg~1kg之间随机取值,试编写程序对苹果进行筛选(注: 使用集合操作):
1)分别统计75#(0.5~0.6),80#(0.6~0.8),85#(0.8~1.0)苹果个数;
2)给出这些苹果的最大重量与最小重量;
3)加工这些苹果(remove,删除所有对象)。

package test;public class Apple {private double weight;public Apple() {}public Apple(double weight) {this.weight = Double.parseDouble((weight+"").substring(0,3));}public double getWeight() {return weight;}public void setWeight(double weight) {this.weight = weight;}@Overridepublic String toString() {return "Apple{" + "weight=" + weight + '}';}
}

苹果尺寸计算的代码

package test;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
import java.util.Scanner;
public class Storage {private ArrayList<Apple> apples;private double appleSizeMin;private double appleSizeMax;private double weightMin;private double weightMax;private int size;public Storage() {apples = new ArrayList<>();}public void add(int num){for (int i=0;i<num;i++){/*** Math.random() 的范围 0 - 1.0* 而苹果要求 0.5 - 1.0间取值,故做如下处理*/Apple apple = new Apple(0.5 + Math.random() / 2);new Random();System.out.println(apple);apples.add(apple);}}// 根据型号设置 每个型号苹果的大小范围 这里为 左闭右开public int calculation(String size){switch (size){case "75#":appleSizeMin = 0.5;appleSizeMax = 0.6;break;case "80#":appleSizeMin = 0.6;appleSizeMax = 0.8;break;case "85#":appleSizeMin = 0.8;appleSizeMax = 1.0;break;}return calculation(appleSizeMin,appleSizeMax);}// 以左闭右开原则进行统计private int calculation(double appleSizeMin,double appleSizeMax){int sum = 0;for (Apple apple : apples){if (apple.getWeight() >= appleSizeMin && apple.getWeight() < appleSizeMax){sum++;}}return sum;}public double getWeightMin(){double temp = apples.get(0).getWeight();for (Apple apple : apples){if (apple.getWeight() < temp){temp = apple.getWeight();}}return temp;}public double getWeightMax(){double temp = apples.get(0).getWeight();for (Apple apple : apples){if (apple.getWeight() > temp){temp = apple.getWeight();}}return temp;}// 使用迭代器进行删除public void remove(){Iterator<Apple> iterator = apples.iterator();while (iterator.hasNext()){Apple apple = iterator.next();System.out.println(apple + " 被删除");iterator.remove();}}// 统计仓库苹果数量public int size(){return apples.size();}public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int num;Storage storage = new Storage();System.out.println("第一次输入数量:");num = scanner.nextInt();storage.add(num);System.out.println("第二次输入数量:");num = scanner.nextInt();storage.add(num);System.out.println("第三次输入数量:");num = scanner.nextInt();storage.add(num);System.out.println("苹果个数:" + storage.size());System.out.println("#75个数:" + storage.calculation("75#"));System.out.println("#80个数:" + storage.calculation("80#"));System.out.println("#85个数:" + storage.calculation("85#"));System.out.println(storage.getWeightMin());System.out.println(storage.getWeightMax());storage.remove();System.out.println("苹果个数:" + storage.size());}
}

Java HashMap、Hashset、ArrayList和函数间调用的练习相关推荐

  1. 云函数本地调用与云函数间调用

    云函数本地调用与云函数间调用 根据上一篇讲到的云函数使用,讲到了云函数的基本使用方式,并给出了使用的示例.现在我们来讲一下常用的云函数调用. 云函数基本调用方法 根据官网给出的本地调用云函数的基本示例 ...

  2. Python程序中各函数间调用关系分析与可视化

    中国大学MOOC"Python程序设计基础"免费学习地址 2020年秋季学期Python教材推荐与选用参考 推荐图书: <Python程序设计(第3版)>,(ISBN: ...

  3. 41.Java HashMap有序集合

    Java HashMap 在ArrayList一章中,您了解到数组将项存储为有序集合,并且必须使用索引号(int类型)访问它们.然而,哈希映射将项目存储在"键/值"对中,您可以通过 ...

  4. Java 集合HashSet TreeSet HashMap ArrayList TreeList

    1.体系结构 2.工具类: package collection;public class InnerTest {private int age;public final int getAge() { ...

  5. Java集合 -- HashSet 和 HashMap

    HashSet 集合 HashMap 集合 HashSet集合 1.1 Set 接口的特点 Set体系的集合: A:存入集合的顺序和取出集合的顺序不一致 B:没有索引 C:存入集合的元素没有重复 1. ...

  6. 聊聊传说中的散列哈希Hash算法,以及Java中的HashTable,HashMap,HashSet,ConcurrentHashMap......

    建议本文结合java源码来阅读,看了之后就什么都懂了,还有参考文献. 散列(Hash) 是一种按关键字编址的存储和检索方法 散列表(HashTable)根据元素的关键字确定元素的位置 散列函数(Has ...

  7. Java集合 HashSet 和 HashMap

    HashSet 集合 HashMap 集合 HashSet集合 Set 接口的特点 Set体系的集合: 存入集合的顺序和取出集合的顺序不一致 没有索引 存入集合的元素没有重复 HashSet 使用&a ...

  8. Java HashSet和Java HashMap

    Java HashSet Java 集合框架 HashSet 基于 HashMap 来实现的,是一个不允许有重复元素的集合. HashSet 允许有 null 值. HashSet 是无序的,即不会记 ...

  9. HashTable HashMap HashSet区别(java) [From 爱做饭的小莹子]

    Hashtable: 1. key和value都不许有null值 2. 使用enumeration遍历 3. 同步的,每次只有一个线程能够访问 4. 在java中Hashtable是H大写,t小写,而 ...

最新文章

  1. 您是否应该始终偏爱xrange()而不是range()?
  2. Host is not allowed to connect to this MySQL server解决方法
  3. DevExpress TreeListLookupEdit常用技巧
  4. 玩转C#控件-常用控件属性
  5. GCN pytorch实现 笔记
  6. 简易版viewport
  7. 检测浏览器是否支持ajax ===小代码
  8. 9-汇编转移指令-offset+jmp+jcc+loop
  9. 关于液晶触摸屏的信号传递?
  10. 麒麟V10图形界面安装与运行人大金仓数据库
  11. 对数数学知识回忆(log)
  12. 什么是 ARPU 为什么你应该关心 ARPU?
  13. 主板怎么安装在计算机主机箱,计算机主板、主机、机箱和计算机的制作方法
  14. 正则 负数 python_【记录】Python推特爬虫思路
  15. 智慧农业管理系统应用
  16. 计算机毕业设计(81)php小程序毕设作品之校园跑腿小程序系统
  17. 华三交换机配置access命令_H3C交换机基本配置命令明细一览
  18. 12306官方火车票各种Api接口
  19. 【电力电子技术AC-DC】电容滤波的单相不可控整流电路simulink仿真
  20. openfire + fastpath + webchat + spark 搭建在线客服系统

热门文章

  1. html刷新页面出现412,html5-video – 如何修复412(前置条件失败)错误HTML5视频标记
  2. wamp php非线程安全,wampserver PHP多版本切换
  3. python用selenium爬取网页数据_Python项目实战:使用selenium爬取拉勾网数据
  4. SPQuery简单使用示例
  5. “自拍神器”贴心实用功能大曝光
  6. Spark 学习文章
  7. vue-concise-slider vue滑动组件
  8. 用ldap方式访问AD域的的错误解释
  9. [导入]用程序来还原数据库(一个遗留了两年的问题)
  10. 前端质量提升利器-马可代码覆盖率平台