java Collection
编程技术  /  houtizong 发布于 3年前   57
import java.util.*;class HashSetDemo {public static void main(String[] args) {HashSet hs = new HashSet();hs.add(new Student("yuan01",17));hs.add(new Student("yuan01",17));hs.add(new Student("yuan02",21));hs.add(new Student("yuan03",23));hs.add(new Student("yuan04",32));for(Iterator it = hs.iterator(); it.hasNext();) {Student st = (Student)it.next();sop(st.getName() + "..." + st.getAge());}}public static void sop(Object obj) {System.out.println(obj);}}class 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 int getAge() {return age;}public boolean equals(Object obj) {if(!(obj instanceof Student)) {return false;}Student st = (Student)obj;System.out.println(this.name + "..equals.." + st.name);return this.age == st.age && this.name.equals(st.name);}public int hashCode() {//System.out.println(this.name + "hashCode()");return 100; //return name.hasCode + age * 20;}}
import java.util.*;class TreeSetDemo1 {public static void main(String[] args) {TreeSet ts = new TreeSet();ts.add(new Person("yuan01", 20));ts.add(new Person("yuan02", 17));ts.add(new Person("yuan03", 19));ts.add(new Person("yuan04", 20));for(Iterator it = ts.iterator(); it.hasNext();) {Person st = (Person)it.next();System.out.println(st.getName() + "..." + st.getAge());}}}class Person implements Comparable{private String name;private int age;public Person(String name, int age) {this.name = name;this.age = age;}public String getName() {return this.name;}public int getAge() {return this.age;}//重写compareTo()方法public int compareTo(Object obj) {Person ps = (Person)obj;if(this.age > ps.age) return 1;if(this.age == ps.age) {return this.name.compareTo(ps.name);}return -1;}}
import java.util.*;class TreeSetDemo {public static void main(String[] args) {TreeSet ts = new TreeSet(new myCompare());ts.add("asdf");ts.add("afggf");ts.add("aasdfasdf");ts.add("asd");ts.add("acd");for(Iterator it = ts.iterator(); it.hasNext();) {String str = (String)it.next();System.out.println(str);}}}class myCompare implements Comparator {public int compare(Object o1, Object o2) {String s1 = (String)o1;String s2 = (String)o2;//System.out.println(s1 + "..compare.." + s2);int l1 = s1.length();int l2 = s2.length();if(l1 > l2)return 1;if(l1 == l2)return s1.compareTo(s2);//当长度一样的时候,还要比较内容return -1;}}
import java.util.*;class GenericDemo {public static void main(String[] args) {ArrayList<String> al = new ArrayList<String>();al.add("yuan01");al.add("yuan02");al.add("yuan03");al.add("yuan04");for(Iterator<String> it = al.iterator(); it.hasNext();) {String str = it.next();System.out.println(str);}}}
import java.util.*;class GenericDemo2 {public static void main(String[] args) {TreeSet<String> ts = new TreeSet<String>(new StrComparator());ts.add("java0");ts.add("java02");ts.add("java");for(Iterator<String> it = ts.iterator(); it.hasNext();) {String str = it.next();System.out.println(str);}}}class StrComparator implements Comparator<String>{public int compare(String s1 , String s2) {int num = ((Integer)s2.length()).compareTo((Integer)s1.length());if(num == 0) return s2.compareTo(s1);return num;}}
class GenericDemo3 {public static void main(String[] args) {Tool<String> tool = new Tool<String>();tool.setObject("loveyuan");System.out.println(tool.getObject());}}class Tool<T>{private T t;public void setObject(T t) {this.t = t;}public T getObject() {return t;}}
class GenericDemo4 {public static void main(String[] args) {Demo<String> d = new Demo<String>();d.show("love");d.print(new Integer(1));d.print("yuan");}}class Demo<T>{public void show(T t) {System.out.println("show" + t);}public <Q> void print(Q q) {System.out.println("print" + q);}}
import java.util.*;class GenericDemo6 {public static void main(String[] args) {ArrayList<String> al = new ArrayList<String>();al.add("java1");al.add("java2");al.add("java3");ArrayList<Integer> al1 = new ArrayList<Integer>();al1.add(1);al1.add(2);al1.add(3);print(al);}public static void print(ArrayList<?> al) {for(Iterator<?> it = al.iterator(); it.hasNext();) {System.out.println(it.next());}}}
import java.util.*;class People{private String name;public People(String name) {this.name = name;}public String getName() {return name;}}class Teacher extends People{public Teacher(String name) {super(name);}}class GenericDemo7 {public static void main(String[] args) {ArrayList<Teacher> al = new ArrayList<Teacher>();al.add(new Teacher("java1"));al.add(new Teacher("java2"));al.add(new Teacher("java3"));printAll(al);}public static void printAll(ArrayList<People> al) {for(Iterator<People> it = al.iterator(); it.hasNext();) {System.out.println(it.next().getName());}}}
public static void printAll(ArrayList<? extends People> al) {for(Iterator<? extends People> it = al.iterator(); it.hasNext();) {System.out.println(it.next().getName());}}
class GenericDemo7 {public static void main(String[] args) {ArrayList<People> al = new ArrayList<People>();al.add(new People("java--1"));al.add(new People("java--2"));al.add(new People("java--3"));printAll(al);}public static void printAll(ArrayList<? super Teacher> al) {for(Iterator<? super Teacher> it = al.iterator(); it.hasNext();) {System.out.println(it.next());}}}
import java.util.*;class MapDemo1 {public static void main(String[] args) {Map<String , String> map = new HashMap<String, String>();map.put("01","java01");map.put("02","java02");map.put("03","java03");System.out.println(map.values());}}
import java.util.*;class MapDemo2 {public static void main(String[] args) {Map<String ,String> map = new HashMap<String , String>();map.put("01","java1");map.put("02","java2");map.put("03","java3");Set<String> set = map.keySet();for(Iterator<String> it = set.iterator();it.hasNext();) {String key = it.next();String value = map.get(key);System.out.println(key + "=" + value);}}}
import java.util.*;class MapDemo3 {public static void main(String[] args) {Map<String , String> map = new HashMap<String , String>();map.put("01","java1");map.put("02","java2");map.put("03","java3");Set<Map.Entry<String,String>> set = map.entrySet();for(Iterator<Map.Entry<String,String>> it = set.iterator();it.hasNext();) {Map.Entry<String,String> me = it.next();String key = me.getKey();String value = me.getValue();System.out.println(key + "=" + value);}}}
import java.util.*;class MapDemo4 {public static void main(String[] args) {TreeMap<Student, String> map = new TreeMap<Student, String>();map.put(new Student("java17",20),"beijing");map.put(new Student("java13",20),"shanghai");map.put(new Student("java16",20),"wuhan");map.put(new Student("java12",20),"nanjing");Set<Map.Entry<Student,String>> set = map.entrySet();for(Iterator<Map.Entry<Student,String>> it = set.iterator();it.hasNext();) {Map.Entry<Student,String> me = it.next();Student st = me.getKey();String addr = me.getValue();System.out.println(st + "=" + addr);}}}class Student implements Comparable<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 int getAge() {return age;}public int hashCode() {return name.hashCode() + age * 23;}public boolean equals(Object obj) {if(!(obj instanceof Student)) throw new ClassCastException("类型不匹配!");Student st = (Student)obj;return this.name.equals(st.name) && this.age == st.age;}public int compareTo(Student st) {int num = ((Integer)this.age).compareTo((Integer)st.age);if(num == 0)return this.name.compareTo(st.name);return num;}public String toString() {return this.name + ":" + this.age;}}
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成
网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];
文章归档
文章标签
友情链接