Memorandum No.2 "Making a search history with ArrayList and HashSet" [Java]

Introduction

Recorded because I had a hard time in the introduction to paiza's skill check.

What you want to do

  1. Enter multiple character strings of any number of characters and have them in the list.
  2. At that time, if the entered character string already exists in the list, delete the previous character string and add a new one.
  3. Output from the top in order from the most recently input.

Code 1

    Scanner sc = new Scanner(System.in);
    List<String> words = new ArrayList<>();
    while(sc.hasNext()) {
        String s = sc.nextLine();

if (words.contains (s)) {// Check if it's already in the list and delete it if it exists words.remove(s); } words.add (0, s); // Add the entered string to the beginning of the list }

    for(String word : words) {
        System.out.println(word);
    }

Problems with code 1

-If you increase or decrease the value of ArrayList with remove () or add (), all the stored values must be shifted, so it takes time to process. -Since contains () of ArrayList also searches all the values in the list, the more the stored values, the longer the processing time.

solution

-Use HashSet to search for character strings. → Elements can be converted to hash values and used for searching, and values can be found quickly.

Code 2

Add all the strings entered to the list and use HashSet to hide duplicates. (I don't actually delete it, I assume it wasn't there)

    Scanner sc = new Scanner(System.in);

    List<String> words = new ArrayList<>();
    while(sc.hasNext()) {
        String s = sc.nextLine();
        words.add(s);
    }

Collections.reverse (words); // Invert the list to output from the newly entered one

Set usedWords = new HashSet <> (); // HashSet used to determine duplicate strings in the list

    for(String word : words) {

if (! usedWords.contains (word)) {// Determine if it has already been added to usedWord (duplicate) and display if not System.out.println(word); } usedWords.add(word); }

Recommended Posts

Memorandum No.2 "Making a search history with ArrayList and HashSet" [Java]
Memorandum No.4 "Get a character string and decorate it" [Java]
Prepare a scraping environment with Docker and Java
Story of making a task management application with swing, java
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
Socket communication with a web browser using Java and JavaScript ②
Socket communication with a web browser using Java and JavaScript ①
Search and execute method by name from instance with processing (java)
Solving with Ruby, Perl and Java AtCoder ABC 136 D Breadth-first search
Create a high-performance enum with fields and methods like Java with JavaScript
Use java with MSYS and Cygwin
Distributed tracing with OpenCensus and Java
Install Java and Tomcat with Ansible
Output PDF and TIFF with Java 8
[Java] Difference between array and ArrayList
Encrypt with Java and decrypt with C #
A memorandum with NTP (chrony) set
Find the address class and address type from the IP address with Java [No. 2 decoction]
The story of making a game launcher with automatic loading function [Java]
[Java] Create a jar file with both compressed and uncompressed with the jar command
I wrote a Lambda function in Java and deployed it with SAM
<java> Split the address before and after the street address with a regular expression
How to make an app with a plugin mechanism [C # and Java]