foreach循环:除了使用迭代器遍历集合之外,使用JDK1.5及以上提供的增强for循环,也可以遍历集合。
foreach循环的格式:
for(变量类型 变量名 : 集合){ //操作变量 }
相当于,每次循环,使用指定变量,去指向集合中的一个对象,然后在循环体中对该变量进行操作
集合
java.util
接口:
Collection
list:允许重复,记录元素进入集合的先后顺序
set:不予许重复,不记录元素进入集合的先后顺序
sortedset set加排序
Map:键值对(key:value) key不允许重复
sortedmap: Map+对key排序
数据类型 变量名=new 数据类型();
实现类:
LIst:
- ArrayList(Vector) 数组:增删慢,查找快 //Vector线程安全
- LInkedLIst 链表:增删快,查找慢
Set: 在map的基础上实现(value为none)
Object
toString()
equals()
hashcode()
- HashSet Hash散列表:HashSet中元素不可重复,主要是靠对象的hashCode和equals方法来判断对象是否重复,HashSet中存储元素是无序的,主要因为它是靠对象的哈希值来确定元素在集合中的存储位置。
- SortedSet 一> TreeSet 红黑树:TreeSet可以将我们存进去的数据进行排序,排序的方式有两种:
自然排序比较器排序(也称客户化排序)
Map:
- HashMap 只对key计算的hash散列表:
- HashTable:
- TreeMap 只对key进行排序:
- LinkedHashMap:
排序规则:
String排序: str1.compareTo(str2);
- 内部排序 comparable
public class Student implements Comparable
public int compareTo(Object o) { //注意,这里this代表我,other代表你,也就是当前要和我比较大小的对象 Student other = (Student)o; //如果我的年龄比你大,就返回正数,说明我比你大 if(this.age > other.age){ return 1; } //如果我的年龄比你小,就返回负数,说明我比你小 else if(this.age < other.age){ return -1; } //其他情况返回0,说明俩个对象(我和你)一样大 else {return 0; }
}
- 外部排序 comparator
package com.collection; import com.bean.Book; import org.junit.Test; import java.util.*; public class CollectionTest { final Comparator<Book> c = new Comparator() { public int compare(Object o1, Object o2) { Book b1 = (Book) o1; Book b2 = (Book) o2; // if (b1.getBookName()=="百年孤独" || b2.getBookName()=="百年孤独") return 1; int ter = b1.getBookName().equals("百年孤独") && !b2.getBookName().equals("百年孤独") ? -1 : (!b1.getBookName().equals("百年孤独") && b2.getBookName().equals("百年孤独") ? 1 : 0); if (ter != 0) return ter; else return (Integer.parseInt(b1.getBookId()) == Integer.parseInt(b2.getBookId())) ? Float.compare(b1.getPrice(), b2.getPrice()) : Integer.compare(Integer.parseInt(b1.getBookId()), Integer.parseInt(b2.getBookId())); // if (Integer.parseInt(b1.getBookId())==Integer.parseInt(b2.getBookId())){ // return Float.compare(b1.getPrice(), b2.getPrice()); // }else{ // return Integer.compare(Integer.parseInt(b1.getBookId()), Integer.parseInt(b2.getBookId())); // } // return Integer.compare(Integer.parseInt(b1.getBookId()), Integer.parseInt(b2.getBookId())); // public int compareTo(Object o){ // Book other = (Book) o; // return Float.compare(this.price, other.price); // /* // 等价于if(this.price > other.price){ // return 1; // }else if(this.price < other.price){ // return -1; // }else{ // return 0;} // */ // } } }; @Test public void test1(){ System.out.println("junit test1"); } @Test public void test2(){ //1、构建一个list集合,存储String类型的元素 ArrayList<String> list=new ArrayList<>(); for(int lm=0; lm<6; lm++){ String str = "hello" + lm; list.add(str); } for(String str:list){ System.out.println(str); } } @Test public void testSet(){ //使用hashset集合添加整数 Set<String> set=new HashSet<>(); for(int i=0; i<6; i++){ String str = "hello" + i; set.add(str); } set.add("hello1"); System.out.println(set.size()); System.out.println(set); } @Test public void bookTestSet(){ //构建三本书依次添加到set集合中 Set<Book> book=new HashSet<Book>(); Book book1=new Book("100001", "百年孤独", 20.9f); // Book book2=book1; Book book2=new Book("100001", "百年孤独", 29.9f); // book.add(new Book("100001", "百年孤独", 20.9f)); // book.add(new Book("100001", "百年孤独", 29.9f)); // book.add(new Book("100001", "百年孤独", 20.9f)); book.add(book1); book.add(book2); for(Book bk:book){ System.out.println(bk); } // System.out.println(book); } @Test public void testTreeSet(){ Set<Book> book= new TreeSet<>(c); book.add(new Book("100002", "第二十二条军规", 29.9f)); book.add(new Book("100001", "百年孤独", 29.9f)); book.add(new Book("100001", "百年孤独", 26.9f)); book.add(new Book("100003", "知更鸟女孩", 19.9f)); for(Book bk:book){ System.out.println(bk); } } @Test public void testTreeMap(){ Map<Integer,String> map=new TreeMap<>(); map.put(1, "tom"); map.put(2, "jack"); map.put(3, "mary"); map.put(4, "lucy"); Set<Integer> keys=map.keySet(); Collection<String> values=map.values(); for(Integer key:keys){ System.out.printf("key : %d,value : %s ",key,map.get(key)); } // System.out.println(map.containsKey(5)); Set<Map.Entry<Integer, String>> es=map.entrySet(); for(Map.Entry<Integer, String> entry:es){ Integer key=entry.getKey(); String value=entry.getValue(); System.out.printf("key : %d,value : %s ",key,value); } } @Test public void testEntrySet(){ } }
HashMap:
map.containsKey(key)检测key=5是否在Map中
遍历map集合:
- 使用map.keyset()
Set<Integer> keys=map.keySet(); Collection<String> values=map.values(); for(Integer key:keys){ System.out.printf("key : %d,value : %s ",key,map.get(key)); }
- 使用entrySet
Set<Map.Entry<Integer, String>> es=map.entrySet(); for(Map.Entry<Integer, String> entry:es){ Integer key=entry.getKey(); String value=entry.getValue(); System.out.printf("key : %d,value : %s ",key,value); }