标签:
------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
1 练习 2 package blogtest2; 3 import java.util.*; 4 public class ArrayListTest { 5 6 /** 7 * 将自定义对象传入到ArrayList集合中,并去除重复元素 8 */ 9 public static void main(String[] args) { 10 ArrayList<People> arraylist = new ArrayList<People>(); 11 arraylist.add(new People("zhangshan",22)); 12 arraylist.add(new People("lishan",24)); 13 arraylist.add(new People("wangwu",15)); 14 arraylist.add(new People("zhangshan",22)); 15 ArrayList<People> singlist = singleName(arraylist); 16 for(People stu : singlist){ 17 System.out.println(stu); 18 } 19 } 20 21 22 public static ArrayList<People> singleName(ArrayList<People> arraylist){ 23 ArrayList<People> list = new ArrayList<People>(); 24 Iterator<People> ite = arraylist.iterator(); 25 while(ite.hasNext()){ 26 People stu = ite.next(); 27 if(!list.contains(stu)){ 28 list.add(stu); 29 } 30 } 31 return list; 32 } 33 } 34 class People{ 35 private String name; 36 private int age; 37 People(String name,int age){ 38 this.name = name; 39 this.age = age; 40 } 41 public String getName(){ 42 return name; 43 } 44 public int getAge(){ 45 return age; 46 } 47 public String toString(){ 48 return name +"...."+age; 49 } 50 public boolean equals(Object stu){ 51 if(!(stu instanceof People)){ 52 return false; 53 } 54 People stucopy = (People)stu; 55 return this.name.equals(stucopy.name) && this.age == stucopy.age; 56 } 57 }
1 练习 2 package blogtest2; 3 import java.util.*; 4 public class LinkedListTest { 5 6 /** 7 * 用LinkedList模拟堆栈或者队列结构 8 */ 9 public static void main(String[] args) { 10 LinkedList<String> linkedlist = new LinkedList<String>(); 11 linkedlist.addFirst("java1"); 12 linkedlist.addFirst("java2"); 13 linkedlist.addFirst("java3"); 14 linkedlist.addFirst("java4"); 15 System.out.println(linkedlist); 16 17 LinkedList<String> linkedlist2 = new LinkedList<String>(); 18 linkedlist2.addLast("java1"); 19 linkedlist2.addLast("java2"); 20 linkedlist2.addLast("java3"); 21 linkedlist2.addLast("java4"); 22 System.out.println(linkedlist2); 23 24 25 } 26 27 }
1 练习 2 package blogtest2; 3 import java.util.HashSet; 4 public class HashSetTest { 5 6 /** 7 * 将自定义对象存入HashSet中,同姓名和年龄为重复元素 8 */ 9 public static void main(String[] args) { 10 HashSet<Student> hashset = new HashSet<Student>(); 11 hashset.add(new Student("zhangshan",22)); 12 hashset.add(new Student("lishan",24)); 13 hashset.add(new Student("wangwu",15)); 14 hashset.add(new Student("zhangshan",22)); 15 for(Student stu : hashset){ 16 System.out.println(stu); 17 } 18 19 } 20 21 } 22 class Student implements Comparable<Student>{ 23 private String name; 24 private int age; 25 Student(String name,int age){ 26 this.name = name; 27 this.age = age; 28 } 29 public String getName(){ 30 return name; 31 } 32 public int getAge(){ 33 return age; 34 } 35 public String toString(){ 36 return name +"...."+age; 37 } 38 public int hashCode(){ 39 return this.name.hashCode() + this.age * 30; 40 } 41 public boolean equals(Object stu){ 42 if(!(stu instanceof Student)){ 43 return false; 44 } 45 Student stucopy = (Student)stu; 46 return this.name.equals(stucopy.name) && this.age == stucopy.age; 47 } 48 public int compareTo(Student stu){ 49 int num = new Integer(this.age).compareTo(new Integer(stu.age )); 50 if(num == 0){ 51 return this.name.compareTo(stu.name); 52 } 53 return num; 54 } 55 }
1 练习 2 package blogtest2; 3 import java.util.*; 4 public class TreeSetTest { 5 6 /** 7 * 往TreeSet中存入自定义对象学生,按学生的年龄排序 8 */ 9 public static void main(String[] args) { 10 // TODO Auto-generated method stub 11 TreeSet<Student> treeset = new TreeSet<Student>(new MyComparator()); 12 treeset.add(new Student("zhangshan",22)); 13 treeset.add(new Student("lishan",24)); 14 treeset.add(new Student("wangwu",15)); 15 treeset.add(new Student("zhangshan",22)); 16 for(Student stu : treeset){ 17 System.out.println(stu); 18 } 19 20 } 21 } 22 class MyComparator implements Comparator<Student>{ 23 public int compare(Student stu1,Student stu2){ 24 int num = stu1.getName().compareTo(stu2.getName()); 25 if(num == 0){ 26 return new Integer(stu1.getAge()).compareTo(new Integer(stu2.getAge())); 27 } 28 return num; 29 } 30 } 31 class Student implements Comparable<Student>{ 32 private String name; 33 private int age; 34 Student(String name,int age){ 35 this.name = name; 36 this.age = age; 37 } 38 public String getName(){ 39 return name; 40 } 41 public int getAge(){ 42 return age; 43 } 44 public String toString(){ 45 return name +"...."+age; 46 } 47 Student stucopy = (Student)stu; 48 return this.name.equals(stucopy.name) && this.age == stucopy.age; 49 } 50 public int compareTo(Student stu){ 51 int num = new Integer(this.age).compareTo(new Integer(stu.age )); 52 if(num == 0){ 53 return this.name.compareTo(stu.name); 54 } 55 return num; 56 } 57 }
1 练习 2 package blogtest2; 3 import java.util.*; 4 public class HashMapTest { 5 /** 6 *演示Map集合两种取出方式 7 */ 8 public static void main(String[] args) { 9 Map<String,String> map = new HashMap<String,String>(); 10 map.put("zhangshan", "22"); 11 map.put("lisi", "33"); 12 map.put("wangwu", "44"); 13 map.put("xiaoming", "55"); 14 //第一种取出方式 15 Set<Map.Entry<String, String>> set = map.entrySet(); 16 Iterator<Map.Entry<String, String>> ite = set.iterator(); 17 while(ite.hasNext()){ 18 Map.Entry<String, String> mapentry = ite.next(); 19 String key = mapentry.getKey(); 20 String value = mapentry.getValue(); 21 System.out.println(key + "=" + value); 22 } 23 //第二种取出方式 24 Set<String> set2 = map.keySet(); 25 Iterator<String> ite2 = set2.iterator(); 26 while(ite2.hasNext()){ 27 String key2 = ite2.next(); 28 String value2 = map.get(key2); 29 System.out.println(key2 + "=" + value2); 30 } 31 } 32 }
标签:
原文地址:http://www.cnblogs.com/yuemingxingxing/p/5078006.html