getting duplicate values in map for every key <p>I am trying to put all values in map and i have more then 20k values , now I am trying to put values in map by using idea as key 1 contains values from 1(consider i) to 1000 (i.e i*1000) but the output I'm getting contains duplicate values (key 1 &amp; 2 have same values), not sure what wrong I am doing </p> <p>here is code</p> <pre><code> public class GetNumbers { public static List&lt;String&gt; createList() throws IOException { List&lt;String&gt; numbers = new LinkedList&lt;&gt;(); Path path = null; File file = null; BufferedReader reader = null; String read = ""; try { path = Paths.get("file.txt"); file = path.toFile(); reader = new BufferedReader(new FileReader(file)); while ((read = reader.readLine()) != null) { numbers.add(read); } } catch (FileNotFoundException e) { e.printStackTrace(); } return numbers; } public static Map&lt;Integer, List&lt;String&gt;&gt; createNewFiles() throws IOException { Map&lt;Integer, List&lt;String&gt;&gt; myMap = new HashMap&lt;&gt;(); List&lt;String&gt; getList = GetNumbers.createList(); List&lt;String&gt; list = null; int count = getList.size() / 1000; ---------------------------doubt full code----------------------------------- for (int i = 1; i &lt;= count; i++) { if (getList.size() &gt; 1000) { list = getList.subList(i, i * 1000); } else if (getList.size() &lt; 999) { list = getList.subList(i, getList.size()); } ----------------------------------------------------------------------------- myMap.put(i, list); } return myMap; } public static void getMap() throws IOException { Map&lt;Integer, List&lt;String&gt;&gt; map = GetNumbers.createNewFiles(); List&lt;String&gt; listAtIndexOne = map.get(2); List&lt;String&gt; listAtIndexTwo = map.get(1); for (String elementFromFirstList : listAtIndexOne) { for (String elementFromSecondList : listAtIndexTwo) { if (elementFromFirstList.equals(elementFromSecondList)) { System.out.println("duplicate copy"); } } } } public static void main(String[] args) { try { GetNumbers.getMap(); } catch (IOException e) { e.printStackTrace(); } } } </code></pre> <p><strong>EDIT</strong></p> <p>if I change my Code to </p> <pre><code>for (int i = 0; i &lt;= count; i++) { if (getList.size() &gt; (i * 1000)) { list = getList.subList(i, (i + 1) * 1000); } else if (getList.size() &lt; 999) { list = getList.subList(i, getList.size()); } myMap.put(i, list); } </code></pre> <p>I'm getting</p> <blockquote> <p>Exception in thread "main" java.lang.IndexOutOfBoundsException: toIndex = 25000 at java.util.SubList.(Unknown Source) at java.util.AbstractList.subList(Unknown Source) at com.dnd.GetNumbers.createNewFiles(GetNumbers.java:43) at com.dnd.GetNumbers.getMap(GetNumbers.java:54) at com.dnd.GetNumbers.main(GetNumbers.java:69)</p> </blockquote> <p>Any help is appreciated</p> <p>Thanks </p>