import java.util.ArrayList;
import java.util.Iterator;
public class Demo {
public static void main(String[] args) {
ArrayList arrayList = new ArrayList();
arrayList.add("zhangsan");
arrayList.add("lisi");
arrayList.add("wangwu");
arrayList.add("zhaoliu");
Iterator iterator = arrayList.iterator(); //使用迭代器进行遍历
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
- LinkedList 存储对象底层采用链表数据结构,线程不同步的集合,查询慢,增删快
import java.util.LinkedList;
import java.util.Iterator;
public class Demo {
public static void main(String[] args) {
LinkedList linkedList = new LinkedList();
linkedList.add("zhangsan");
linkedList.add("lisi");
linkedList.add("wangwu");
linkedList.add("zhaoliu");
Iterator iterator = linkedList.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
import java.util.HashSet;
import java.util.Iterator;
public class Demo {
public static void main(String[] args) {
HashSet hashCode = new HashSet();
hashCode.add(new Person("hansan",1));
hashCode.add(new Person("hansan",1));
hashCode.add(new Person("lisi",1));
hashCode.add(new Person("wangwu",3));
hashCode.add(new Person("zhaoliu",2));
Iterator iterator = hashCode.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next().toString());
}
}
}
class 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 int getAge() {
return age;
}
public String toString() {
return name + "---" + age;
}
/**
*去重复,是使用hashCode和equals方法
*如果hasCode相等,则比较再用equals方法进行比较
*/
public int hashCode() {
return name.hashCode() + age;
}
public boolean equals(Object object) {
if(this == object) {
return true;
}
if(object instanceof Person) {
return name.equals(((Person)object).name) && age == ((Person)object).age;
}
return false;
}
}
- TreeSet底层数据结构是二叉树,线程不同步,对于存储到集合中的元素进行自然排序,对对象必须实现comparable接口进行,自然排序,TreeSet也可以自己实现比较器
import java.util.TreeSet;
import java.util.Iterator;
public class Demo {
public static void main(String[] args) {
TreeSet treeCode = new TreeSet();
treeCode.add(new Person("hansan",1));
treeCode.add(new Person("hansan",1));
treeCode.add(new Person("lisi",1));
treeCode.add(new Person("wangwu",3));
treeCode.add(new Person("zhaoliu",2));
Iterator iterator = treeCode.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next().toString());
}
}
}
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 name;
}
public int getAge() {
return age;
}
public String toString() {
return name + "---" + age;
}
//使用Comparable接口中的compareTo方法进行排序
public int compareTo(Person p) {
int num = name.compareTo(p.name);
return num == 0 ? age - p.age : num;
}
}
/**
*HashMap取值的两种方式
*
*/
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class Demo {
public static void main(String[] args) {
HashMap hm = new HashMap();
hm.put("zhangsan",22);
hm.put("lisi",24);
hm.put("wnagwu",23);
hm.put("zhaoliu",22);
//Map集合第一种取元素的方式
//通过Map集合的keySet()将键的集合存到Set集合中
Set set1 = hm.keySet();
Iterator iterator1 = set1.iterator();
while(iterator1.hasNext()) {
String key = iterator1.next();
System.out.println(key + "----" + hm.get(key));
}
//第二种
//通过Map.Entry这个类取出键值关系
Set> set2 = hm.entrySet();
Iterator> iterator2 = set2.iterator();
while(iterator2.hasNext()) {
Map.Entry entry = iterator2.next();
System.out.println(entry.getKey() + entry.getValue());
}
}
}
小例子:计算字符串中字符出现的个数
/**
*计算字符串中字符出现的个数,利用TreeMap实现
*
*/
import java.util.TreeMap;
import java.util.Set;
import java.util.Iterator;
public class Demo {
public static void main(String[] args) {
String s = "jafjkd;afa";
CalNumOfChar cal = new CalNumOfChar(s);
printTreeMap(cal.calNumOfChar());
}
public static void printTreeMap(TreeMap tm) {
Set set= tm.keySet();
Iterator iterator = set.iterator();
while(iterator.hasNext()) {
Character key = iterator.next();
System.out.println(key + "---" + tm.get(key));
}
}
}
class CalNumOfChar {
private String str = "";
private TreeMap tm ;
public CalNumOfChar(String str) {
this.str = str;
}
public TreeMap calNumOfChar() {
char[] chars = str.toCharArray();
tm = new TreeMap();
for(int i = 0; i < chars.length; i++) {
Integer num = tm.get(chars[i]);
if(num == null) {
tm.put(chars[i],1);
}else {
num++;
tm.put(chars[i],num);
}
}
return tm;
}
}