Summary of various usage methods of HashSet

1. HashSet is the implementation class of the set interface , and it is also our most commonly used set collection. It
stores unordered and unique objects.
Because it is unordered , each group of data has no index , and many methods available for list are not
available . There is no method to operate by index,
so it can not be traversed by ordinary for loop , only enhanced for and iterator .

For example :
get(i);
set(int index,Object o);
remove(int index );
etc. There is no method that needs to be operated by index ;

2. Various methods of HashSet:
add
add(null); delete remove(news); compare and find contains(news); clear the set clear(); get the length size( );













Example:

public class NewsHashSet {
public static void main(String[] args) {
News news = new News(1, "It's finally clearing up in Beijing!", "News Agency");
News news2 = new News(2, "Hong Kong Handover Anniversary ", "People's News");
News news3 = new News(3, "Fake milk powder incident exposure", "People's News Network"); //Create a HashSet collection, store disorder, the only data // HashSet is to use equals To compare the objects and determine that the data is unique // If the objects of the two data are consistent, then the HashSet will merge the two and store only one space set.add(news); set.add(news2); set.add(news3); //Because the data stored in HashSet is out of order, get(i); cannot be used to obtain specific objects //So we must Get the various data of HashSet by traversing, because there is no index //so you can't use ordinary type for to traverse it //HashSet can only traverse it through enhanced for and iterator // enhanced for for(News n :set){
















System.out.println(n.getID()+"\t"+n.getLitter()+"\t"+n.getAuthor());
}
System.out.println("******* ************************************");
// Iterator
Iterator<News> it = set.iterator ();
while (it.hasNext()) {
News n = it.next();
System.out.println(n.getID()+"\t"+n.getLitter()+"\t"+n .getAuthor());
} //Various methods of set //set increase set.add(null); //delete set.remove(news); //comparison search set.contains(news); //clear the set set.clear(); //get the length set.size(); } }
























Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324727980&siteId=291194637