import java.awt.print.PrinterGraphics;
import java.util.*;public class Test {public static void main(String[] args) {Set a = new HashSet<>();a.add("aaa");a.add("bbb");a.add("aaa");System.out.println(a);HashSet integers = new HashSet<>();//Integer 是int的封装类,Integer类型是表示集合里面的内容都是Int类型的integers.add(10);integers.add(20);integers.add(10);System.out.println(integers);//集合中的内容不重复}
}
import java.awt.print.PrinterGraphics;
import java.util.*;public class Test {public static void main(String[] args) {Set students = new HashSet<>();Student s1 = new Student("刘备",50);students.add(s1);students.add(new Student("关羽",48));System.out.println(students);}
}
import java.util.Objects;public class Student {private String name;private int age;//重写toString方法@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +'}';}
//重写equals(用来比较和判断重复的)和HashCode方法(这个方法是返回哈希地址)@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;Student student = (Student) o;return age == student.age && Objects.equals(name, student.name);}@Overridepublic int hashCode() {return Objects.hash(name, age);}//空构造public Student() {}//满构造public Student(String name, int age) {this.name = name;this.age = age;}//get和set方法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;}
}
import java.awt.print.PrinterGraphics;
import java.util.*;public class Test {public static void main(String[] args) {LinkedHashSet stringss = new LinkedHashSet();stringss.add("a");stringss.add("b");stringss.add("a");System.out.println(stringss);}
}
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;public class Test {public static void main(String[] args) {ArrayList strings = new ArrayList<>();strings.add("a");strings.add("a");strings.add("b");strings.add("b");strings.add("c");System.out.println("a:" +listTest(strings,"a"));System.out.println("b:" +listTest(strings,"b"));System.out.println("c:" +listTest(strings,"c"));}public static int listTest(ArrayList strings, String c) {//形参列表 形参类型 形参名,形参类型,形参名//ArrayList代表一个全是String类型的列表类型int count = 0;for (String s : strings) {if(s.equals(c)) {count++;}}return count;}
}
import java.util.*;public class Test {public static void main(String[] args) {Collection strings = new ArrayList<>();strings.add("a");strings.add("a");strings.add("b");strings.add("c");System.out.println(strings);Iterator it = strings.iterator();//创建集合迭代器Set set = new HashSet<>();//创建一个set集合的迭代器set.add(10);//添加元素set.add(20);Iterator it1 = set.iterator();while(it.hasNext()){//判断是否有下一个元素System.out.println(it.next());//输出strings集合的元素System.out.println(it1.next());}}
}
这样就很方便,不需要把所传参数和函数接收参数的类型对应,将参数传到函数后可做出相应操作。
import java.util.*;public class Test {public static void main(String[] args) {int a = 10;int b = 20;printMessage(a,b);int[] arr = {1,2,3};printMessage(arr);}private static void printMessage(int... num) {for(int t:num)System.out.println(t);}}