集合方法小结

来源:互联网 发布:linux tomcat日志分割 编辑:程序博客网 时间:2024/06/08 06:10

List

package com.serendipity.day23.collection;/** * Title: Student * Description: Student类  * @author 谢英亮 * @date 2017年12月20日 下午5:22:43 */public class Student /* implements Comparable<Student> */ {public String name;public String stuId;public Student(String name, String stuId) {super();this.name = name;this.stuId = stuId;}public Student() {super();}@Overridepublic String toString() {return "Student [name=" + name + ", stuId=" + stuId + "]";}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getStuId() {return stuId;}public void setStuId(String stuId) {this.stuId = stuId;}@Overridepublic int hashCode() {final int prime = 31;int result = 1;result = prime * result + ((name == null) ? 0 : name.hashCode());result = prime * result + ((stuId == null) ? 0 : stuId.hashCode());return result;}@Overridepublic 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 (name == null) {if (other.name != null)return false;} else if (!name.equals(other.name))return false;if (stuId == null) {if (other.stuId != null)return false;} else if (!stuId.equals(other.stuId))return false;return true;}// @Override// public int compareTo(Student o) {// return this.stuId.compareTo(o.stuId);// }}


package com.serendipity.day23.collection;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import java.util.ListIterator;/** * Title: ListDemo  * Description:List常用方法测试类 * @author 谢英亮 * @date 2017年12月20日 下午5:21:54 */public class ListDemo {public static void main(String[] args) {List<Student> list = new ArrayList<>();// List<Student> list = new LinkedList<>();// 增加list.add(new Student("小明", "201401"));list.add(new Student("小弘", "201402"));list.add(new Student("小张", "201403"));list.add(new Student("小亮", "201404"));Student s = new Student("小王", "201405");list.add(s);System.out.println("list中元素个数" + list.size());System.out.println(list);// 删除list.remove(0);list.remove(s);System.out.println(list);// 修改list.set(0, s);s.setStuId("201401");System.out.println(list);// 查询System.out.println(list.get(1));System.out.println(list.indexOf(s));List<Student> subList = list.subList(0, 2);// 得到一个子集合System.out.println(subList);// 遍历for (Student o : list) {System.out.println(o);}//两种迭代器的区别Iterator<Student> iterator = list.iterator();while (iterator.hasNext()) {System.out.println(iterator.next());}ListIterator<Student> it = list.listIterator();// list.add(new Student("小梅", "201406"));//会报异常! Exception in thread// "main"// java.util.ConcurrentModificationExceptionit.add(new Student("小梅", "201406"));while (it.hasNext()) {Student next = it.next();if (next.getName().equals("小明"))it.remove();// list.remove(next);// 会报异常! Exception in thread "main"// java.util.ConcurrentModificationException}System.out.println(list);}}



Set

package com.serendipity.day23.collection;import java.util.Comparator;/** * Title: MyComparator * Description: 用于TreeSet排序 * @author 谢英亮 * @date 2017年12月20日 下午5:22:29 */public class MyComparator implements Comparator<Student>{@Overridepublic int compare(Student o1,Student o2) {return o1.stuId.compareTo(o2.stuId);}}


package com.serendipity.day23.collection;import java.util.Iterator;import java.util.Set;import java.util.TreeSet;/** * Title: SetDemo * Description: Set常用方法测试类 * @author 谢英亮 * @date 2017年12月20日 下午5:22:35 */public class SetDemo {public static void main(String[] args) {Set<Student> set = new TreeSet<>(new MyComparator());//Set<Student> set = new TreeSet<>(); Student类需要实现comparable接口  并重写compareTo()方法set.add(new Student("小向", "001"));set.add(new Student("小陈", "004"));set.add(new Student("小梅", "005"));set.add(new Student("小邓", "002"));set.add(new Student("小周", "003"));// 重写hashCode和equals方法后 认为属性相同的及为同一对象set.add(new Student("小梅", "005"));System.out.println("set中元素个数:" + set.size());for (Student s : set) {System.out.println(s);}Iterator<Student> it = set.iterator();while (it.hasNext()) {Student next = it.next();// if (next.getName().equals("小梅")) {// set.remove(next);// }}System.out.println("set中元素个数:" + set.size());}}

Map

package com.serendipity.day23.collection;import java.util.HashMap;/** * Title: MapDemo * Description: Map常用方法测试类 * @author 谢英亮 * @date 2017年12月20日 下午5:22:23 */public class MapDemo {public static void main(String[] args) {HashMap<String, Student> map = new HashMap<>();Student s1 = new Student("小明", "001");Student s2 = new Student("小张", "002");Student s3 = new Student("小李", "003");Student s4 = new Student("小王", "004");map.put(s1.getStuId(), s1);map.put(s2.getStuId(), s2);map.put(s3.getStuId(), s3);map.put(s4.getStuId(), s4);System.out.println(map);boolean containsKey = map.containsKey("2222");boolean containsValue = map.containsValue(s3);System.out.println("是否包含键 2222 :"+containsKey);System.out.println("是否包含值 s3 :"+containsValue);System.out.println(map.get("001").getName());}}


原创粉丝点击