泛型和集合

目录

1. 泛型

2. 集合

1. 泛型

// 定义泛型类
class Test<T> {
	private T ob;

	public Test(T ob) {
		this.ob = ob;
	}

	public T getOb() {
		return ob;
	}

	public void setOb(T ob) {
		this.ob = ob;
	}

	public void showType() {
		System.out.println("T的实际类型是:" + ob.getClass().getName());
	}
}

public class TestDemo {
	public static void main(String[] args) {
		Test<Integer> intOb = new Test<Integer>(88);
		intOb.showType();
		int i = intOb.getOb();
		System.out.println("value = " + i);
		System.out.println("---------------------------");
		Test<String> strOb = new Test<String>("Hello World");
		strOb.showType();
		String str = strOb.getOb();
		System.out.println("value = " + str);
	}
}
// 简单使用泛型方法
package com.company;

class Animal {
	public Animal() {
		System.out.println("我是动物");
	}
}

class Dog extends Animal {
	public Dog() {
		System.out.println("我是狗");
	}
}

public class Main {
	public <T, S extends T> T testDemo(T t, S s) {
		System.out.println("我是 T 类型,我的类型是" + t.getClass().getName());
		System.out.println("我是 S 类型,我的类型是" + s.getClass().getName());
		return t;
	}

	public static void main(String[] args) {
		Main test = new Main();
		Dog dog = new Dog();
		Animal animal = new Animal();
		Animal newAnimal = test.testDemo(animal, dog);
		System.out.println("我是对象 newAnimal,我的类型是" + animal.getClass().getName());
	}
}
// 通过通配符定义泛型方法
package com.company;

import java.util.ArrayList;
import java.util.List;

class Animal {
	public Animal() {
		System.out.println("我是动物");
	}
}

class Dog extends Animal {
	public Dog() {
		System.out.println("我是狗");
	}
}

public class Main {
	public void testDemo(List<?> list) {
		for (Object obj : list) {
			System.out.println("我的类型是" + obj.getClass().getName());
		}
	}

	public static void main(String[] args) {
		Main test = new Main();
		Dog dog = new Dog();
		Animal animal = new Animal();

		List<Animal> list = new ArrayList<Animal>();
		list.add(dog);
		list.add(animal);
		test.testDemo(list);
	}
}

2. 集合

// ArrayList 测试
package com.company;

import java.util.*;

class Student {
	public String id;
	public String name;
	public Student(String id, String name) {
		this.id = id;
		this.name = name;
	}

	public String toString() {
		return "Student{" + "id=" + id +'\'' +
				", name=" + name + '\'' + '}';
	}
}

public class Main {
	public List<Student> students;

	public Main() {
		this.students = new ArrayList<Student>();
	}

	public void testAdd() {
		Student st1 = new Student("1", "张三");
		students.add(st1);

		Student temp = students.get(0);
		System.out.println("添加了学生:" + temp.id + ":" + temp.name);

		Student st2 = new Student("2", "李四");
		students.add(0, st2);

		Student temp2 = students.get(0);
		System.out.println("添加了学生:" + temp2.id + ":" + temp2.name);

		Student[] student = {new Student("3", "王五"),
							new Student("4", "马六")};
		students.addAll(Arrays.asList(student));
		Student temp3 = students.get(2);
		Student temp4 = students.get(3);
		System.out.println("添加了学生:" + temp3.id + ":" + temp3.name);
		System.out.println("添加了学生:" + temp4.id + ":" + temp4.name);

		Student[] students2 = {new Student("5", "周七"),
								new Student("6", "赵八")};

		students.addAll(2, Arrays.asList(students2));

		Student temp5 = students.get(2);
		Student temp6 = students.get(3);
		System.out.println("添加了学生:" + temp5.id + ":" + temp5.name);
		System.out.println("添加了学生:" + temp6.id + ":" + temp6.name);
	}

	public void testGet() {
		int size = students.size();
		for (int i = 0; i < size; i++) {
			Student st = students.get(i);
			System.out.println("学生:" + st.id + ":" + st.name);
		}
	}

	public void testIterator() {
		Iterator<Student> iter = students.iterator();
		System.out.println("有如下学生(通过迭代器访问):");
		while (iter.hasNext()) {
			Student st = iter.next();
			System.out.println("学生:" + st.id + ":" + st.name);
		}
	}

	public void testForEach() {
		System.out.println("有如下学生(通过foreach访问):");
		for (Student obj : students) {
			Student st = obj;
			System.out.println("学生:" + st.id + ":" + st.name);
		}
		students.stream().sorted(Comparator.comparing(x -> x.id)).forEach(System.out::println);
	}

	public void testModify() {
		students.set(4, new Student("3", "吴酒"));
	}

	public void testRemove() {
		Student st = students.get(4);
		System.out.println("我是学生:" + st.id + ":" + st.name + ",我即将被删除");
		students.remove(st);
		System.out.println("成功删除学生");
		testForEach();
	}

	public static void main(String[] args) {
		Main test = new Main();
		test.testAdd();
		test.testGet();
		test.testIterator();
		test.testModify();
		test.testForEach();
		test.testRemove();
	}
}
// Map 测试
package com.company;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;

class Course {
	public String id;
	public String name;
	public Course(String id, String name) {
		this.id = id;
		this.name = name;
	}
}

public class Main {
	public Map<String, Course> courses;

	public Main() {
		this.courses = new HashMap<String, Course>();
	}

	public void testPut() {
		Scanner console = new Scanner(System.in);

		for (int i = 0; i < 3; i++) {
			System.out.println("请输入课程 ID:");
			String ID = console.next();

			Course course = courses.get(ID);
			if (course == null) {
				System.out.println("请输入课程名称: ");
				String name = console.next();
				Course newCourse = new Course(ID, name);
				courses.put(ID, newCourse);
				System.out.println("成功添加课程," + courses.get(ID).name);
			}
			else {
				System.out.println("该课程 ID 已被占用");
			}
		}
	}

	public void testKeySet() {
		Set<String> keySet = courses.keySet();
		for (String courseID : keySet) {
			Course course = courses.get(courseID);
			if (course != null) {
				System.out.println("课程:" + course.name);
			}
		}
	}

	public void testRemove() {
		Scanner console = new Scanner(System.in);
		while (true) {
			System.out.println("请输入要删除的课程ID:");
			String ID = console.next();
			Course course = courses.get(ID);
			if (course == null) {
				System.out.println("该 ID 不存在!");
				continue;
			}
			courses.remove(ID);
			System.out.println("成功删除课程: " + course.name);
			break;
		}
	}

	public void testEntrySet() {
		Set<Entry<String, Course>> entrySet = courses.entrySet();
		for (Entry<String, Course> entry : entrySet) {
			System.out.println("取得键:" + entry.getKey());
			System.out.println("对应的值为:" + entry.getValue().name);
		}
	}

	public void testModify() {
		System.out.println("请输入要修改的课程ID:");
		Scanner console = new Scanner(System.in);
		while (true) {
			String ID = console.next();
			Course course = courses.get(ID);
			if (course == null) {
				System.out.println("该 ID 不存在!");
				continue;
			}
			System.out.println("当前该课程ID所对应的课程为:" + course.name);
			System.out.println("请输入新的课程名称:");
			String name = console.next();
			Course newCourse = new Course(ID, name);
			courses.put(ID, newCourse);
			System.out.println("修改成功!");
			break;
		}
	}

	public static void main(String[] args) {
		Main test = new Main();
		test.testPut();
		test.testKeySet();
		test.testRemove();
		test.testModify();
		test.testEntrySet();
	}
}
// Set 测试
package com.company;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.List;
import java.util.Scanner;
import java.util.Arrays;

class Student {
	public String id;
	public String name;
	public Student(String id, String name) {
		this.id = id;
		this.name = name;
	}

	public String toString() {
		return "Student{" + "id=" + id + '\'' +
				", name=" + name + '\'' + '}';
	}
}

class PD {
	public String id;
	public String name;
	public Set<Student> students;
	public PD(String id, String name) {
		this.id = id;
		this.name = name;
		this.students = new HashSet<Student>();
	}
}

public class Main {
	public List<Student> students;

	public Main() {
		students = new ArrayList<Student>();
	}

	public void testAdd() {
		Student st1 = new Student("1", "张三");
		students.add(st1);

		Student st2 = new Student("2", "李四");
		students.add(st2);

		Student[] student = {new Student("3", "王五"),
							new Student("4", "马六")};
		students.addAll(Arrays.asList(student));

		Student[] student2 = {new Student("5", "周七"),
							new Student("6", "赵八")};
		students.addAll(Arrays.asList(student2));
	}

	public void testForEach() {
		System.out.println("有如下学生(通过foreach):");
		for (Object obj : students) {
			Student st = (Student)obj;
			System.out.println(st.toString());
		}
	}

	public void testForEachForSet(PD pd) {
		for (Student s : pd.students) {
			System.out.println("选择了学生:" + s.id + ":" + s.name);
		}
	}

	public static void main(String[] args) {
		Main st = new Main();
		st.testAdd();
		st.testForEach();
		PD pd = new PD("1", "张老师");
		System.out.println("请" + pd.name + "选择小组成员!");

		Scanner console = new Scanner(System.in);

		for (int i = 0; i < 3; i++) {
			System.out.println("请输入学生 ID");
			String studentID = console.next();
			for (Student s : st.students) {
				if (s.id.equals(studentID)) {
					pd.students.add(s);
				}
			}
		}
		st.testForEachForSet(pd);
		console.close();
	}
}
// Collections 测试
package com.company;

import java.util.ArrayList;
import java.util.List;
import java.util.Collections;

public class Main {
	public static void printList(List<Integer> list, String str) {
		System.out.print(str + ":");
		list.forEach(v ->System.out.print(v + "\t"));
		System.out.println();
	}

	public static void main(String[] args) {
		List<Integer> list = new ArrayList<Integer>();

		list.add(3);
		list.add(5);
		list.add(7);
		list.add(9);
		list.add(12);
		printList(list, "初始顺序");

		Collections.shuffle(list);
		printList(list, "打乱顺序");

		Collections.swap(list, 0, list.size() - 1);
		printList(list, "交换第一位和最后一位后");

		Collections.sort(list);
		printList(list, "Sort排序后");

		System.out.println("二分查找数值7的位置为:" + (1 + Collections.binarySearch(list, 7)));

		List<Integer> synchronizedList = Collections.synchronizedList(list);
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值