A piece of code to understand the use of list, set set and map in Java _java

Source: Internet
Author: User
Tags data structures set set

The use code for the list, set collection, and map in

Java is as follows:

Package Tingjizifu;
Import java.util.*;
The public class Tongji {* * * uses scanner to read a string from the console, statistics the number of occurrences of each character in the string, and requires the use of learned knowledge to complete the above requirements * implementation of the idea according to the set, List, map set of characteristics completed.
*/public static void main (string[] args) {//input string Scanner input = new Scanner (system.in);
String Shuru = Input.next ();
Load string characters into the list set list<string> list = new arraylist<string> (); for (int i = 0; i < shuru.length (); i++) {List.add (string.valueof (Shuru.charat (i)));///load string characters into set collection set<string& Gt
set = new Hashset<string> (); for (int i = 0; i < shuru.length (); i++) {Set.add (string.valueof (Shuru.charat (i))}////compare characters in set with characters in list, plus 1 Count,//Then take the character in the set set as the key (key), count the number as the value (value), and finally print out the data map<string in the map, integer> map = new hashmap<string, Integer
> (); for (String str:set) {int sum = 0;//System.out.println (str), for (int i = 0; i < list.size (); i++) {if List.get (
i). Equals (str)) {sum++}}
Map.put (str, sum);
} set<string> SS = Map.keyset (); for (String str:ss) {System.ouT.PRINTLN (str + "occurrences are" + map.get (str) + "Times");
} input.close (); }
}

Let's look at the difference between the set map list

It's all a collection interface.

Set--the value of which does not allow duplication, unordered data structures
List--where the value is allowed to repeat because it is an ordered data structure
map--data structure, the health value must be unique (the key cannot be the same, otherwise the value is replaced)

List saves objects in the order in which they are entered, without sorting or editing operations.

Set accepts only once for each object and uses its own internal sorting method (usually, you only care about whether an element belongs to the set, not the order of it--otherwise you should use the list).

Map also saves one copy of each element, but this is based on the "key", and the map has a built-in sort, and therefore does not care about the order in which the elements are added. If the order in which you add elements is important to you, you should use Linkedhashset or Linkedhashmap.

Collection is a collection of objects, Collection has two sub-interfaces List and set

List can be obtained by subscript (1,2.) and the value can be repeated

The Set can only be evaluated by a cursor, and the value cannot be duplicated

ArrayList, Vector, LinkedList is the implementation class of List

ArrayList is thread insecure, Vector is thread safe, and the bottom of these two classes are all implemented by arrays

LinkedList is thread insecure, and the bottom is realized by the linked list

Map is a collection of key-value pairs

HashTable and HASHMAP are the implementation classes of the MAP

HashTable is thread safe and cannot store null values

HASHMAP is not thread-safe and can store null values

The above is a small set for you to introduce the Java list, set sets and map of the use of knowledge, hope to help everyone, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.