您的位置:首页 > 编程语言 > Java开发

java中集合的详细讲解(总结篇)

2016-09-23 20:09 495 查看
java中的集合有List,Set,Map之前一直混淆,尤其是遍历方法容易牛头不对马嘴,所以今天花点时间好好整理下:

本文只介绍ArrayList,HashSet,TreeSet,HashMap,TreeMap的添加和遍历,队列Queue和栈Stack的基本使用

List:

List list = Arrays.asList("一","二","三","八","五","六","零","一");
Iterator iterator = list.iterator();
while(iterator.hasNext()){                       //使用迭代器遍历

System.out.println(iterator.next());
}

for (String l:                         //for循环

list) {

System.out.println(l);
}

System.out.println(list.get(5));
System.out.println(list.lastIndexOf("一"));
ListIterator stringListIterator = list.listIterator(4);
Spliterator spliterator = list.spliterator();//分迭代器
//        spliterator.forEachRemaining(lang -> System.out.println(lang));
List strings = list.subList(2, 6);    //输出list中从下标2到6的元素

for (String s : strings){

System.out.println(s);
}

HashSet的使用:Set hashset = new HashSet<>();
hashset.add("小二");
hashset.add("大二");
hashset.add("小二");
hashset.add("六");
hashset.add("炸");
Set humanSet =  new HashSet<>();
humanSet.add(new Human("1","2"));
humanSet.add(new Human("3","4"));
humanSet.add(new Human("5","6"));
Human[] s = humanSet.toArray();
Iterator iterator = hashset.iterator();
while(iterator.hasNext()){

System.out.println(iterator.next());
}

Spliterator spliterator = hashset.spliterator();
spliterator.forEachRemaining(s -> System.out.println(s));

TreeSet的使用:Set treeHuman = new TreeSet<>(new Comparator() {

@Override
public int compare(Human o1, Human o2) {

return -1;
}

});
treeHuman.add(new Human("1","2"));
treeHuman.add(new Human("3","4"));
treeHuman.add(new Human("5","6"));
Iterator iterator = treeHuman.iterator();
while (iterator.hasNext()) {

System.out.println(iterator.next());
}

Map之HashMap的使用:Map,String> hashMap = new TreeMap<>();
hashMap.put("123","qwe");
hashMap.put("234","asd");
hashMap.put("345","zxc");
Iterator, String>> iterator = hashMap.entrySet().iterator();//迭代器
while (iterator.hasNext()) {

Map.Entry, String> next = iterator.next();
String s1 = next.getKey();
String s2 = next.getValue();
System.out.println(s1+" "+s2);
}

Object[] objects = hashMap.entrySet().toArray();//数组
for (int i = 0 ; i < objects.length;i++){

Map.Entry object = (Map.Entry) objects[i];
Object key = object.getKey();
Object value = object.getValue();

TreeMap的使用:

Map,Integer> treeMap = new TreeMap<>();
treeMap.put("123",1);
treeMap.put("234",2);
treeMap.put("345",3);
treeMap.put("456",4);
Set strings = treeMap.keySet();
Iterator iterator = strings.iterator();
while (iterator.hasNext()) {

int s = treeMap.get(iterator.next());
System.out.println(s);
}

System.out.println(key+" " +value);
}

队列Queue和栈Stack的讲解:

package sample.org.jasen.TestQueueAndStack;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

class TestQueueAndStack {

static void testQueue(){

Queue queue=new LinkedList();
//添加几个元素
queue.offer("a");
queue.offer("b");
queue.offer("c");
queue.offer("d");
queue.offer("e");
queue.add("1");
queue.add("2");
queue.add("3");
queue.add("4");
queue.add("5");
System.out.println("队列中的元素是:"+queue);
//弹出元素
queue.poll();
System.out.println("队列中的元素是:"+queue);
//查看队列中首个元素,并不移除
String peek=queue.peek();
System.out.println("查看队列中首个元素,并不移除:"+peek);
System.out.println("队列中的元素是:"+queue);
}

static void testStack(){

Stack stack=new Stack();
//添加几个元素
stack.push("a");
stack.push("b");
stack.push("c");
stack.push("d");
stack.push("e");
stack.add("1");
stack.add("2");
stack.add("3");
stack.add("4");
stack.add("5");
System.out.println("栈中的元素是:"+stack);
//弹出元素
stack.pop();
System.out.println("栈中的元素是:"+stack);
//查看栈中首个元素,并不移除
String peek=stack.peek();
System.out.println("查看栈中首个元素,并不移除:"+peek);
System.out.println("栈中的元素是:"+stack);
}

public static void main(String[] args) {

testQueue();
System.out.println("-------栈--------");
testStack();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: