Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
158 views
in Technique[技术] by (71.8m points)

Java -read from a text file and Add item( string , arrayList) to hashmap

This is the text file : output1.txt

zzz ***Wed Jan 15 10:00:03 +08 2020
a : 20
b : 30
c : 40
zzz ***Wed Jan 15 11:00:03 +08 2020
a : 22
b : 24
c : 25

I am trying to add the date in String and a,b,c as ArrayList into a hash map:

Desired output:

{zzz ***Wed Jan 15 10:00:03 +08 2020=[a : 20, b : 30, c : 40],
 zzz ***Wed Jan 15 11:00:03 +08 2020=[a : 22, b : 24, c : 25]}

My code:

    String dateString ="";
    ArrayList<String> value = new ArrayList<String>();
    HashMap<String, ArrayList> result = new HashMap<String, ArrayList>();

    String fileName = "/Users/--/Downloads/output1.txt";
    File file = new File(fileName);
    FileReader fr = new FileReader(file);
    BufferedReader br = new BufferedReader(fr);
    String line;

    while ((line = br.readLine()) != null) {
        if (line.startsWith("zzz")) {
            dateString = line;
        } else {
            value.add(line);
        }
        result.put(dateString, value);
    }
    System.out.println(result);

And the result I got is:

{zzz ***Wed Jan 15 10:00:03 +08 2020=[a : 20, b : 30, c : 40, a : 22, b : 24, c : 25], 
 zzz ***Wed Jan 15 11:00:03 +08 2020=[a : 20, b : 30, c : 40, a : 22, b
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is example code.

We use Scanner to parse each line of the input string. The scanner object is defined within a try-with-resources, to be automatically closed.

We define a date-time formatting pattern with DateTimeFormatter to match your funky input. Tip: Teach the publisher of your data about the ISO 8601 standard defining practical formats for exchanging date-time values as text.

As our key, we parse the first of each four lines to get a OffsetDateTime object. This is the key in our Map. Our map is defined concretely as a TreeMap, to keep the keys in sorted chronological order.

We build a List of Integer numbers parsed from the expected "a, b, c" values. We use the new List.of feature for simple syntax in building a List object of an unknown concrete type. A List keeps the elements in the order in which they were added, so we know the first is a, second is b, and third is c.

String s = "zzz ***Wed Jan 15 10:00:03 +08 2020
" +
        "a : 20
" +
        "b : 30
" +
        "c : 40
" +
        "zzz ***Wed Jan 15 11:00:03 +08 2020
" +
        "a : 22
" +
        "b : 24
" +
        "c : 25";

DateTimeFormatter f = DateTimeFormatter.ofPattern( "'zzz ***'EEE MMM dd HH:mm:ss x uuuu" ).withLocale( Locale.US );
Map < OffsetDateTime, List < Integer > > momentCounts = new TreeMap <>();
try (
        Scanner scanner = new Scanner( s ) ;
)
{
    while ( scanner.hasNextLine() )
    {
        OffsetDateTime moment = OffsetDateTime.parse( scanner.nextLine() , f );
        List < Integer > counts = List.of(
                Integer.valueOf( scanner.nextLine().replace( "a : " , "" ) ) ,
                Integer.valueOf( scanner.nextLine().replace( "b : " , "" ) ) ,
                Integer.valueOf( scanner.nextLine().replace( "c : " , "" ) )
        );
        momentCounts.put( moment , counts );
    }
}

System.out.println( "momentCounts = " + momentCounts );

When run.

momentCounts = {2020-01-15T10:00:03+08:00=[20, 30, 40], 2020-01-15T11:00:03+08:00=[22, 24, 25]}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...