JAVA frame set (two) and Set -List

List of common implementation class

list set is ordered, the order is added, the element is repeatable.

  • ArrayList
  • LinkedList
  • Vector

ArrayList

Based on the underlying array implementation. In the process add elements, if more than the array capacity expansion automatically.
Main features: Fast Random Access (characteristic of the array), but at a slower intermediate element insertion and removal (copy operation involving the array).

LinkedList

Based on the underlying design of linked list data structure. Main features: fast insertions and deletions in the middle. But random access is slow (need to traverse the node pointer). LinkedList some proprietary methods:

  • void addFirst (E e) add the specified element is set at the beginning of this
  • void addLast (E e) add the specified element to the end of the collection
  • E getFirst () Returns a collection of the first element
  • E getLast () Returns the last element of this collection
  • E removeFirst () Remove the first item in this collection
  • E removeLast () to delete the last element in this collection

Vector achieved similar ArrayList. But Vector is thread-safe. Most of its methods are added to the genlock synchronized

Set common implementation class

set collection does not save duplicate elements.

  • HashSet
  • TreeSet
  • LinkedHashSet

HashSet

Hash algorithm storage according HashSet set of elements, with good access and search performance. Judged by the hash element () and equals () method whether the element is repeated. Thus, storage order HashSet elements and order of addition of the elements is not the same.

LinkedHashSet

LinkedHashSet is HashSet subclasses. Use the list to maintain the order of addition of elements, so adding storage order is the order of elements.

TreeSet

Based on the storage elements underlying red-black tree data structure. TreeSet to ensure that the stored elements in the collection in an orderly state. This can be a natural sort order and custom order. Natural order:
TreeSet calls the compareTo element of the set (Object obj) method to compare the size relationship between the elements, and then let the set in ascending order. Added elements of the collection need to achieve Comparable interface, override compareTo ().

public class Person implements Comparable<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 void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + "]";
	}
	public void setAge(int age) {
		this.age = age;
	}
	
	// 按年龄排序
	public int compareTo(Person person) {
		int ret = (this.age - person.age);
		return ret;
	}
}

测试类:  
public class setMain {
	public static void main(String[] args) {
		Set<Person> personSet = new TreeSet<Person>();
		personSet.add(new Person("张三",10));
		personSet.add(new Person("王五",23));
		personSet.add(new Person("李四",15));
		System.out.println(personSet);
	}
	
}

复制代码

Output:

[Person [name = Joe Smith, age = 10], Person [name = John Doe, age = 15], Person [name = Wang Wu, age = 23]]

Custom ordering:
If you want to customize the sort, need to pass Comparator comparator TreeSet constructor.

public class NameComparator implements Comparator<Person>{

	public int compare(Person person1, Person person2) {
		return person1.compareTo(person2);
	}
}

测试类:   
public class setMain {
	public static void main(String[] args) {
		Set<Person> set = new TreeSet<Person>(new NameComparator());
		set.add(new Person("张三",10));
		set.add(new Person("王五",23));
		set.add(new Person("李四",15));
		System.out.println(set);
	}
}
复制代码

Output:

[Person [name = Joe Smith, age = 10], Person [name = John Doe, age = 15], Person [name = Wang Wu, age = 23]]

Watch ants programming ↓

Guess you like

Origin juejin.im/post/5d0dcda4f265da1b833396c4