集合的遍历和去除重复:
import java.util.Enumeration;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Vector;
public class Test01 {
public static void main(String[] args) {
Vector<Employee> vector = new Vector<Employee>();
vector.add(new Employee("李四", 26, "办事员"));
vector.add(new Employee("王五", 23, "经理"));
vector.add(new Employee("王五", 23, "经理"));
vector.add(new Employee("赵六", 36, "秘书"));
System.out.println("-----------方式一:去除重复元素-------------");
Vector<Employee> newList = new Vector<Employee>();
for (Object object : vector) {
Employee employee = (Employee) object;
if (!newList.contains(employee)) {
newList.add(employee);
}
}
for (Object object : newList) {
System.out.println(object);
}
System.out.println("-----------方式二:去除重复元素-------------");
for (int i = 0; i < vector.size() - 1; i++) {
for (int j = i + 1; j < vector.size(); j++) {
if (vector.get(i).equals(vector.get(j))) {
vector.remove(j);
j--;
}
}
}
for (Object object : vector) {
System.out.println(object);
}
}
}
class Employee {
private String name;
private int age;
private String position;
public Employee() {
super();
}
public Employee(String name, int age, String position) {
super();
this.name = name;
this.age = age;
this.position = position;
}
@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 + ((position == null) ? 0 : position.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;
Employee other = (Employee) 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 (position == null) {
if (other.position != null)
return false;
} else if (!position.equals(other.position))
return false;
return true;
}
@Override
public String toString() {
return "Employee [name=" + name + ", age=" + age + ", position=" + position + "]";
}
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 getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
}
随机数的生成:
import java.util.HashSet;
public class Task07 {
public static void main(String[] args) {
HashSet<Integer> list = new HashSet<Integer>();
while (list.size() < 10) {
list.add(getRandomNumber(1, 20));
}
for (Integer it : list) {
System.out.println(it);
}
}
public static int getRandomNumber(int start, int end) {
return (int) (Math.random() * (end - start + 1) + start);
}
}
集合的排序:
import java.text.Collator;
import java.util.Comparator;
import java.util.Locale;
import java.util.Scanner;
import java.util.TreeSet;
public class Task09 {
public static void main(String[] args) {
TreeSet<Student> list = new TreeSet<Student>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
Collator c = Collator.getInstance(Locale.CHINESE);
double num1 = s1.getTotalScore() - s2.getTotalScore();
double num2 = (num1 == 0) ? s1.getChineseAch() - s2.getChineseAch() : num1;
double num3 = (num2 == 0) ? s1.getMathematicsAch() - s2.getMathematicsAch() : num2;
double num4 = (num3 == 0) ? s1.getEnglishAch() - s2.getEnglishAch() : num3;
double num5 = (num4 == 0) ? s1.getAge() - s2.getAge() : num4;
double num6 = (num5 == 0) ? c.compare(s1.getName(), s2.getName()) : num5;
return num6 < 0 ? -1 : ((num6 == 0) ? 0 : 1);
}
});
for (int i = 1; i <= 5; i++) {
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
System.out.print("请输入第" + i + "个学生的姓名:");
String name = input.next();
System.out.print("请输入第" + i + "个学生的年龄:");
Integer age = input.nextInt();
System.out.print("请输入第" + i + "个学生的语文成绩:");
Double ChineseAch = input.nextDouble();
System.out.print("请输入第" + i + "个学生的数学成绩:");
Double MathematicsAch = input.nextDouble();
System.out.print("请输入第" + i + "个学生的英语成绩:");
Double EnglishAch = input.nextDouble();
list.add(new Student(name, age, ChineseAch, MathematicsAch, EnglishAch));
}
for (Student student : list) {
System.out.println(student);
}
}
}
使用LinkedList或者ArrayDeque模拟栈结构和队列结构的集合:
```Java
import java.util.AbstractCollection;
import java.util.EmptyStackException;
import java.util.Iterator;
import java.util.LinkedList;
public class Practicec02 {
public static void main(String[] args) {
MyStack<String> stack = new MyStack<>();
stack.push("abc");
stack.push("efg");
stack.push("hij");
for (String s : stack) {
System.out.println(s);
}
System.out.println("--------------------------");
MyQueue<String> queue = new MyQueue<String>();
queue.offer("abc1");
queue.offer("abc2");
queue.offer("abc3");
while (!queue.isEmpty()) {
System.out.println(queue.poll());
}
}
}
class MyQueue<E> extends AbstractCollection<E> {
private LinkedList<E> list;
public MyQueue() {
list = new LinkedList<>();
}
public void offer(E e) {
list.addLast(e);
}
public E poll() {
return list.removeFirst();
}
public E peek() {
return list.peekFirst();
}
public boolean isEmpty() {
return list.isEmpty();
}
@Override
public Iterator<E> iterator() {
return list.iterator();
}
@Override
public int size() {
return list.size();
}
}
class MyStack<E> extends AbstractCollection<E> {
private LinkedList<E> list;
public MyStack() {
list = new LinkedList<>();
}
public void push(E e) {
list.addFirst(e);
}
public E pop() {
try {
return list.removeFirst();
} catch (Exception e) {
throw new EmptyStackException();
}
}
public E peek() {
return list.peekFirst();
}
public boolean isEmpty() {
return list.isEmpty();
}
@Override
public Iterator<E> iterator() {
return list.iterator();
}
@Override
public int size() {
return list.size();
}
}