java-collections-api

Stack using the Java 8 collection streaming API

元气小坏坏 提交于 2020-01-12 06:36:08
问题 I have a method which generates an object each time I execute it, and I need to reverse the order with which I am getting them. So I thought the natural way to do it would be a Stack, since it is LIFO. However, the Java Stack does not seem to play well with the new Java 8 streaming API. If I do this: Stack<String> stack = new Stack<String>(); stack.push("A"); stack.push("B"); stack.push("C"); List<String> list = stack.stream().collect(Collectors.toList()); System.out.println("Collected: " +

Stack using the Java 8 collection streaming API

橙三吉。 提交于 2019-12-03 10:54:54
I have a method which generates an object each time I execute it, and I need to reverse the order with which I am getting them. So I thought the natural way to do it would be a Stack, since it is LIFO. However, the Java Stack does not seem to play well with the new Java 8 streaming API. If I do this: Stack<String> stack = new Stack<String>(); stack.push("A"); stack.push("B"); stack.push("C"); List<String> list = stack.stream().collect(Collectors.toList()); System.out.println("Collected: " + list); The output I get is: Collected: [A, B, C] Why isn't it outputing them in the expected LIFO order

contains() method in List not working as expected

£可爱£侵袭症+ 提交于 2019-12-02 05:47:10
问题 the api of contains() method says "Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)). " I overrode the equals() method in my class but contains() still returns me false when i check my code class Animal implements Comparable<Animal>{ int legs; Animal(int legs){this.legs=legs;} public int compareTo(Animal otherAnimal){ return this.legs-otherAnimal.legs; }

contains() method in List not working as expected

牧云@^-^@ 提交于 2019-12-02 00:52:58
the api of contains() method says "Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)). " I overrode the equals() method in my class but contains() still returns me false when i check my code class Animal implements Comparable<Animal>{ int legs; Animal(int legs){this.legs=legs;} public int compareTo(Animal otherAnimal){ return this.legs-otherAnimal.legs; } public String toString(){return this.getClass().getName();} public boolean equals(Animal otherAnimal){

Sort an ArrayList by primitive boolean type

纵然是瞬间 提交于 2019-11-28 09:43:45
I want to sort my ArrayList using a boolean type. Basically i want to show entries with true first. Here is my code below: Abc.java public class Abc { int id; bool isClickable; Abc(int i, boolean isCl){ this.id = i; this.isClickable = iCl; } } Main.java List<Abc> abc = new ArrayList<Abc>(); //add entries here //now sort them Collections.sort(abc, new Comparator<Abc>(){ @Override public int compare(Abc abc1, Abc abc2){ boolean b1 = abc1.isClickable; boolean b2 = abc2.isClickable; if (b1 == !b2){ return 1; } if (!b1 == b2){ return -1; } return 0; } }); Order before sorting: true true true false

Sort an ArrayList by primitive boolean type

本秂侑毒 提交于 2019-11-27 03:10:26
问题 I want to sort my ArrayList using a boolean type. Basically i want to show entries with true first. Here is my code below: Abc.java public class Abc { int id; bool isClickable; Abc(int i, boolean isCl){ this.id = i; this.isClickable = iCl; } } Main.java List<Abc> abc = new ArrayList<Abc>(); //add entries here //now sort them Collections.sort(abc, new Comparator<Abc>(){ @Override public int compare(Abc abc1, Abc abc2){ boolean b1 = abc1.isClickable; boolean b2 = abc2.isClickable; if (b1 == !b2

Why jdk code style uses a variable assignment and read on the same line - eg. (i=2) < max

梦想的初衷 提交于 2019-11-26 22:06:59
问题 I noticed that in the jdk source code and more specifically in the collections framework there is a preference in assigning variables right before reading them in expressions. Is it just a simple preference or is it something more important which I am not aware of? One reason that I can think of is that the variable is used in this expression only. As I am not used to this style I find it hard to read it. The code is very condensed. Below you can see an example taken from java.util.HashMap