List of usage examples for java.util ArrayList ArrayList
public ArrayList()
From source file:Main.java
public static void main(final String[] args) { final List<String> asu = new ArrayList<String>(); asu.add("2"); asu.add("11"); asu.add("7"); asu.add("10"); asu.add("7"); asu.add("12"); asu.add("2"); asu.add("11"); asu.add("11"); asu.add("7"); asu.add("7"); asu.add("7"); List<String> list = new ArrayList<String>(); Map<String, Integer> counts = new HashMap<String, Integer>(); list.addAll(asu);/*from w w w . j a v a 2 s.co m*/ for (String item : list) { Integer count = counts.get(item); if (count == null) { count = 1; } else { count = count + 1; } counts.put(item, count); } Collections.sort(asu, new Comparator<String>() { @Override public int compare(final String left, final String right) { int result = counts.get(left).compareTo(counts.get(right)); if (result == 0) { result = left.compareTo(right); } return result; } }); System.out.println(asu); }
From source file:Main.java
public static void main(String[] args) { List<Student> members = new ArrayList<>(); members.add(new Student("P", Sex.MALE, 12, "p@g.com")); members.add(new Student("J", Sex.MALE, 42, "j@g.com")); members.add(new Student("K", Sex.FEMALE, 22, "k@g.com")); members.add(new Student("L", Sex.FEMALE, 52, "l@g.com")); for (Sex gender : Sex.values()) { System.out.println("All " + gender + ":"); processMembersWithFunction(members, p -> p.getGender() == gender, Student::getName, System.out::println); }//ww w . ja va2s .com System.out.println("All FEMALE over 30:"); processMembersWithFunction(members, p -> p.getGender() == Sex.FEMALE && p.getAge() > 30, Student::getName, System.out::println); }
From source file:NewStyle.java
public static void main(String args[]) { // Now, list holds references of type String. ArrayList<String> list = new ArrayList<String>(); list.add("one"); list.add("two"); list.add("three"); list.add("four"); // Notice that Iterator is also generic. Iterator<String> itr = list.iterator(); // The following statement will now cause a compile-time eror. // Iterator<Integer> itr = list.iterator(); // Error! while (itr.hasNext()) { String str = itr.next(); // no cast needed // Now, the following line is a compile-time, // rather than runtime, error. // Integer i = itr.next(); // this won't compile System.out.println(str + " is " + str.length() + " chars long."); }// w w w . j a v a2 s . co m }
From source file:Main.java
public static void main(String[] args) { String s = "(123, 234; 345, 456) (567, 788; 899, 900)"; Matcher m = Pattern.compile("\\d+").matcher(s); List<Integer> numbers = new ArrayList<Integer>(); while (m.find()) { numbers.add(Integer.parseInt(m.group())); }/*from www . jav a 2s . co m*/ System.out.println(numbers); }
From source file:Main.java
License:asdf
public static void main(String[] args) { int howManyWords = 2; List<String> listOfWords = new ArrayList<>(); Random random = new Random(); listOfWords.addAll(Arrays.asList(randomMessages)); List<String> selectedRandomMessages = new ArrayList<>(); for (int i = 0; i < howManyWords; i++) { int randomNumber = random.nextInt(listOfWords.size()); String randomItem = listOfWords.get(randomNumber); selectedRandomMessages.add(randomItem); listOfWords.remove(randomItem);//from w w w .j a v a 2 s. c om } System.out.println(selectedRandomMessages); }
From source file:Main.java
public static void main(String[] args) { Collection<String> c = Collections.synchronizedCollection(new ArrayList<String>()); List<String> list = Collections.synchronizedList(new ArrayList<String>()); Set<String> s = Collections.synchronizedSet(new HashSet<String>()); Map<String, String> m = Collections.synchronizedMap(new HashMap<String, String>()); }
From source file:MainClass.java
public static void main(String args[]) { ArrayList<String> al = new ArrayList<String>(); System.out.println("Initial size of al: " + al.size()); al.add("C");// w w w .j ava 2 s . c o m al.add("A"); al.add("E"); al.add("B"); al.add("D"); al.add("F"); al.add(1, "A2"); System.out.println("Size of al after additions: " + al.size()); System.out.println("Contents of al: " + al); al.remove("F"); al.remove(2); System.out.println("Size of al after deletions: " + al.size()); System.out.println("Contents of al: " + al); }
From source file:Main.java
public static void main(String[] argv) { List<DayOfWeek> list = new ArrayList<>(); list = (List<DayOfWeek>) Stream.of(Month.values()).map(month -> LocalDate.now().withYear(2010).with(month) .with(TemporalAdjusters.lastDayOfMonth()).getDayOfWeek()).collect(Collectors.toList()); System.out.println(list);/*from w w w. j a v a2 s .c o m*/ }
From source file:Main.java
public static void main(String[] args) { List<String> texts = new ArrayList<>(); texts.add("test"); texts.add("test"); Path dest = Paths.get("twinkle.txt"); Charset cs = Charset.forName("US-ASCII"); try {/*from w w w . j a v a 2s.co m*/ Path p = Files.write(dest, texts, cs, StandardOpenOption.WRITE, StandardOpenOption.CREATE); System.out.println("Text was written to " + p.toAbsolutePath()); } catch (IOException e) { e.printStackTrace(); } }
From source file:MainClass.java
public static void main(String args[]) { ArrayList<String> al = new ArrayList<String>(); al.add("C");/* w w w . ja va 2 s. c o m*/ al.add("A"); al.add("E"); al.add("B"); al.add("D"); al.add("F"); System.out.print("Original contents of al: "); Iterator<String> itr = al.iterator(); while (itr.hasNext()) { String element = itr.next(); System.out.print(element + " "); } System.out.println(); ListIterator<String> litr = al.listIterator(); while (litr.hasNext()) { String element = litr.next(); litr.set(element + "+"); } // Now, display the list backwards. System.out.print("Modified list backwards: "); while (litr.hasPrevious()) { String element = litr.previous(); System.out.print(element + " "); } }