Home | 简体中文 | 繁体中文 | 杂文 | 打赏(Donations) | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 知乎专栏 | Search | Email

1.5. 数据结构

1.5.1. Collection

		
package cn.netkiller.example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.TreeSet;

public class Test {

	public Test() {
		// TODO Auto-generated constructor stub
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		// A A B E F G C D
		String[] array = { "A", "A", "B", "E", "F", "G", "C", "D" };
		Collection<String> list = new ArrayList<String>(Arrays.asList(array));
		for (String str : list) {
			System.out.print(str + " ");
		}
		System.out.println();

		// A A B E F G C D
		Collection<String> linkedList = new LinkedList<String>(Arrays.asList(array));
		for (String str : linkedList) {
			System.out.print(str + " ");
		}
		System.out.println();

		// 无重复,无序 D E F G A B C
		Collection<String> hashSet = new HashSet<String>(Arrays.asList(array));
		for (String str : hashSet) {
			System.out.print(str + " ");
		}
		System.out.println();

		// 无重复 A B C D E F G
		Collection<String> treeSet = new TreeSet<String>(Arrays.asList(array));
		for (String str : treeSet) {
			System.out.print(str + " ");
		}
		System.out.println();

		// 无重复 A B E F G C D
		Collection<String> linkedHashSet = new LinkedHashSet<String>(Arrays.asList(array));
		for (String str : linkedHashSet) {
			System.out.print(str + " ");

		}

	}

}
		
		

输出结果

A A B E F G C D 
A A B E F G C D 
A B C D E F G 
A B C D E F G 
A B E F G C D 
		

1.5.2. ArrayList

判断元素是否存在

		
import java.util.ArrayList;

public class arraylist {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ArrayList<String> whitelist = new ArrayList<String>();
		whitelist.add("Neo");
		whitelist.add("Jam");
		whitelist.add("Sam");

		if (whitelist.contains("Neo")) {
			System.out.println("Found!");
		}else{
			System.out.println("Not Found!");
		}
	}

}
		
		
		
package cn.netkiller.type;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ArrayListExample {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		List<String> list = new ArrayList<String>();
		list.add("Jack");
		list.add("Jet");
		list.add("Jack");
		list.add("Mike");
		list.add("Kitty");
		list.add("Tom");
		
		//while 循环
		Iterator<String> it = list.iterator();
		while (it.hasNext()) {
			System.out.println(it.next());
		}

		for (Iterator<String> it1 = list.iterator(); it1.hasNext();) {
			System.out.println(it1.next());
		}

		// for 循环
		for (int i = 0; i < list.size(); i++) {
			System.out.println(list.get(i));
		}

		// for 循环加强版
		for (String i : list) {
			System.out.println(i);
		}

	}

}
		
		

ArrayList 转为 Array

		
		String[] array = {"/bin/sh","-c"};
		List<String> list = new ArrayList<String>(Arrays.asList(array));
	    list.add("command");
	    list.add("param");
	   
	    String[] command = (String[]) list.toArray(new String[0]);
	    System.out.println(Arrays.toString(command));
		
		

1.5.2.1. ArrayList to String

			
		List<String> list = new ArrayList<String>();
	    list.add("command");
	    list.add("param");
	   
	    String listString = String.join(", ", list);
	    
	    System.out.println(listString);
	    				
			

1.5.2.2. Array to List

Arrays.asList(array)			
			

1.5.3. HashMap

		
	private static final Map<String, String> point;
	static {
		point = new HashMap<String, String>();
		point.put("CN", "China");
		point.put("HK", "Hongkong");
		point.put("TW", "Taiwan");
	};
		
		

1.5.3.1. foreach hashmap

			
Map<String, Integer> session = new HashMap<String, Integer>();

session.put("A",1);
...
...
session.put("Z",26)

for (Map.Entry<String, Integer> entry : session.entrySet()) {
	System.out.println(String.format("%s:%d", entry.getKey(), entry.getValue()));
}