------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
<span style="font-size:12px;">import java.util.ArrayList; import java.util.Iterator; import java.util.ListIterator; public class ListDemo { public static void main(String[] args) { ArrayList<String> array = new ArrayList<String>(); array.add("1"); array.add("2"); array.add("3"); //第一种方式集合自己的迭代器迭代器,不能增删改查 Iterator<String> it = array.iterator(); //while()方式遍历迭代器 while(it.hasNext()){ String s = it.next(); System.out.print(s+" "); } System.out.println(); //for的方式遍历迭代器 for (Iterator<String> iterator = array.iterator(); iterator.hasNext();) { String s = iterator.next(); System.out.print(s+" "); } System.out.println(); //第二种,用list的迭代器遍历,可以增删改查 ListIterator<String> listit = array.listIterator(); //while()方式 while(listit.hasNext()){ String s = listit.next(); System.out.print(s+" "); } System.out.println(); //for的方式遍历迭代器 for (ListIterator<String> listiterator = array.listIterator(); listiterator.hasNext();) { String s = listiterator.next(); System.out.print(s+" "); } System.out.println(); //第三种方式,增强for for (String s : array) { System.out.print(s+" "); } System.out.println(); } }</span>
import java.util.TreeSet; //实现Comparable接口 class Student implements Comparable<Student> { private String name; private int num; public Student(String name, int num) { super(); this.name = name; this.num = num; } public Student() { super(); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } public int compareTo(Student o) { int num = this.num - o.num; int num2 = num==0 ? this.name.compareTo(o.name) :num; return num2; } public String toString() { return "Student [name=" + name + ", num=" + num + "]"; } } public class TreeSetDemo { public static void main(String[] args) { //自然排序 并且唯一 TreeSet<Student> ts1 = new TreeSet<Student>(); ts1.add(new Student("xiao", 2)); ts1.add(new Student("wang", 1)); ts1.add(new Student("zhao", 3)); for (Student student : ts1) { System.out.println(student); } } }
package setdeom; import java.util.Comparator; import java.util.TreeSet; class Student { private String name; private int num; public Student(String name, int num) { super(); this.name = name; this.num = num; } public Student() { super(); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } } public class TreeSetDemo { public static void main(String[] args) { Student s1 = new Student("wang",18); Student s2 = new Student("zhao",21); Student s3 = new Student("li",11); Student s4 = new Student("xue",22); //比较器实现Comparator接口 TreeSet<Student> ts = new TreeSet<Student>(new MyComparator()); ts.add(s1); ts.add(s2); ts.add(s3); ts.add(s4); for (Student student : ts) { System.out.println(student.getName()+" "+student.getNum()); } } } class MyComparator<Student> implements Comparator<Student>{ @Override public int compare(Student s1, Student s2) { int num = ((setdeom.Student) s1).getNum() - ((setdeom.Student) s2).getNum(); int num2 = num==0 ? ((setdeom.Student) s1).getName().compareTo(((setdeom.Student) s2).getName()) :num; return num2; } }
<span style="font-size:12px;">import java.util.HashSet; class Student{ private String name; private String num; public Student(String name, String num) { super(); this.name = name; this.num = num; } public Student() { super(); // TODO Auto-generated constructor stub } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNum() { return num; } public void setNum(String num) { this.num = num; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + ((num == null) ? 0 : num.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 (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (num == null) { if (other.num != null) return false; } else if (!num.equals(other.num)) return false; return true; } } public class HashSetDemo { public static void main(String[] args) { Student s1 = new Student("qxc","222"); Student s2 = new Student("wl","111"); Student s3 = new Student("ly","555"); Student s4 = new Student("qxc","222"); HashSet<Student> hs = new HashSet<Student>(); hs.add(s1); hs.add(s2); hs.add(s3); hs.add(s4); for (Student student : hs) { System.out.println(student.getName()+" "+student.getNum()); } } }</span><strong style="font-size:18px;"> </strong>
public class MapDemo { public static void main(String[] args) { Map<String, String> map = new HashMap<String, String>(); // // //添加 // //改键不存在直接存储,存在返回以前的 // System.out.println(map.put("1", "qxc")); // System.out.println(map.put("1", "wl")); // System.out.println(map.put("2", "xixi")); // System.out.println(map.put("3", "haha")); // System.out.println(map.put("4", "wuw")); // // //删除 // System.out.println(map.remove("3")); // System.out.println(map.remove("13")); // // //包含 // System.out.println(map.containsKey("1")); // System.out.println(map.containsKey("11")); // //输出集合名称 // System.out.println("map:"+map); // System.out.println(map.size()); // // System.out.println("-------"); // // System.out.println(map.get("1")); // Set<String> set = map.keySet(); // // for (String string : set) { // System.out.println(string); // } // Collection<String> coll = map.values(); // System.out.println("-------"); // for (String string : coll) { // System.out.println(string); // } //遍历 Map<String,String> m = new HashMap<String, String>(); map.put("杨过", "小龙女"); map.put("郭靖", "黄蓉"); map.put("杨康", "穆念慈"); map.put("陈玄风", "梅超风"); // Set<String> s = map.keySet(); // for (String str : s) { // System.out.println(str+"="+map.get(str)); // } Set<Map.Entry<String, String>> s = map.entrySet(); for (Entry<String, String> entry : s) { System.out.println(entry.getKey()+"="+entry.getValue()); } } }
<span style="font-size:12px;">import java.util.HashMap; import java.util.Map.Entry; import java.util.Set; public class HashMapDemo { public static void main(String[] args) { HashMap<String,String> hm = new HashMap<String, String>(); String key1 = "IT001"; String value1 = "马化腾"; hm.put(key1, value1); hm.put("IT003", "李彦宏"); hm.put("IT004", "张朝阳"); hm.put("IT005", "乔布斯"); Set<Entry<String,String>> set = hm.entrySet(); for (Entry<String, String> entry : set) { System.out.println(entry.getKey()+"="+entry.getValue()); } System.out.println("----------"); Set<String> key = hm.keySet(); for (String string : key) { System.out.println(string+"="+hm.get(string)); } } }</span>