简体   繁体   中英

implementing collections or arrays in java

I have a text file, which has to be processed. The lines contains, country name and some of its states, like below. i want to make it in an array. Is there any collections available which can be implemented?

if(line.contains("India")){
   //add their states to the collections
}

India

Karnataka

TamilNadu

Andhrapradesh

Pakistan

Karachi

Lahore

Australia

Canberra

Adelide

USA

New York

Washington

New Jersy

You can use a Map with String keys:

Map<String, List<String> countryStates = new HashMap<String, List<String>>();
if(line.contains("India")){
    List<String> states = new ArrayList<String>();
    states.add("state1");
    // ...
    countryStates.add("India", states);
}

The more elegant solution would be to use an Object:

public class Country {
    private String name;
    private List<String> states;


    // getters, setters
}
List<Country> countries = //...

签出apache文件utils 行迭代器

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM