Java 集合类
集合的长度是可变的,用来存放对象的引用。常见的集合类有List集合、Set集合、Map集合。
List接口
import java.util.Iterator; import java.util.LinkedList; import java.util.List;
public class CollectionDemoList {
public static void main(String[] args) { String aString = "A", bString = "B", cString = "C", dString = "D", eString = "E"; List<String> list = new LinkedList<>(); list.add(aString); list.add(bString); list.add(eString); Iterator<String> iter = list.iterator(); while(iter.hasNext()) { System.out.print(iter.next() + " "); } System.out.println(); list.set(1, cString); Iterator<String> it = list.iterator(); while(it.hasNext()) { System.out.print(it.next() + " "); } }
}
|
结果:
Set接口
import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set;
public class CollectionDemoSet {
public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("dog"); list.add("cat"); list.add("fish"); list.add("cat"); Set<String> set = new HashSet<>(); set.addAll(list); Iterator<String> it = set.iterator(); while(it.hasNext()) { System.out.print(it.next() + " "); } }
}
|
结果:
于Set集合中的对象是无序的,遍历Set集合的结果与插入Set集合的顺序并不相同。
Map接口
Map接口提供了将键映射到值的对象,一个映射不能包含重复的键,每个键最多只能映射一个值。
import java.util.HashMap; import java.util.Map;
public class MapDemo {
public static void main(String[] args) { Map<String, String> map = new HashMap<>(); map.put("1", "dog"); map.put("2", "cat"); map.put("3", "fish"); for(int i=1; i<=3; i++) { System.out.println("第" + i + "个元素是:" + map.get("" + i + "")); } }
}
|
结果:
第1个元素是:dog 第2个元素是:cat 第3个元素是:fish
|
创建Map集合时,Map接口的“<>”中含有两个类型,分别对应其key与value。
List接口的实现类
List接口的实现类常用的有ArrayList和LinkedList。
ArrayList类实现了可变的数组,可以根据索引位置对集合进行快速的随机访问。LinkedList类采用链表结构保存对象,便于向集合中插入和删除对象。
List list1 = new ArrayList(); List list2 = new LinkedList();
|
ArrayList<String> list1=new ArrayList<String>(); list1.add("第一条数据"); list1.add("第二条数据"); list1.add("第三条数据"); list1.add("第四条数据"); list1.add("第五条数据"); list1.add("第三条数据"); list1.add(2,"第六条数据");
System.out.println(list1.size()); System.out.println(list1.get(2));
System.out.println(list1.isEmpty()); System.out.println(list1.contains("第一条数据")); System.out.println(list1.indexOf("第三条数据")); System.out.println(list1.lastIndexOf("第三条数据")); System.out.println(list1.remove(1)); System.out.println(list1.set(0, "替换第一条数据")); List list=list1.subList(2, 5); System.out.println(list1.toString());
LinkedList<News> list2=new LinkedList<News>(); list2.add(new News(1,"xxxxxxx","赵")); list2.add(new News(2,"sssssss","钱")); list2.add(new News(3,"yyyyyyy","孙")); list2.add(new News(4,"nnnnnnn","李")); list2.add(new News(5,"rrrrrrr","周")); System.out.println(list2.contains(new News(3,"yyyyyyy","孙"))); System.out.println(list2.remove(new News(1,"xxxxxxx","赵")));
for (int i = 0; i < list1.size(); i++) { if(list1.get(i) instanceof String){ String str=list1.get(i); System.out.println(str); } }
for (News news : list2) { System.out.println(news.getId()+" "+news.getTitle()+"\t"+news.getAuthor()); }
Iterator<String> iter=list.iterator(); while(iter.hasNext()){ String s=iter.next(); System.out.println(s); }
|
Set接口的实现类
Set接口的实现类常用的有HashSet和TreeSet。
Set<String> set1 = new HashSet<String>(); Set<String> set2 = new TreeSet<String>();
|
Set<String> set1 = new HashSet<String>(); set1.add("set1"); set1.add("set2"); set1.add("set3"); set1.add("set4"); set1.add("set5");
for (String set : set1) { System.out.println(set); }
Set<Person> set3 = new TreeSet<Person>(); set3.add(new Person(11,"a")); set3.add(new Person(22,"b")); set3.add(new Person(33,"c")); set3.add(new Person(44,"d"));
Iterator<Person> iter = set3.iterator(); while(iter.hasNext()){ Person p=iter.next(); System.out.println(p.getId()+"\t"+p.getName()); }
|
Map接口的实现类
Map接口的实现类常用的有HashMap和TreeMap,建议使用HashMap(效率相对较高)。
Map map1 = new HashMap(); Map map2 = new TreeMap();
|
常用方法
put(key,value):向Map的最后追加一个键值对;
get(key):通过键,取到一个值;
clear():清除Map中的所有数据;
containsKey(key):检测是否包含指定的键;
containsValue(obj):检测是否包含指定的值;
replace():替换指定键的值;
遍历Map
keySet():返回Set,先取键,再取值;
values():返回Collection,直接取值;
取代一个entry键值对,返回Set>;
Map<String, String> map1=new HashMap<String, String>(); map1.put("01", "aaaaaa"); map1.put("02", "bbbbbb"); map1.put("03", "cccccc"); map1.put("04", "dddddd");
System.out.println(map1.containsKey("01")); System.out.println(map1.containsValue("aaaa")); System.out.println(map1.replace("03", "dddddd")); System.out.println(map1.get("02"));
Set<String> keys=map1.keySet(); Iterator<String> iter=keys.iterator(); while(iter.hasNext()){ String key=iter.next(); System.out.println(key+" "+map1.get(key)); }
Collection<String> values = map1.values(); Iterator<String> iter1= values.iterator(); while(iter1.hasNext()){ System.out.println(iter1.next()); }
Set<Entry<String,String>> set= map1.entrySet(); Iterator<Entry<String,String>> iter2=set.iterator(); while(iter2.hasNext()){ Entry<String,String> entry=iter2.next(); System.out.println(entry.getKey()+" "+entry.getValue()); }
|
迭代器
迭代器是利用Iterator接口创建的。
Iterator<String> it = set.iterator(); while(it.hasNext()) { System.out.print(it.next() + " "); }
|
上述代码用Iterator接口和iterator()方法创建了一个迭代器;用while循环遍历这个集合,hasNext()方法是循环判断条件,即如果有元素可以迭代,返回true,循环继续执行;next()方法是返回迭代的下一个元素。
参考
https://www.cnblogs.com/adamjwh/p/8372114.html
https://www.cnblogs.com/yanglianwei/p/8847861.html