Type listType = new TypeToken<List<Country>>() {}.getType();
Gson gson = new Gson();
String json = "["
+ "{\"CountryID\" : \"3\",\"CountryName\" : \"China\"},"
+ "{\"CountryID\" : \"2\",\"CountryName\" : \"Japan\"},"
+ "{\"CountryID\" : \"1\",\"CountryName\" : \"Australia\"},"
+ "{\"CountryID\" : \"4\",\"CountryName\" : \"India\"},"
+ "{\"CountryID\" : \"5\",\"CountryName\" : \"Holland\"}"
+ "]";
ArrayList<Country> countries = gson.fromJson(json, listType);
System.out.println(countries);
Collections.sort(countries);
System.out.println(countries);
Country searchKey = new Country(3,"");
int index = Collections.binarySearch(countries, searchKey);
if(index < 0 || index >= countries.size() ) System.out.println("Country with ID "+searchKey.getCountryID()+ " not found in results");
else
{
System.out.println("Found Country with ID : " + searchKey.getCountryID()+ " @ index : " + index);
Country searchResult = countries.get(index);
System.out.println("Searched result : "+searchResult);
}