码迷,mamicode.com
首页 > 编程语言 > 详细

java Set

时间:2020-10-18 16:16:23      阅读:17      评论:0      收藏:0      [点我收藏+]

标签:second   while   michael   alt   ges   for   核心   exce   pac   

简介

java 基础数据结构 SET, java核心编程
统计不同的字符串

code

import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;

public class SetTest {
  public static void main(String[] args) {
    Set<String> words = new HashSet<>();
    long totaltime = 0;

    try (Scanner in = new Scanner(System.in)) {
      while (in.hasNext()) {
        String word = in.next();
        long callTime = System.currentTimeMillis();
        words.add(word);
        callTime = System.currentTimeMillis() - callTime;
        totaltime += callTime;
      }
    }
    Iterator<String> iter = words.iterator();
    for (int i = 1; i <= 20 && iter.hasNext(); i++) {
      System.out.println(iter.next());
    }
    System.out.println("...");
    System.out.println(words.size() + " distinct words. " + totaltime +
                       " milliseconds.");
  }
}

命令调用

java SetTest < alice.txt

result

> /opt/jdk-14.0.2+12/bin/java -XX:+ShowCodeDetailsInExceptionMessages -Dfile.encoding=UTF-8 -cp /home/ling/.vscode-server/data/User/workspaceStorage/f8f4922c995979dd665d7ceea52d62e1/redhat.java/jdt_ws/java_c7cf4489/bin SetTest < alice.txt 
frowning
prepare
half
don‘t
spoke
mine,
hall
_should_
turtles,
mine.
pretend
gimble
tea
twinkling
would
pick
rabbit
Michael
character,
electronically
...
4654 distinct words. 4 milliseconds.

java Set

标签:second   while   michael   alt   ges   for   核心   exce   pac   

原文地址:https://www.cnblogs.com/eat-too-much/p/13824977.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!