java 集合 HashSet 实现随机双色球 HashSet addAll() 实现去重后合并 HashSet对象去重 复写 HashCode()方法和equals方法 ArrayList去重
package com.swift.lianxi; import java.util.HashSet; import java.util.Random; /*训练知识点:HashSet 训练描述 双色球规则:双色球每注投注号码由6个红色球号码和1个蓝色球号码组成。 红色球号码从1—33中选择;蓝色球号码从1—16中选择.请随机生成一注双色球号码*/ public class Test4 { public static void main(String[] args) { Random ran = new Random(); HashSet<Integer> hong = new HashSet<Integer>(); while(hong.size()<6) { int hongqiu=ran.nextInt(33)+1; hong.add(hongqiu); } int lanqiu=ran.nextInt(16)+1; System.out.print("双色球红球号码是: "); for(Integer i:hong) { System.out.print(i+" "); } System.out.print(" 双色球篮球号码是: "); System.out.print(lanqiu+""); } }
上边是双色球
下边是使用HashSet 去重后合并
package com.swift.lianxi; import java.util.ArrayList; import java.util.HashSet; /*训练知识点:HashSet 训练描述 有2个ArrayList,元素如下: array1 = "a","b","a","c","d",array2 = "e","f","a","d","g",将两个集合重复项去除后合并成一个集合 操作步骤描述 1.创建ArrayList array1 2.使用add方法为array1存放对应的元素 3.创建ArrayList array2 4.使用add方法为array2存放对应的元素 5.创建HashSet用于存放去除重复的元素 6.遍历array1,获取到每个元素,将每个元素添加到HashSet中 7.遍历array2,获取到每个元素,将每个元素添加到HashSet中 8.输出HashSet中去除重复后的元素*/ public class Test3 { public static void main(String[] args) { ArrayList<String> array1 = new ArrayList<String>(); array1.add("a"); array1.add("b"); array1.add("a"); array1.add("c"); array1.add("d"); HashSet<String> set = new HashSet<String>(); set.addAll(array1); ArrayList<String> array2 = new ArrayList<String>(); array2.add("e"); array2.add("f"); array2.add("a"); array2.add("d"); array2.add("g"); set.addAll(array2); for(String str:set) { System.out.println(str); } } }
HashSet对象去重 复写 HashCode()方法和equals方法
package com.swift.lianxi; import java.util.HashSet; import java.util.LinkedHashSet; /*定义一个Student类,包含名称,年龄,性别(姓名,年龄,性别完全相同视为同一学生) 创建10个Student对象,至少有两个学生姓名,年龄,性别完全相同. 把这10个学生添加到Set集合中,不可以重复, 遍历Set集合打印学生信息,使用两种方式*/ public class Demo3 { public static void main(String[] args) { HashSet<Students> set = new LinkedHashSet<Students>(); set.add(new Students("zhangsan",20,"男")); set.add(new Students("lisi",22,"男")); set.add(new Students("wangwu",223,"男")); set.add(new Students("zhaoliu",1,"男")); set.add(new Students("tianqi",54,"男")); set.add(new Students("wangermazi",209,"男")); set.add(new Students("zhangsan",20,"男")); set.add(new Students("zhangsan",20,"男")); set.add(new Students("zhangsan",20,"男")); set.add(new Students("zhangsan",20,"男")); for(Students stu:set) { System.out.println(stu.toString()); } } } /** * @author Advancing-Swift * */ class Students{ private String name; private int age; private String sex; 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; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Students(String name, int age, String sex) { super(); this.name = name; this.age = age; this.sex = sex; } public Students() { super(); } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + age; result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + ((sex == null) ? 0 : sex.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Students other = (Students) obj; if (age != other.age) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (sex == null) { if (other.sex != null) return false; } else if (!sex.equals(other.sex)) return false; return true; } @Override public String toString() { return "Students [name=" + name + ", age=" + age + ", sex=" + sex + "]"; } }
ArrayList去重
package com.swift.kaoshi; import java.util.Iterator; import java.util.LinkedList; /*训练描述 已知数组存放一批QQ号码.QQ号码最长为11位,最短为5位 String[] strs = {"12345","67891",1"2347809933","98765432102","67891","12347809933"} 将该数组里面的所有qq号都存放在LinkedList中,将list中重复元素删除,将list中所有元素用两种方式打印出来 操作步骤描述 1.定义QQ号码数组String[] strs 2.创建LinkedList 3.遍历strs获取每个qq号码 4.判断LinkedList是否已经存在这个qq号码 5.不存在这个qq号码则添加到LinkedList中 6.增强for遍历LinkedList 7.迭代器遍历LinkedList*/ /* * ArrayList也可以存放唯一的内容,通过toArray()转成数组,再通过contains判断集合中是否存在 */ public class Test1 { public static void main(String[] args) { String[] strs = {"12345","67891","12347809933","98765432102","67891","12347809933"}; LinkedList<String> qqhao = new LinkedList<String>(); for(String str:strs) { if(!qqhao.contains(str)) { qqhao.add(str); } } Iterator<String> it = qqhao.iterator(); while(it.hasNext()) { String qq = it.next(); System.out.println(qq); } } }
Never waste time any more, Never old man be a yong man