java基础学习日志16

集合

List

public class Student implements Comparable<Student> {
	String name;
	int age;
	
	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 Student(String name,int age) {
		this.name= name;
		this.age=age;
	}

	@Override
	public int compareTo(Student o) {
		System.out.println("hello compareTo");
		return o.age-this.age;
	}
	
}


import java.util.Comparator;

public class StudentComparator implements Comparator<Student>{

	@Override
	public int compare(Student o1, Student o2) {
		
		return o1.name.compareTo(o2.name);
	}

}




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

public class ListDem1 {
	public static void main(String[] args) {
		String person1 = "1zhangsan";
		String person2 = "2li4";
		String person3 = "3wang5";
		String person4 = "4zhao6";

		List<String> list = new ArrayList<>();
		list.add(person1);
		list.add(person2);
		list.add(person3);
		list.add(person4);
		// Collections.sort(list);
		// for(int i=list.size()-1;i>=0;i--){
		// if(list.get(i).equals("zhangsan")){
		// list.remove(i);
		// }
		// System.out.print(list.get(i)+"\t");
		// }
		// list = new CopyOnWriteArrayList<>(list);
		// int i=0;
		// for(String str:list){
		// if(str.equals("zhangsan")){
		// list.remove(str);
		// }
		// }
		// System.out.print(list.toString());

		Student stu1 = new Student("a孙勇", 18);
		Student stu2 = new Student("d李凯伦", 12);
		Student stu3 = new Student("f李南", 9);
		Student stu4 = new Student("d杨东旭", 3);
		Student stu5 = new Student("h丁浩天", 3);
		Student stu6 = new Student("e冯苏州", 12);
		Student stu7 = new Student("q任峻成", 17);
		Student stu8 = new Student("h陈杰", 19);
		Student stu9 = new Student("k梁跃博", 16);
		Student stu10 = new Student("z李炜业", 16);
		List<Student> stulist = new ArrayList<Student>();
		stulist.add(stu1);
		stulist.add(stu2);
		stulist.add(stu3);
		stulist.add(stu4);
		stulist.add(stu5);
		stulist.add(stu6);
		stulist.add(stu7);
		stulist.add(stu8);
		stulist.add(stu9);
		stulist.add(stu10);
		Collections.sort(stulist);

		for (int i = 0; i < stulist.size(); i++) {
			System.out.println(stulist.get(i).name + "\t" + stulist.get(i).getAge());
		}
		Collections.sort(stulist, new StudentComparator());
		System.out.println("--------------------------------------------");
		for (int i = 0; i < stulist.size(); i++) {
			System.out.println(stulist.get(i).name + "\t" + stulist.get(i).getAge());
		}

		Collections.sort(stulist, new StudentComparator() {
			public int compare(Student o1, Student o2) {
				return o1.getName().length() - o2.getName().length();
			}
		});
		System.out.println("--------------------------------------------");
		for (int i = 0; i < stulist.size(); i++) {
			System.out.println(stulist.get(i).name + "\t" + stulist.get(i).getAge());
		}
	}
}


Set



import java.util.Comparator;

public class Student implements Comparable<Student>,Comparator<Student>{
	private String name;
	private int age;
	
	public Student(String name ,int age) {
		this.name = name;
		this.age = age;
		
	}
	
	public String getName() {
		return name;
	}
	public void setName(String nam) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	//比较年龄大小,重写
	@Override
	public int compareTo(Student o) {
		return this.age-o.age;
	}
	//比较字符第一个字母排序,重写
	@Override
	public int compare(Student o1, Student o2) {
		return o1.name.compareTo(o2.name);
	}

	//set中要去重,重写
	@Override
	public int hashCode() {
		System.out.println("这还是equals方法");
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + ((name == null) ? 0 : name.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;
		Student other = (Student) obj;
		if (age != other.age)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	
	
	
	
	
}



import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class SetDemo {
	public static void main(String[] args) {
		// Set<String> set = new HashSet<>();
		 String str = "tom";
		 String str1 = "tom1";
		 String str2 = "tom2";
		 String str3 = "tom";
		// set.add(str);
		// set.add(str1);
		// set.add(str2);
		// set.add(str3);
		// Iterator<String> it = set.iterator();
		// while (it.hasNext()) {
		// System.out.println(it.next());
		// }
		
		//想要去重,在Student里重写equals
		Set<Student> stuSet = new HashSet<>();
		Student stu = new Student("tom1", 12);
		Student stu1 = new Student("tom2", 12);
		Student stu2 = new Student("tom", 12);
		Student stu3 = new Student("tom", 12);
		stuSet.add(stu);
		stuSet.add(stu1);
		stuSet.add(stu2);
		stuSet.add(stu3);
		Iterator<Student> stuit = stuSet.iterator();
		while (stuit.hasNext()) {
			System.out.println(stuit.next().getName());
		}

	}

}



import java.util.Iterator;
import java.util.TreeSet;

public class TreeSetDemo {
	public static void main(String[] args) {
		TreeSet<Person> pset = new TreeSet<Person>();
		Person p1 = new Person("tom",12);
		Person p2 = new Person("jerry",14);
		Person p3 = new Person("jack",19);
		Person p4 = new Person("rose",21);
		Person p5 = new Person("rose",21);
		pset.add(p1);
		pset.add(p2);
		pset.add(p3);
		pset.add(p4);
		pset.add(p5);
		Iterator<Person> pit = pset.iterator();
		while(pit.hasNext()){
			Person p = pit.next();
			System.out.println(p.getName()+"\t"+p.getAge());
		}
	}
}

Map



public class Person implements Comparable<Person> {
	private String name;
	private int age;

	public Person(String name, int age) {
		this.name = name;
		this.age = age;
	}

	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;
	}

	@Override
//	public int compareTo(Person o) {
//		// TODO Auto-generated method stub
//		return this.age - o.age;
//	}
	//光比较年龄大小有局限性,重写方法,增加其他变量的比较,用conpareTo
	public int compareTo(Person person) {
		if (this.age != person.age) {
			return this.age - person.age;
		} else {
			return this.name.compareTo(person.name);
		}
	}
  没调用,不需要重写
//	@Override
//	public int hashCode() {
//		System.out.println("这是equals方法");
//		final int prime = 31;
//		int result = 1;
//		result = prime * result + age;
//		result = prime * result + ((name == null) ? 0 : name.hashCode());
//		return result;
//	}
//
//	@Override
//	public boolean equals(Object obj) {
//		System.out.println("hello this is equals");
//		if (this == obj)
//			return true;
//		if (obj == null)
//			return false;
//		if (getClass() != obj.getClass())
//			return false;
//		Person other = (Person) obj;
//		if (age != other.age)
//			return false;
//		if (name == null) {
//			if (other.name != null)
//				return false;
//		} else if (!name.equals(other.name))
//			return false;
//		return true;
//	}

}



import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class MapDemo {

	public static void main(String[] args) {
		Map<Integer, Person> person = new HashMap<>();
		Person per1 = new Person("自行车", 98);
		Person per2 = new Person("九八", 97);
		Person per3 = new Person("阿拉斯加", 876);
		Person per4 = new Person("柴犬", 9);

		person.put(1, per1);
		person.put(2, per2);
		person.put(4, per3);
		person.put(3, per4);
		//通过值遍历
//		Collection<Person> a = person.values();
//		for (Person per : a) {
//			Person p = per;
//			System.out.println(p.getName()+"\t"+p.getAge());
//		}
		//通过键值遍历
//		Set<Integer> set = person.keySet();
//		for (Integer per:set ) {
//			System.out.println(per+":"+person.get(per).getAge());
//		}
		//通过entry遍历
		for(Entry<Integer, Person> en : person.entrySet()){
			System.out.println(en.getKey()+":"+en.getValue().getName()+"\t"+en.getValue().getAge());
		}
		
		
	}
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值